APEX 文本框更新时触发

APEX Trigger when a textfield gets updated

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

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

看起来像这样:

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
}

有人可以帮我语法吗?

您可以利用内置的 Trigger.newTrigger.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

的文档