You need to sign in to do that
Don't have an account?
Jessica Carstens
Use an Apex Trigger to add Primary Contact First Name and Email to Opportunity Page
I'm trying to send emails to clients that are triggered by changes to the Opportunity. I've created two new fields on the Opportunity to hold the client's name and email address.
I've writted a trigger which works great expcept when you go to convert a lead into a contact with a new Opportunity.
trigger UpdateClientEmail on Opportunity (after update) {
for (Opportunity o : Trigger.new) {
if (o.StageName <> 'Closed/Lost') {
OpportunityContactRole ContactRole =
[SELECT ContactID from OpportunityContactRole where IsPrimary = True and OpportunityId = :o.id];
Contact c =
[SELECT FirstName, Email FROM Contact WHERE ID = :ContactRole.ContactID];
o.Client_Name__c = c.FirstName;
o.Client_Email__c = c.Email;
}
}
}
When I go to convert a new lead I get this error:
Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateClientEmail: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject Trigger.UpdateClientEmail: line 5, column 1: [] Class.leadconvert.BulkLeadConvert.handleOpportunityInserts: line 730, column 1 Class.leadconvert.BulkLeadConvert.convertLead: line 104, column 1
I'm not sure how to change my trigger so that it only applies to opportunities that have already been created. I've got workflows that get the fields updated correctly when a lead is created and converted. The only time I need the trigger is when the Primary Contact is changed from one contact to another or when the email address for the contact is changed.
Any help is much appreciated.
I've writted a trigger which works great expcept when you go to convert a lead into a contact with a new Opportunity.
trigger UpdateClientEmail on Opportunity (after update) {
for (Opportunity o : Trigger.new) {
if (o.StageName <> 'Closed/Lost') {
OpportunityContactRole ContactRole =
[SELECT ContactID from OpportunityContactRole where IsPrimary = True and OpportunityId = :o.id];
Contact c =
[SELECT FirstName, Email FROM Contact WHERE ID = :ContactRole.ContactID];
o.Client_Name__c = c.FirstName;
o.Client_Email__c = c.Email;
}
}
}
When I go to convert a new lead I get this error:
Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateClientEmail: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject Trigger.UpdateClientEmail: line 5, column 1: [] Class.leadconvert.BulkLeadConvert.handleOpportunityInserts: line 730, column 1 Class.leadconvert.BulkLeadConvert.convertLead: line 104, column 1
I'm not sure how to change my trigger so that it only applies to opportunities that have already been created. I've got workflows that get the fields updated correctly when a lead is created and converted. The only time I need the trigger is when the Primary Contact is changed from one contact to another or when the email address for the contact is changed.
Any help is much appreciated.
The problem I'm having now is that my test class keeps failing. This is the error I'm getting:
"System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateClientEmail: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject Trigger.UpdateClientEmail: line 5, column 1: []"
Here is my test class:
Any help is much appreciated. I'm still learning!
Thanks!
check your fields setup and validation rules for
Purchase_Time_Frame__c
Preferred_Delivery_Location__c
Client_Name__c
Client_Email__c