简体   繁体   English

创建一个触发器来验证 Product 对象的数据

[英]Create a trigger that validates the data for the Product object

I have an apex trigger (before insert/update) and a helper class to that trigger.我有一个顶点触发器(在插入/更新之前)和一个该触发器的辅助类。 The problem is: When creating an object record, the trigger should check if the AddedDate field is filled and if it's not - then assign it today's date and current time.问题是:创建对象记录时,触发器应检查AddedDate 字段是否已填充,如果未填充,则为其分配今天的日期和当前时间。

And when I create and update a Product object record, the trigger must check the length of the Description field, if the field is longer than 200 characters, I must trim it to 197 characters and add a triple to the end of the line.当我创建和更新 Product 对象记录时,触发器必须检查 Description 字段的长度,如果该字段超过 200 个字符,我必须将其修剪为 197 个字符并在行尾添加一个三元组。 What am I doing wrong and how should I proceed?我做错了什么,我应该如何继续?

My trigger :我的触发器

trigger ProductTrigger on Product__c (before insert, before update) { 
       if(Trigger.isUpdate && Trigger.isAfter){
        ProductTriggerHelper.producthandler(Trigger.new);
    }


}

Trigger helper class :触发助手类

public class ProductTriggerHelper {

    public static void producthandler(List<Product__c> products) {
        Schema.DescribeFieldResult F = Product__c.Description__c.getDescribe(); 
        Integer lengthOfField = F.getLength();

        //List<Product__c> prList = new list<Product__c>(); 
        for(Product__c pr: products){

            pr.AddedDate__c=system.today();

            if (String.isNotEmpty(pr.Description__c)) {
               pr.Description__c = pr.Description__c.abbreviate(lengthOfField);
            }
        } 
    }

}

According to your requirements根据您的要求

When creating an object record, the trigger should check if the AddedDate field is filled and if it's not - then assign it today's date and current time.创建对象记录时,触发器应检查AddedDate 字段是否已填充,如果未填充,则为其分配今天的日期和当前时间。

You aren't doing that.你不这样做。

Change pr.AddedDate__c=system.today();更改pr.AddedDate__c=system.today(); to

if (pr.AddedDate__c == null) { pr.AddedDate__c=system.today(); }

Also according to the abbreviate function documentation the parameter it takes is the max length including the 3 elipses.此外,根据缩写函数文档,它采用的参数是最大长度,包括 3 个省略号。

So change pr.Description__c = pr.Description__c.abbreviate(lengthOfField);所以改变pr.Description__c = pr.Description__c.abbreviate(lengthOfField); to

pr.Description__c = pr.Description__c.abbreviate(200);

To add to Programmatic 's answer...要添加到Programmatic的答案...

You defined the trigger as before insert, before update .before insert, before update定义了触发器。 Cool, that's perfect place for doing data validations, field prepopulation... And you'll get save to database for free!太酷了,这是进行数据验证、字段预填充的理想场所……而且您可以免费保存到数据库!

But then this clashes with next line if(Trigger.isUpdate && Trigger.isAfter){ .但是这与下一行if(Trigger.isUpdate && Trigger.isAfter){发生冲突。 With this setup it'll never fire.有了这个设置,它永远不会开火。 Either remove the if completely or (if you think trigger can get more events in future) go with trigger.isBefore && (trigger.isInsert || trigger.isUpdate) .要么完全删除 if 要么(如果您认为触发器将来可以获取更多事件)使用trigger.isBefore && (trigger.isInsert || trigger.isUpdate)

PS It's datetime field? PS 这是日期时间字段? So pr.AddedDate__c=system.now();所以pr.AddedDate__c=system.now(); is better更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM