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 

Write contact information to a contact

Hi All,

I am trying to write some opportunity information from the opportunity to a contact that is listed as a contact role on an opportunity. I found this trigger and was able to update it to do most of what I need it to do. Currently, I am able to sum three opportunity amount fields to custom fields on the contact. 

What I need help with is being able to write the first opportunity close date which would be the furthest opportunity date in the past and then the last opportunity closed date. The second thing is being able to count the number of opportunities that the contact is on. And the last thing is bullying the trigger.

I am posting my current code below and any help would be greatly appreciated. 
 
trigger ContactRoleRollup on Opportunity (after insert, after update)
{

     Integer i = 0;

          // List the associated opportunities ID
          String intTest = Trigger.new[i].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 = :intTest 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 From Opportunity Where Opportunity.Id IN (Select OpportunityContactRole.OpportunityId from OpportunityContactRole where OpportunityContactRole.ContactId = :test2.ContactId)];
 
Double totalReferrals = 0;
Double netReferrals = 0;
Double totalReturns = 0;

// 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 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;

//Do all this when the trigger is initiated.

update test3;
 

}

Thanks in advance for your help,
Ed 
edward scott 10edward scott 10
I was able to figure out how to get the first close date field figured out but I am not sure how to return the last value in a list. The updated code is posted below.
 
trigger ContactRoleRollup on Opportunity (after insert, after update)
{

     Integer i = 0;

          // List the associated opportunities ID
          String intTest = Trigger.new[i].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 = :intTest 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;

// 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;

//Do all this when the trigger is initiated.

update test3;

}

 
edward scott 10edward scott 10
Okay last update I was able to get the last opportunity date as well as the count of opportunities associated to a contact. The last two things I am hoping someone can help me with is making sure the code is bulkified and to only run the trigger when there is a contact on the opportunity with the contact role of referring therapist. 

Thanks so much for your help,
Ed
edward scott 10edward scott 10
Forgot to post the updated code
 
trigger ContactRoleRollup on Opportunity (after insert, after update)
{

     Integer i = 0;

          // List the associated opportunities ID
          String intTest = Trigger.new[i].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 = :intTest 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;

//Do all this when the trigger is initiated.
if(opp.size() > 0){
update test3;
}
}