从Salesforce中的活动流程实例对象更新自定义对象

时间:2013-09-16 15:25:47

标签: triggers salesforce apex-code

此触发器从Process Instance对象中提取值,将它们存储在列表中,并更新自定义对象上的字段。问题是,为了查看值,用户必须更新并保存记录。通常我会在Process Instance对象上添加一个'After Update'触发器,但Salesforce不允许这样做。

任何帮助或建议都将不胜感激。

在Service_Request__c上触发update_Provisioner(更新前){

for(Service_Request__c sr:Trigger.new){

List <ProcessInstance> pi = [SELECT Id, CreatedDate from ProcessInstance where TargetObjectId = :sr.Id ORDER BY CreatedDate DESC limit 1];
List <ProcessInstanceStep> op = [SELECT Id, StepStatus, ActorId, OriginalActorId, CreatedDate FROM ProcessInstanceStep where ProcessInstanceId = :pi[0].Id ORDER BY CreatedDate DESC limit 1];


        if(op.size()>0){

        //System.debug('Hello'+sr.Provisioner__c+' '+op[0].StepStatus + ' '+ op[0].ActorId + ' ' +op[0].OriginalActorId + ' '+ op[0].CreatedDate);
        sr.Provisioner__c = op[0].ActorId;

          }       

}

}

1 个答案:

答案 0 :(得分:0)

将update_Provisioner触发器更改为After Update事件。 再次更新trigger.new列表,并通过静态标志停止循环。

这样的事情:

trigger update_Provisioner on Service_Request__c (after update) {
    if(flag)
      return;
    flag=true;
    for(Service_Request__c sr:Trigger.new){

List <ProcessInstance> pi = [SELECT Id, CreatedDate from ProcessInstance where TargetObjectId = :sr.Id ORDER BY CreatedDate DESC limit 1];
List <ProcessInstanceStep> op = [SELECT Id, StepStatus, ActorId, OriginalActorId, CreatedDate FROM ProcessInstanceStep where ProcessInstanceId = :pi[0].Id ORDER BY CreatedDate DESC limit 1];


        if(op.size()>0){

        //System.debug('Hello'+sr.Provisioner__c+' '+op[0].StepStatus + ' '+ op[0].ActorId + ' ' +op[0].OriginalActorId + ' '+ op[0].CreatedDate);
        sr.Provisioner__c = op[0].ActorId;

          }       

}
update trigger.new;
}