You need to sign in to do that
Don't have an account?
Forrest 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)
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.
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
- Total_Fees__c - Currency(16, 2) field
- Settlement_Recovery_Total__c - Currency(16, 2) field
- Engagement_Total_Fees__c - Currency(16, 2) field
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.
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:
- 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?
- Should this trigger fire on creation? update? deletion? all of the above?
- 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:Note: Items in bold are from your answers, and "FooTrigger" is just a token name.
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?
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.
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
Do I add this trigger code on Opportunities? what object will I place this code in?
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.
Am I writing this trigger correctly?
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
replace same code
Please view updated trigger below.
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.
2. Put line 20 out of line 17 for loop.
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?
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.
view below
get the same for Settlement object as well.
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.
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?
Do you have any suggestions for my trigger request? I was trying to modify the trigger however I had no luck.