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
Forrest MulduneForrest Muldune 

Apex Trigger on Opportunitites

All,

I would appreciate some help on an Apex Trigger I am trying to create on Opportunities. I am kind of new in Apex trigger coding so I would really appreciate your help.

Below are objects with the following fields

Settlement__c  (custom Object)
  • Recovery_Total__c  - Formula (Currency) field with formula -  Initial_Value__c - Book_Value__c 
Matter__c (custom Object)
  • Total_Fees__c - Currency(16, 2) field 
Opportunities
  • Settlement_Recovery_Total__c - Currency(16, 2) field
  • Engagement_Total_Fees__c - Currency(16, 2) field 
There is a lookup relationship from Settlement__c  to Opportunities and another lookup relationship from Matter__c  to Opportunities.

What I was trying to figure out is how to sum the total records from the Total_Fees__c  field in Matter__c into the Settlement_Recovery_Total__c in Opportunities . Also sum the total records from the Recovery_Total__c  in Settlement__c  to the Settlement_Recovery_Total__c in Opportunities.

These total sums are for related records only between the objects.

Once this trigger is created, I will have a better understanding of creating other cross object triggers in the future.

I would really appreciate your help.
KevinPKevinP
Forrest,

I'm all for helping you learn to write triggers, but i think it'd be best if I didn't actually write the code for you. So let me ask you some guided questions and we'll see if we can get this written today? Sound good? Great.

Fist off, let me thank you for providing the detail you ahve in your original post. Thats fantastic. 

Now for the questions:
  1. What object(s) should this trigger fire on? ie: if someone adds or updates a matter record, should this trigger recalculate the opportunities value? or should this fire just when an Opportunity is updated or created?
  2. Should this trigger fire on creation? update? deletion? all of the above?
  3. Should this trigger run *after* the fact or *before* ?
We'll use your answers to those questions to write the trigger context string, an example of which looks like this:
Trigger FooTrigger on Matter__c (after insert, after update, after delete){

}

Note: Items in bold are from your answers, and "FooTrigger" is just a token name.
Forrest MulduneForrest Muldune
Kevin,

When someone updates a record in the Recovery_Total__c field in the Settlement__c  object and the Total_Fees__c field in Matter__c, the Settlement_Recovery_Total__c and Engagement_Total_Fees__c on Opportunities will recalculate with the correct total sum . 

Also, the trigger should fire on all of the above (creation, update, and deletion) . 

Question , will it be best to place this trigger  in Opportunities or the other objects? 
Himanshu ParasharHimanshu Parashar
Hi Forrest,

I would agree with Kevin so I am giving you pseudo code instead of writing the actual code.

1. As you have mentioned that you need that on Opportunity update then you can Wrtite trigger on Opportunity before update event
2. As you need that in Trigger then you will have to consider bulk handling so you will get data in maps for both objects. this is the first one.
3. Create a new Map with Opportunity id as key and Total as value. you can use following query to get the data.
 
for(Matter__c Matter : SELECT sum(Total_Fees__c) Total,Opportunity__c FROM Matter__c group by Opportunity__c])
{
//Generate your map with Opportunityid as key and Total as value.
}

3. Create Map for Settlement__c object with Opportunity ID as key and Recovery_Total as value
4. Loop on Trigger.new Object and get data from obove map using MapName.get(Opportunity.id)


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant
Forrest MulduneForrest Muldune
Himanshu,

Do I add this trigger code on Opportunities? what object will I place this code in?
Himanshu ParasharHimanshu Parashar
Forrest,

as you have said in your problem statement tha you need trigger on Opportunity so you will write this on Opportunity but if you are not sure where you need that trigger. Try to find answers of all the questions which Kevin has posted.
Forrest MulduneForrest Muldune
I made the updates below in Opportunities but I received the error message below.

User-added image

Am I writing this trigger correctly? 
Himanshu ParasharHimanshu Parashar
for this error please update first line with following line
 
trigger updatematterandsettlementtotals on opportunity (before update)

but to understand how trigger work please go through this basic details about trigger

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_trigger.htm
Forrest MulduneForrest Muldune
Did I create step 3 (Create a new Map with Opportunity id as key and Total as value. you can use following query to get the data.) correctly?
Himanshu ParasharHimanshu Parashar
yes you need to create that.
Forrest MulduneForrest Muldune
this is what I have below , but I am not sure if it is correct.

User-added image
Himanshu ParasharHimanshu Parashar
Try to learn with this code
trigger updatematterandsettlementtotals on opportunity (before update){
//This will hold your Matter object data
Map<String,Decimal> Mattermap = new Map<String,Decimal>();

for(AggregateResult ag : [SELECT sum(Total_Fees__c) Total,Opportunity__c FROM Matter_c__c group by Opportunity__c])
{
 Mattermap.put(String.valueof(ag.get('Opportunity__c')),Decimal.valueof(String.valueof(ag.get('Total'))));
}
//create another map with data of second object.



//Loop through trigger.new data
for(Opportunity Opp : Trigger.new)
{


{


}

replace same code


 
Himanshu ParasharHimanshu Parashar
trigger updatematterandsettlementtotals on opportunity (before update){
//This will hold your Matter object data
Map<String,Decimal> Mattermap = new Map<String,Decimal>();


for(AggregateResult ag : [SELECT sum(Total_Fees__c) Total,Opportunity__c FROM Matter_c__c group by Opportunity__c])
{
 //Put data in map for future use with opportunity id as key and value
 Mattermap.put(String.valueof(ag.get('Opportunity__c')),Decimal.valueof(String.valueof(ag.get('Total'))));
}
//create another map with data of second object.



//Loop through trigger.new data
for(Opportunity Opp : Trigger.new)
{
   opp.Settlement_Recovery_Total__c = Mattermap.get(opp.id);
   //Add second field here and get value from secon map.
}


Sorry I hit enter on last post. Here is the correct one.
Forrest MulduneForrest Muldune
Himanshu,

Please view updated trigger below.


User-added image

I changed line 6 from Matter_c__c to Matter__c

I also changed Settlement_Recovery_Total__c to Engagement_Total_Fees__c because the aggregetated value in the records in the Total_Fees__c field, will go into the Engagement_Total_Fees__c in Opportunities. Am I correct to make this change?

I also received an "Error: Compile Error: Invalid field: 'Opportunity__c' at line 6 column 26" warning.

are my updates correct?

I will be waiting for your reply.




Himanshu ParasharHimanshu Parashar
1. replace line 6 Opportunity__c field with your own field api name on Matter__c object.
2. Put line 20 out of line 17 for loop.

 
Forrest MulduneForrest Muldune
For number 1, I did not understand clearly, what field will I insert instead of Opportunity__c?

FYI - The Total_Fees__c field is in the Matter__c object and the Engagement_Total_Fees__c is in Opportunity__c.

Number 2, is it ok if I place line 20 below line 18? 

 
Himanshu ParasharHimanshu Parashar
Forrest,

You are getting error on line 6 which means it required api name of Opportunity. go to detail object page of your Matter__c object and check what you have given as api name for Opportunity lookup or post your screenshot of Matter__c object here.
Forrest MulduneForrest Muldune
Himanshu,

view below

User-added image
Himanshu ParasharHimanshu Parashar
put Deal__c intead of Opportunity__c at line 6.

get the same for Settlement object as well.
Forrest MulduneForrest Muldune
Himanshu,

I had received the error message below

Error: Compile Error: expecting right curly bracket, found '<EOF>' at line 26 column 0

I noticed I did not have any right curly bracket on line 26 at all. sorry to bother you so much but I tried researching for the issue but I could not the solution.
Forrest MulduneForrest Muldune
Himanshu,

I did some modifications below to the Trigger you created, however it is still not working. Do you know why this is still not working?



trigger updatematterandsettlementtotals on opportunity (before update){
//This will hold your Matter object data
Map<String,Decimal> Mattermap = new Map<String,Decimal>();
 
 
for(AggregateResult ag : [SELECT sum(Total_Fees__c) Total,Deal__c FROM Matter__c group by Deal__c])
{
 //Put data in map for future use with opportunity id as key and value
 Mattermap.put(String.valueof(ag.get('Opportunity__c')),Decimal.valueof(String.valueof(ag.get('Total'))));
}
 
//create another map with data from the Settlement object.
//This will hold my Settlement object data.
Map<String,Decimal> Settlementmap = new Map<String,Decimal>();
 
 
for(AggregateResult ag : [SELECT sum(Recovery_Total__c) Total,Deal__c FROM Settlement__c group by Deal__c])
{
 
//Loop through trigger.new data
for(Opportunity Opp : Trigger.new)
{
   opp.Engagement_Total_Fees__c = Mattermap.get(opp.id);
   //Add second field here and get value from secon map.
   opp.Settlement_Recovery_Total__c = Settlementmap.get(opp.id);
}
}
}

Do you know why this is still not working?
 
Forrest MulduneForrest Muldune
Kevin,

Do you have any suggestions for my trigger request? I was trying to modify the trigger however I had no luck.