1

I'm trying to update an Email field on an sObject, we have lots of relationships, so using workflow rule is out of the question. Basically I need to send and email alert to the account owner of a related account. Thought triggers should do the trick, for some reason it's not working. Am I missing something on my code? I mean, I copied from another code I have and works.

Trigger snippet:

//Header
trigger OpportunityTrigger on Opportunity (before insert, after insert, before update, after update,before delete, after delete) {    

    new Triggers()
    .bind(Triggers.Evt.afterUpdate, new XBEBR_Sync2BPDealHandler())    
    .manage(); 

    if(LenovoUtility.isTriggerEnabled() ){ 

        OpportunityTriggerHandler handler = new OpportunityTriggerHandler();

        handler.newRecordList = trigger.new;
        handler.oldRecordList = trigger.old;
        handler.newRecordMap = trigger.newMap;
        handler.oldRecordMap = trigger.oldMap;    
        if(trigger.isBefore){
            if(trigger.isInsert){ 
                OpportunityTriggerHandler.setPartnerManagerEmail(Trigger.New);
            }
            if(trigger.isUpdate){ handler.onBeforeUpdate(); }
            //if(trigger.isDelete){ handler.onBeforeDelete(); }
        }

Handler method:

public static void setPartnerManagerEmail (List<Opportunity> oppList) {
    Set<Id> partnerAccounts = new Set<Id> ();
    for (Opportunity o : oppList) {
        partnerAccounts.add(o.Partner_Account__c);
    }

    Map <Id, Account> accountMap = new Map<Id,Account>([select id, Owner.Email from Account where id in :partnerAccounts]);
    for (Opportunity o : (List<Opportunity>) oppList) {
        o.EmailPartnerAccountManager__c = accountMap.get(o.Partner_Account__c).Owner.Email;
    }
} ...

The selection on Account is to get the right email address. I found no other way of retrieving it.

Any help will be greatly appreciated.

  • 2
    can you add trigger heading code? does it have before insert keyword and is it also activated? – Pranay Jaiswal Jul 17 at 17:27
  • so Opportunity.PartnerAccountManager is null? or is non-null and no email is sent? – cropredy Jul 17 at 17:37
  • The trigger is firing, if I insert a debug line after assign the variable, it prints the right value. It seems it's not committing right. After the trigger the EmailPartnerAccountManager field continues blank. – Thiago Aciely Rodrigues Jul 18 at 11:45
  • What does LenovoUtility.isTriggerEnabled do? What about the binding above that line? Do you have any other workflows or Process Builder flows which could be interfering? – Adrian Larson Jul 18 at 12:17
  • It enables or disables the triggers org wide. But I figured it out. I'll right the answer right away – Thiago Aciely Rodrigues Jul 23 at 17:07
0

I figured it out,

It's quite a complex architecture, there's this trigger, that creates a new object, this new object has a trigger that updates the first one, but for some reason, instead of updating it was making the following:

Opportunity opp = new Opportunity();
opp.ID = oldOpp.Id;

And procedded on setting the fields. That's why the field I was setting was blank, it was being reinitialized later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.