You need to sign in to do that
Don't have an account?
Swapnil 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
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
Following thread has sample code for understanding which you can tweak as per your requirement:https://developer.salesforce.com/forums/ForumsMain?id=906F000000090eOIAQ
Thank you for the resource, it does works however for some reasons it's adding contact twice as a contact role..
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