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
edward scott 10edward scott 10 

How to Update A Contact From An Opportunity

Hi All,
 
I am trying to rollup opportunity amounts to a contact record if they are attached to an opportunity as a contactrole with the role of 'referring therapist'. I have gotten pretty far with the code. Currently when I edit an opportunity everything work and the fields that I created on the contact get updated. The main problem I am having now is this trigger fires even when there isn't a contact role attached to the opportunity and when that happens I recieve an error message that says:

ContactRoleRollup: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject Trigger.ContactRoleRollup: line 16, column 1

I know this is because it is trying to run on every opportunity but what I dont know is how to fix it. 

Any help would be greatly appreciated. I am posting the code below. Thanks in advance for your help.
 
trigger ContactRoleRollup on Opportunity (after insert, after update)
{

    List<Contact> cwOpsToUpdate = new List<Contact>();
     List<Id>ocrIds = new List<Id>();
   {

     
     for(Opportunity ocr: Trigger.New) {
         ocrIds.add(ocr.Id);
         }



 //Must use the colon that specifies the bind variable. This query pulls one row from the Opportunity contact role table. I will use the contact id from this record.
         OpportunityContactRole test2 = [SELECT Id, OpportunityId, ContactId, Role FROM OpportunityContactRole WHERE OpportunityId = :ocrIds AND Role = 'Referring Therapist'];
         

//Create a list of opportunities who are children of this contact record.

List<Opportunity> opp = [Select Opportunity.Id, Opportunity.Amount, Opportunity.Net_Amount__c, Opportunity.Amount_Returned__c, 
Opportunity.CloseDate From Opportunity Where Opportunity.Id IN (Select OpportunityContactRole.OpportunityId from OpportunityContactRole where 
OpportunityContactRole.ContactId = :test2.ContactId) ORDER BY Opportunity.CloseDate ASC];
 
Double totalReferrals = 0;
Double netReferrals = 0;
Double totalReturns = 0;
Date minCloseDate = opp[0].CloseDate;
Integer oppListSize = opp.size();


List<Opportunity> oppDSC = [Select Opportunity.Id, Opportunity.Amount, Opportunity.Net_Amount__c, Opportunity.Amount_Returned__c, 
Opportunity.CloseDate From Opportunity Where Opportunity.Id IN (Select OpportunityContactRole.OpportunityId from OpportunityContactRole where 
OpportunityContactRole.ContactId = :test2.ContactId) ORDER BY Opportunity.CloseDate DESC];

Date maxCloseDate = oppDSC[0].CloseDate;

// Loop through the filtered opportunites and sum up their amounts.

    for(Opportunity op : opp)
    {
     If (op.Amount != Null)
     {
      totalReferrals += op.Amount;
      netReferrals += op.Net_Amount__c;
      totalReturns += op.Amount_Returned__c;
     }
    }

//Place the summed total in a custom field on the associated contact record.

Contact test3 = [SELECT Id, Total_Amount_Of_Referrals__c, Total_Amount_Of_Returns__c, Net_Amount__c, First_Referral_Date__c FROM Contact WHERE Id = :test2.ContactId];
test3.Total_Amount_Of_Referrals__c = totalReferrals;
test3.Total_Amount_Of_Returns__c = totalReturns;
test3.Net_Amount__c = netReferrals;
test3.First_Referral_Date__c = minCloseDate;
test3.Last_Referral_Date__c = maxCloseDate;
test3.Referral_Opportunity_Count__c = oppListSize;
cwOpsToUpdate.add(test3);

}

if(cwOpsToUpdate.size()>0){
update cwOpsToUpdate;
}
}