Apex触发器,将帐户所有者更改为事件所有者

时间:2015-03-11 09:24:10

标签: salesforce apex

我是新手,我想问你一点帮助:

我在User X拥有的帐户上创建一个新事件,并将此事件分配给用户Y. 然后,我想要一个触发器,将帐户的所有权更改为用户Y(以前由用户X拥有)。 我该怎么办?

非常感谢

1 个答案:

答案 0 :(得分:0)

每次在此帐户上创建新事件时,只需更改帐户上的某些字段即可解决此问题。 您需要在事件上触发:

trigger EventTrigger on Event (after insert){
    EventTriggerHandler.changeAccountOwner(Trigger.New);
}

然后在EventTriggerHandler类中你会得到这个:

public static class EventTriggerHandler{
   public void changeAccountOwner(Event triggerNew){
      //we are cheching the prefix of the field on the event that tells you where it was created
      String idPrefix = String.valueOf(triggerNew.WhatId).subString(0,2);

      if(idPrefix == '001'){ // this is because the event may be created sometwhere else and not always in an account
          Account a = [SELECT Id, OwnerId FROM Account WHERE Id = :triggerNew.WhatId];

          //we just place the owner of the event to be the owner of the account
          a.OwnerId = triggerNew.OwnerId;
          update a;
      }
   }
}