You need to sign in to do that
Don't have an account?
djensen
Add primary contact to opportunity custom field
I need to map the Primary Contact's last name to a custom text field on the opporunity record. I found the below trigger in a forumn that seems to work -although it maps the contact's full name. Although it maps the name correctly, it now creates an error when converting a lead. Error message below. I'm not a developer and new to triggers so I'm hoping someone can help me with this issue.
1) Can I map just the last name only?
2) Can the lead conversion error be fixed?
Lead Conversion Error:
Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContactRoleName: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject Trigger.ContactRoleName: line 5, column 1: [] (System Code)
Trigger:
Trigger ContactRoleName on Opportunity(before update)
{OpportunityContactRole Roles = new OpportunityContactRole();
for(Opportunity o:Trigger.new)
{
Roles = [select Id,IsPrimary,Contact.Name, Contact.Email, Contact.Phone from OpportunityContactRole where opportunity.id=:o.Id];
o.Primary_Contact_Name__c = Roles.Contact.Name;
}
}
1) Can I map just the last name only?
2) Can the lead conversion error be fixed?
Lead Conversion Error:
Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContactRoleName: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject Trigger.ContactRoleName: line 5, column 1: [] (System Code)
Trigger:
Trigger ContactRoleName on Opportunity(before update)
{OpportunityContactRole Roles = new OpportunityContactRole();
for(Opportunity o:Trigger.new)
{
Roles = [select Id,IsPrimary,Contact.Name, Contact.Email, Contact.Phone from OpportunityContactRole where opportunity.id=:o.Id];
o.Primary_Contact_Name__c = Roles.Contact.Name;
}
}
try this one
trigger ContactRoleName on Opportunity(before update) {
//Map to hold Opportunity Id as key and Primary contact's Last Name as value
Map<Id, String> mapOppIdWithConLN = new Map<Id,String>();
//Loop through contact roles
for(OpportunityContactRole oCR : [select Id,IsPrimary,Contact.Name, Contact.LastName, OpportunityId From OpportunityContactRole where opportunityId IN : Trigger.new AND isprimary = true]) {
//Populate map with values
mapOppIdWithConLN.put(oCR.OpportunityId, oCR.Contact.LastName);
}
//Loop through Opportunity
for(Opportunity opp : Trigger.new) {
//Check if map contains Opportunity
if(mapOppIdWithConLN.containsKey(opp.Id))
opp.Primary_Contact_Name__c = mapOppIdWithConLN.get(opp.Id);
}
}
It will update Primary_Contact_Name__c with Prmary Contact's LastName if any Primary contact on opportunity.
One more thing once you add Primary contact role on opportunity you need to blank update your opportunity record to fire the trigger.
Regards
Subhash
All Answers
try this one
trigger ContactRoleName on Opportunity(before update) {
//Map to hold Opportunity Id as key and Primary contact's Last Name as value
Map<Id, String> mapOppIdWithConLN = new Map<Id,String>();
//Loop through contact roles
for(OpportunityContactRole oCR : [select Id,IsPrimary,Contact.Name, Contact.LastName, OpportunityId From OpportunityContactRole where opportunityId IN : Trigger.new AND isprimary = true]) {
//Populate map with values
mapOppIdWithConLN.put(oCR.OpportunityId, oCR.Contact.LastName);
}
//Loop through Opportunity
for(Opportunity opp : Trigger.new) {
//Check if map contains Opportunity
if(mapOppIdWithConLN.containsKey(opp.Id))
opp.Primary_Contact_Name__c = mapOppIdWithConLN.get(opp.Id);
}
}
It will update Primary_Contact_Name__c with Prmary Contact's LastName if any Primary contact on opportunity.
One more thing once you add Primary contact role on opportunity you need to blank update your opportunity record to fire the trigger.
Regards
Subhash