You need to sign in to do that
Don't have an account?
edward 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.
Thanks in advance for your help,
Ed
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
Thanks so much for your help,
Ed