+ Start a Discussion
SFDC-DMGSFDC-DMG 

Apex Code on Activity to pull in related record info - Prioritize

Hello,

 

I created apex triggers to pull in information from the related record. I am all set with the leads. However, I have one for Contact and one for Opportunity. I want to Opportunity one to trigger/populate first and then, only if that is empty, to pull in the Contact information as a second step. I have them as two separate triggers, and I'm not sure how to combine them.

 

Could someone please help with figuring out either a) combining these into one to ensure that the order above is correct; or b) let me know if there is a way to have the Contact one trigger after the Opportunity one

 

I am familiar enough with apex that I was able to create these two simple separate ones, but I have no idea how I would make them more advanced to accomplish the above goal.

 

Thank you in advance for any guidance/help that you can give me!

 

Here are my two triggers:

 

Contact:

 

trigger ActivityContactDetails on Task (before insert, before update){
// Goad: Pull fields from the contact record for tracking activities related to lists and to discern in which lead stages these activities occured

// If related to a contact, query to find out the Contact ID, Lead Source, and Lead Source Detail

// Create collection of tasks that are related to an contact (where the contact is listed only once)
    Set<Id> contactIds = new Set<Id>();
    for(Task t : trigger.new){
        String wId = t.WhoId;
        if(wId!=null && wId.startsWith('003') && !contactIds.contains(t.whoId)){
            contactIds.add(t.WhoId);
        }
    }
    // Pull in contact ids and related field to populate task record
    List<contact> taskContact = [Select Id, leadsource, lead_source_detail__c from Contact where Id in :ContactIds];
    Map<Id, Contact> contactMap = new Map<Id, contact>();
    for(Contact c : taskContact){
        contactMap.put(c.Id,c);
    }
    // Update custom task field with custom opp field
    for(Task t : trigger.new)
    if(t.what.id == null){
        String wId = t.WhoId;
        if(wId!=null && wId.startswith('003')){
           Contact thisContact = contactMap.get(t.WhoId);
            if(thiscontact!=null){t.Lead_Source__c = thisContact.LeadSource;} 
            if(thiscontact!=null){t.Sales_Stage__c = 'Existing Contact';}
            if(thiscontact!=null){t.Record_Type__c = 'Contact';} 
            if(thiscontact!=null){t.lead_source_detail__c = thiscontact.Lead_source_detail__c;} 
        }
    }
}

Opportunity:

 

trigger ActivityOppStageName on Task (before insert, before update){
// Goad: Pull fields from the lead and opportunity record for tracking activities related to lists and to discern in which lead/opp stages these activities occured

// If related to an Opportunity, query to find out the Opportunity number

// Create collection of tasks that are related to an opp (where the opp is listed only once)
    Set<Id> opIds = new Set<Id>();
    for(Task t : trigger.new){
        String wId = t.WhatId;
        if(wId!=null && wId.startsWith('006') && !opIds.contains(t.WhatId)){
            opIds.add(t.WhatId);
        }
    }
    // Pull in opp ids and related field to populate task record
    List<Opportunity> taskOps = [Select Id, leadsource, lead_source_detail__c, StageName from Opportunity where Id in :opIds];
    Map<Id, Opportunity> opMap = new Map<Id, Opportunity>();
    for(Opportunity o : taskOps){
        opMap.put(o.Id,o);
    }
    // Update custom task field with custom opp field
    for(Task t : trigger.new){
        String wId = t.WhatId;
        if(wId!=null && wId.startswith('006')){
            Opportunity thisOp = opMap.get(t.WhatId);
            if(thisOp!=null){t.Sales_Stage__c = thisOp.StageName;} 
            if(thisOp!=null){t.Lead_source__c = thisOp.LeadSource;}
            if(thisOp!=null){t.record_type__c = 'Opportunity';}
            if(thisOp!=null){t.lead_source_detail__c = thisOp.Lead_source_detail__c;} 
        }
    }
}

SFDC-DMGSFDC-DMG

A follow up notes on this... it works correctly if I manually create a task. However, we use buttons for some of our tasks (i.e. we have a button to schedule demos on the opportunity page layout), and when this button is used to create the task, that is when it is when the triggers are not working in the order I would like to (i.e. it is pulling the Contact record info vs. the opportunity record info).

 

Thanks!