Salesforce Apex触发器,将帐户所有者更改为事件所有者
我是一名新手,我想请您提供以下帮助: 我在用户X拥有的帐户上创建一个新事件,并将此事件分配给用户Y。 然后,我希望有一个触发器将帐户的所有权更改为用户Y(以前由用户X拥有)。 我怎么做Salesforce Apex触发器,将帐户所有者更改为事件所有者,salesforce,apex,Salesforce,Apex,我是一名新手,我想请您提供以下帮助: 我在用户X拥有的帐户上创建一个新事件,并将此事件分配给用户Y。 然后,我希望有一个触发器将帐户的所有权更改为用户Y(以前由用户X拥有)。 我怎么做 非常感谢只要每次在此帐户上创建新事件时更改帐户上的某些字段,就可以解决此问题。 事件发生时需要触发器: trigger EventTrigger on Event (after insert){ EventTriggerHandler.changeAccountOwner(Trigger.New); }
非常感谢只要每次在此帐户上创建新事件时更改帐户上的某些字段,就可以解决此问题。 事件发生时需要触发器:
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;
}
}
}