0

I want to update Custom Field(Checkbox) on Contact whenever a new Opp is created and Opp has Primary Contact from Contact

trigger PrimaryConOnAccOfOppHandler on Opportunity (after insert, after update, after delete) 
{
    if(Trigger.isInsert)
    {
        PrimaryConOnAccOfOppHandler.insertHandler(Trigger.New);
    }
    if(Trigger.isDelete)
    {
        PrimaryConOnAccOfOppHandler.deleteHandler(Trigger.Old);
    }
    if(Trigger.isUpdate)
    {
        PrimaryConOnAccOfOppHandler.updateHandler(Trigger.New, Trigger.oldMap);
    }
}
public class PrimaryConOnAccOfOppHandler 
{
    //New Opportunity is Created
    public static void insertHandler(List<Opportunity> newOppList)
    {
        Set<Id> conIds = new Set<Id>();
        for(Opportunity opp : newOppList){
            conIds.add(opp.Primary_Contact__c);
        }
        List<Contact> conList = new List<Contact>();
        for(Contact a : [SELECT Id, Primary_Contact_Checkbox__c FROM Contact WHERE Id IN :conIds]){
                    a.Primary_Contact_Checkbox__c=true;
                    conList.add(a);
        }
        Update conList;
    }
    //Old Opportunity is Deleted
    public static void deleteHandler(List<Opportunity> oldOppList)
    {
        Set<Id> conIds = new Set<Id>();
        for(Opportunity opp : oldOppList){
            conIds.add(opp.Primary_Contact__c);
        }
        List<Contact> conList = new List<Contact>();
        List<Opportunity> opptList = [SELECT Id, Primary_Contact__c FROM Opportunity WHERE ContactId IN :conIds];
        if(!opptList.isEmpty())
        {
            for(Contact a : [SELECT Id, Primary_Contact_Checkbox__c FROM Contact WHERE Id IN :conIds]){
                a.Primary_Contact_Checkbox__c=True;
                conList.add(a);
            }
        }
        else
        {
            for(Contact a : [SELECT Id, Primary_Contact_Checkbox__c FROM Contact WHERE Id IN :conIds])
            {
                a.Primary_Contact_Checkbox__c=False;
                conList.add(a);
            }
        }
        Update conList;
    }
    //Old Opportunity is Updated
    public static void updateHandler(List<Opportunity> oppList, Map<Id, Opportunity> oldOppList)
    {
        Set<Id> conIds = new Set<Id>();
        for(Opportunity opp : oppList){
            if(opp.Primary_Contact__c != oldOppList.get(opp.Id).Primary_Contact__c )
            {
                conIds.add(opp.Primary_Contact__c);
            }   
        }
        List<Contact> conList = new List<Contact>();
        List<Opportunity> opptList = [SELECT Id, Primary_Contact__c FROM Opportunity WHERE ContactId IN :conIds];
        if(!opptList.isEmpty())
        {
            for(Contact a : [SELECT Id, Primary_Contact_Checkbox__c FROM Contact WHERE Id IN :conIds]){
                a.Primary_Contact_Checkbox__c=True;
                conList.add(a);
            }
        }
        else{
            for(Contact a : [SELECT Id, Primary_Contact_Checkbox__c FROM Contact WHERE Id IN :conIds]){
                a.Primary_Contact_Checkbox__c=False;
                conList.add(a);
            }
        }
        Update conList;
    }

}

Now Insert and Delete is working Fine but Problem lies in Update Section..I don't want this to perform Bulk Operations.

I want to perform same operation as written in code but i think my Condition is Wrong.. Can someone guide me here?

  • How is Primary_Contact__c populated? It sounds like it's just not populated at the time of the after insert trigger. – David Reed Jul 24 at 12:49
  • calling 'insertHandler' method from which DML context ? after insert or 'before insert'? – sdandamud1 Jul 24 at 13:04
  • after Insert and I've made some changes in the Code – SFDC_M Jul 24 at 13:10
  • We can't confirm whether your code is correct, Manish. I'd suggest writing unit tests to validate its behavior; that and hands-on testing are the best safeguard. – David Reed Jul 24 at 13:16
  • Can you Please check Update Conditions where I am going wrong – SFDC_M Jul 24 at 14:35
0

Do you have to use triggers?

This functionality is available via process builder and it takes round 5 min to build a process that will cover your requirement.

If it is a bit more complex you may use even flow which is triggered from process. And this covers a lot of use cases.

  • 1
    I Know but my task is to do this using Trigger – SFDC_M Jul 25 at 10:39

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.