Triggers 创建用于验证产品对象数据的触发器

Triggers 创建用于验证产品对象数据的触发器,triggers,salesforce,apex,visualforce,Triggers,Salesforce,Apex,Visualforce,我有一个apex触发器(在插入/更新之前)和一个helper类。问题是:在创建对象记录时,触发器应该检查addedate字段是否已填充,如果未填充,则为其指定今天的日期和当前时间 当我创建和更新产品对象记录时,触发器必须检查描述字段的长度,如果字段长度超过200个字符,我必须将其修剪为197个字符,并在行尾添加一个三元组。 我做错了什么?我应该如何继续 我的触发器: 触发器帮助器类: 公共类ProductTriggerHelper{ 公共静态void producthandler(列出产品){

我有一个apex触发器(在插入/更新之前)和一个helper类。问题是:在创建对象记录时,触发器应该检查addedate字段是否已填充,如果未填充,则为其指定今天的日期和当前时间

当我创建和更新产品对象记录时,触发器必须检查描述字段的长度,如果字段长度超过200个字符,我必须将其修剪为197个字符,并在行尾添加一个三元组。 我做错了什么?我应该如何继续

我的触发器:

触发器帮助器类:

公共类ProductTriggerHelper{
公共静态void producthandler(列出产品){
Schema.descripebieldResult F=产品描述获取描述();
整数lengthOfField=F.getLength();
//List prList=新列表();
用于(产品采购:产品){
pr.AddedDate_uuc=system.today();
if(String.isNotEmpty(pr.Description\uuu\c)){
pr.Description\uuuuu c=pr.Description\uuuuu c.缩写(lengthOfField);
}
} 
}
}

根据您的要求

创建对象记录时,触发器应检查AddedDate字段是否已填充,如果未填充,则为其指定今天的日期和当前时间

你没有那样做

Change
pr.AddedDate_uuc=system.today()

if(pr.AddedDate\uuu c==null){pr.AddedDate\uu c=system.today();}

此外,根据参数,它采用的是最大长度,包括3个省略号

因此,更改
pr.Description\uuuuu c=pr.Description\uuuu c.缩写(lengthOfField)

pr.Description\uuuu c=pr.Description\uu c.缩写(200)

添加到的答案中

您将触发器定义为插入之前、更新之前的
。酷,这是做数据验证,字段预填充的完美地方。。。您将免费获得“保存到数据库”功能

但是,这与下一行的if(Trigger.isUpdate&&Trigger.isAfter){
冲突。使用此设置,它将永远不会触发。要么完全删除if,要么(如果您认为触发器将来可以获得更多事件)使用
Trigger.isBefore&(Trigger.isInsert | Trigger.isUpdate)

另外,它是datetime字段?所以
pr.AddedDate\uu c=system.now();
更好

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


}
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);
            }
        } 
    }

}