• Jessica Carstens
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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'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.