APEX在文本字段更新时触发

时间:2017-04-10 15:34:50

标签: apex

我正在尝试在APEX中创建触发器,当自定义sObject的自定义 textfield 更新产品时(意味着,当新产品插入或现有产品被删除时)。

如何在APEX中将Trigger.Old值与Trigger进行比较?此字段的新值以启动触发器。

它看起来像这样:

Trigger NameOfTrigger on CustomSObject__c (after update){

/*there is already an existing list of products that get insert into the custom textfield (probably as Strings)
*/

List <String> textList = new List <String> (); 

/*PseudoCode: if the textfield got updated/has changed, copy from every entry of this textfield (entry = product name as a string) and copy fieldX into another sObject
*/

if(CustomSObject.field(OldValues) != CustomSObject.field(NewValues)){
for (String product : textList){
   //Trigger e.g. copy the values of a certain field of p and paste them in another sObject
}

有人可以帮我解释语法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用内置的 Trigger.new Trigger.old 来获取任何记录的最新值和旧值。这些列表可用于实现您的目标。

示例示例如下:

Trigger NameOfTrigger on CustomSObject__c (after update){

    for(CustomSObject__c customObject : Trigger.new) {

        // get old record
        CustomSObject__c oldCustomObject = Trigger.oldMap.get(customObject.Id);

        // compare old and new values of a particular field
        if(customObject.fieldName != oldCustomObject.fieldName){
            //Trigger e.g. copy the values of a certain field of p and paste them in another sObject
        }

    }

}

请参阅Trigger.new & Trigger.old

的文档