3

I have written a after insert trigger on Opportunity to create a Opportunity Contact Role.

Here is the part of code

 if( trigger.isAfter && trigger.isinsert){
Set<Id> accIds = new Set<Id>();
List<OpportunityContactRole> contactRolesToInsert = new List<OpportunityContactRole>();
List<OpportunityContactRole> contactRolesToUpdate = new List<OpportunityContactRole>();
for(Opportunity o: Trigger.New)
{
    accIds.add(o.AccountId);
}

List<Account> accList = [Select Id, (Select Id, From_A2Z__c from Contacts Order By LastActivityDate desc) 
                         from Account where Id in: accIds ];
Map<Id,Id> mapOfAccIdNConId = new Map<Id,Id>();


for(Account a: accList)
{ 
    Id conId=null;
    if(a.contacts.size()>0)
    {
        //Get contact of recent LastActivityDate
        conId = a.contacts.get(0).Id;
        //Get contact where From A2Z TRUE and overwrite above contact id if found
        for(Contact c: a.contacts)
        {
            if(c.From_A2Z__c==true)
            {
                conId=c.Id;
                break;
            }
        }
    }
    mapOfAccIdNConId.put(a.Id,conId);
}

for(Opportunity o:trigger.new)
{
    if(mapOfAccIdNConId.containsKey(o.AccountId) && mapOfAccIdNConId.get(o.AccountId)!=null)
    {
        Id conId=mapOfAccIdNConId.get(o.AccountId);            
        //Insert Contact Role
        OpportunityContactRole OppContactRole = new OpportunityContactRole();
        OppContactRole.ContactId=conId;
        OppContactRole.Role='Decision Maker';
        OppContactRole.OpportunityId=o.id;
        contactRolesToInsert.add(OppContactRole);
    }
}

if(contactRolesToInsert.size()>0)
    insert contactRolesToInsert; }

Whenever a new Opportunity is created by converting a lead or from Contact detail page, two Opportunity Contact Roles are inserted for the same Opportunity. One Contact Role has "Role" set to null and other one with "Role" I have assigned in trigger.

What approach I should follow to handle these scenarios which causes duplicate Oportunity Contact Roles?

4
  • 1
    I'm not seeing anything here that could be creating an OpportunityContactRole with a null Role field. Do you have a trigger on OpportunityContactRole which could be firing? Any processes (process builder) or flow that inserts an OpportunityContactRole?
    – Derek F
    Commented Jul 26, 2016 at 13:40
  • Thanks for your comment. But as I mentioned in my question itself that, the one Oppty Contact Role having "Role" set to null is created after converting a lead or after if Opportunity is created from Contact Detail Page. I have tested this, a new Contact role is created for Opptys created by above manner. Commented Jul 27, 2016 at 13:55
  • Yes, and the mechanisms that could be responsible are another trigger, a process, or a flow. That is why I was asking you about those in the second part of my comment.
    – Derek F
    Commented Jul 27, 2016 at 15:30
  • Actually there is no other trigger, process, or a flow which is creating Oppty Contact Role. I have explored these cases and tested several times, then it is found that it is Salesforce's standard functionality to create Oppty Contact Role in above discussed cases. So, this is the reason behind Oppty Contact Role with "Role" having null value. Commented Jul 28, 2016 at 13:42

2 Answers 2

0

I think it may be happening because of this section of code which initializes the conId to null and also has a break inside the if statement in the middle of the for loop. The conId will be null if not set to null anyway. I suggest you remove both the break; and leave the initialization as simply Id conId;. You may need to add a null check before adding values to your map at the end of the 1st major loop.

for(Account a: accList)
{ 
    Id conId=null;  <<== Remove the initalizion to null.
    if(a.contacts.size()>0)
    {
        //Get contact of recent LastActivityDate
        conId = a.contacts.get(0).Id;
        //Get contact where From A2Z TRUE and overwrite above contact id if found
        for(Contact c: a.contacts)
        {
            if(c.From_A2Z__c==true)
            {
                conId=c.Id;
                break;  <<== remove this break
            }
        }
    }
    // regardless, may need to add if(conId != null){
    mapOfAccIdNConId.put(a.Id,conId);
    // }
}

With that having been said, you may have other workflow in your org that's creating the OCR for you automatically and your trigger may not be needed at all. Since you've also not included the remainder of your code that might tell us what else could be contributing to your issues, there's nothing more I can tell you.

0

Ok! I have got the solution for it. What I am doing is checking for existing Oppty Contact Roles created after SF standard functionality (i.e. after Converting a lead or in case of Opportunity created from Contact Detail page).

If there is an existing Oppty Contact Role for particular Opportunity, then I am just updating it's "Role" from null value to "Decision maker" and new Opportunity Contact Role is not inserted for the same Opportunity. Here is part of my code that successfully works for me:

  if(trigger.isinsert)
        {
            Set<Id> accIds = new Set<Id>();
            List<OpportunityContactRole> contactRolesToInsert = new List<OpportunityContactRole>();
            List<OpportunityContactRole> contactRolesToUpdate = new List<OpportunityContactRole>();
            for(Opportunity o: Trigger.New)
            {
                accIds.add(o.AccountId);
            }

            List<Account> accList = [Select Id, (Select Id, From_A2Z__c from Contacts Order By LastActivityDate desc) 
                                     from Account where Id in: accIds ];
            Map<Id,Id> mapOfAccIdNConId = new Map<Id,Id>();


            for(Account a: accList)
            { 
                Id conId=null;
                if(a.contacts.size()>0)
                {
                    //Get contact of recent LastActivityDate
                    conId = a.contacts.get(0).Id;
                    //Get contact where From A2Z TRUE and overwrite above contact id if found
                    for(Contact c: a.contacts)
                    {
                        if(c.From_A2Z__c==true)
                        {
                            conId=c.Id;
                            break;
                        }
                    }
                }
                mapOfAccIdNConId.put(a.Id,conId);
            }

            List<Id> opportunityIdList = new List<Id>();                

            for(Opportunity o:trigger.new)
            {
                  opportunityIdList.add(o.id);
            }

            //Get Oppty Contact Roles for Opportunities created by converting leads or created 
            //from Contact Detail page 

            List<OpportunityContactRole> opptyContactRoleList = [Select Id, OpportunityId, contactId from OpportunityContactRole
                                                                where OpportunityId in: opportunityIdList ];
            system.debug('###opptyContactRoleList'+opptyContactRoleList);

            Map<String, OpportunityContactRole> opptysNContactRoleMap = new Map<String, OpportunityContactRole>();

            for(OpportunityContactRole cr : opptyContactRoleList)
            {           
                opptysNContactRoleMap.put(String.valueOf(cr.OpportunityId),cr); 
            }
            System.debug('###opptysNContactRoleMap: '+opptysNContactRoleMap);

            //Create or update Oppty Contact Roles
            for( Opportunity o : Trigger.new)
            {
                if(opptysNContactRoleMap.containsKey(String.valueof(o.Id)))
                {
                    //Contact Role present
                    OpportunityContactRole cr = opptysNContactRoleMap.get(String.valueof(o.Id));
                    cr.Role = 'Decision Maker';
                    contactRolesToUpdate.add(cr);  // Add Contact Role to be updated                     
                }else
                {
                    if(mapOfAccIdNConId.containsKey(o.AccountId) && mapOfAccIdNConId.get(o.AccountId)!=null)
                    {
                        System.debug('###Insert New Contact Role');
                        Id conId=mapOfAccIdNConId.get(o.AccountId);            
                        //Insert Contact Role
                        OpportunityContactRole OppContactRole = new OpportunityContactRole();
                        OppContactRole.ContactId=conId;
                        OppContactRole.Role='Decision Maker';
                        OppContactRole.OpportunityId=o.id;
                        contactRolesToInsert.add(OppContactRole); // Add Contact Role to be inserted
                    }
                }
            }
            System.debug('###contactRolesToUpdate: '+contactRolesToUpdate);
            System.debug('###contactRolesToInsert: '+contactRolesToInsert);           

            if(contactRolesToUpdate.size()>0)
                update contactRolesToUpdate;
            if(contactRolesToInsert.size()>0)
                insert contactRolesToInsert;
        }

    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .