function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Swapnil PatneSwapnil Patne 

​Copying contact from custom opportunity lookup field to opportunity contact roles?

Hi,

I've create a custom lookup field on Opportunity called "Contact__c"  which is used to tag the main contact on that opportunity, however the opportunity doesn't show up under contact because that contact is not a primary under contact roles.

So, how can we make the contact slected in lookup field as a primary contact under opportunity Contact Roles and assign  a default role? 

I understand it requires an apex trigger, so wondering if any one can help me with this?

Much appreciated.

Thanks,
Swapnil
SonamSonam (Salesforce Developers) 
Hey Swapnil,

Following thread has sample code for understanding which you can tweak as per your requirement:https://developer.salesforce.com/forums/ForumsMain?id=906F000000090eOIAQ
Swapnil PatneSwapnil Patne
Hey Sonam,

Thank you for the resource, it does works however for some reasons it's adding contact twice as a contact role.. 
SonamSonam (Salesforce Developers) 
Hey Swapnil, I've gone through the code but do not see this to be creating dup contact roles - have you made any changes in this code - if yes, pls share.
Swapnil PatneSwapnil Patne
Hi Sonam,

Nope I haven't made any changes, I used below code but get following error:

Also, if you clone an opportunity it creates a dup role.

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger addContactRole caused an unexpected exception, contact your administrator: addContactRole: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.addContactRole: line 13, column 1

CODE ------>>>>

trigger addContactRole on Opportunity (after Insert, after update) {

List<OpportunityContactRole> newContactRoleList=new List<OpportunityContactRole>();
List<OpportunityContactRole> oldContactRoleList=new List<OpportunityContactRole>();
Set<Id> OppId=new Set<Id>();
Set<Id> ContactId=new Set<Id>();

for(Opportunity oppObj: Trigger.new)
{
//Insert condition
if(Trigger.isInsert)
{
if(oppObj.Contact__c!=null && Trigger.oldMap.get(oppObj.Id).Contact__c==null)
{
//Creating new contact role
newContactRoleList.add(new OpportunityContactRole (ContactId=oppObj.Contact__c,OpportunityId=oppObj.Id, Role='Decision Maker', IsPrimary = true));

}
}
else
{

if(oppObj.Contact__c==null && Trigger.oldMap.get(oppObj.Id).Contact__c!=null)
{
//Getting the contact and oppty Id from old values and adding this in set
Opportunity OldoppObj=Trigger.oldMap.get(oppObj.Id);
OppId.add(OldoppObj.id);
ContactId.add(OldoppObj.Contact__c);

}
else if(oppObj.Contact__c!=null && Trigger.oldMap.get(oppObj.Id).Contact__c==null)
{
//Creating new contact role
newContactRoleList.add(new OpportunityContactRole (ContactId=oppObj.Contact__c, OpportunityId=oppObj.Id, Role='Decision Maker', IsPrimary = true));
}
}
}


try
{
//inserting new Contacts
if(newContactRoleList.size()>0) insert newContactRoleList;

//Selecting old contact roles
if (OppId.size()>0) oldContactRoleList=[Select Id from OpportunityContactRole where ContactId in : ContactId and OpportunityId in : OppId];

//Deleting old contact roles
if (oldContactRoleList.size()>0) delete oldContactRoleList;
}
catch(Exception e)
{
System.debug(e);
trigger.new[0].addError('Technical error occurred. Please contact to your system administrator or try after some time.');

}




}

Thanks,
Swapnil
SonamSonam (Salesforce Developers) 
hey Swapnil - tried the same on my test ORG - working fine for me - are you seeing this in the specific case of cloning the opportunity?
Swapnil PatneSwapnil Patne
Hey Sonam.. no it's not for specific case but for every opportunity that I clone it creates a duplicate contact role And if there is a contact role already on the opportunity then that contact appears three times. Ideally the code should also check if contact role already exists if yes- then do not create new role. I guess this piece of logic is not there..? 
SonamSonam (Salesforce Developers) 
Yes, this code is not checking for duplicates in the contact roles -