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
Nagma KhanNagma Khan 

Calculate the Total Opportunity Amount for the Associated Contact Record

hi
i am new please tell me
how  to  create sum of the all opportunity amount which is associated with the Contact record on the contact object 



Thanks
Nagma Khan
Best Answer chosen by Nagma Khan
NagendraNagendra (Salesforce Developers) 
Hi Nagma,

Please find the working code for the above requirement.
trigger TestBed on Opportunity (after insert, after update)
{
Integer i = 0;
 
// Grab this 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 FROM OpportunityContactRole WHERE OpportunityId = :intTest];
//Step 2. Create a list of opportunities who are children of this contact record.
  
 
List<Opportunity> opp = [Select Opportunity.Id, Opportunity.Amount From Opportunity Where Opportunity.Id IN (Select OpportunityContactRole.OpportunityId from OpportunityContactRole where OpportunityContactRole.ContactId = :test2.ContactId)];
 
Double j = 0;
 
// Loop through the filtered opportunites and sum up their amounts.
 for(Opportunity op : opp)
{
If (op.Amount != Null)
{
j += op.Amount;
}
}
//Place the summed total in a custom field on the associated contact record.
 
Contact test3 = [SELECT Id, c_Opportunity_Total__c FROM Contact WHERE Id = :test2.ContactId];
test3.c_Opportunity_Total__c = j;
//Do all this when the trigger is initiated.
update test3;
 
}
Hope this helps.

Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering similar issue.

Best Regards,
Nagendra.

 

All Answers

Prithviraj_ChavanPrithviraj_Chavan
Hi Nagma,
There is no direct relationship between contact and opportunity so you need to write trigger for that and update field on contact.
please follow the steps :-
1) created field sumOfOpp__c : type number on contact
2) Create Trigger on opportunity;
3)
List​<Contact> lstContact = new List<Contact>();
AggregateResult[] groupedResults = [SELECT Id, Sum(Amount) FROM Opportunity GROUP BY ContactId ];
for (AggregateResult ar : groupedResults)  {
    Contact cont = new Contact();
    cont.Id = Id of your contact;
    cont.sumOfOpp__c = ar.get('expr0');
    lstContact.add(cont);
}
Database.update(lstContact);
NagendraNagendra (Salesforce Developers) 
Hi Nagma,

Please find the working code for the above requirement.
trigger TestBed on Opportunity (after insert, after update)
{
Integer i = 0;
 
// Grab this 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 FROM OpportunityContactRole WHERE OpportunityId = :intTest];
//Step 2. Create a list of opportunities who are children of this contact record.
  
 
List<Opportunity> opp = [Select Opportunity.Id, Opportunity.Amount From Opportunity Where Opportunity.Id IN (Select OpportunityContactRole.OpportunityId from OpportunityContactRole where OpportunityContactRole.ContactId = :test2.ContactId)];
 
Double j = 0;
 
// Loop through the filtered opportunites and sum up their amounts.
 for(Opportunity op : opp)
{
If (op.Amount != Null)
{
j += op.Amount;
}
}
//Place the summed total in a custom field on the associated contact record.
 
Contact test3 = [SELECT Id, c_Opportunity_Total__c FROM Contact WHERE Id = :test2.ContactId];
test3.c_Opportunity_Total__c = j;
//Do all this when the trigger is initiated.
update test3;
 
}
Hope this helps.

Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering similar issue.

Best Regards,
Nagendra.

 
This was selected as the best answer
Nagma KhanNagma Khan
Thanks 
Nagendra