You need to sign in to do that
Don't have an account?
Sourav P
To sum up the amounts from related list as per certain criteria
Hi
In Contract object, i have the " Payments" as the related object.
In Payments i have the field " Opp renew check" where the values can be 01,02,03 etc. Multiples Payments records can have the value as 01 or 02 or 03 etc.
Now i want that, where its all say, 01, those all record's amount will sum up and show in the Contract object, when new records comes with 02, the latest( highest number) will get sum up.
E.g if two records with 01, and values as 1. $1000
2. $500
Those two will sum up as $1500.
When new records come up with 02, say 1. $2000, 2. $ 400
It will change to $2400.
Both are relatd as Master-detail. I tried in trigger as below. But as new to apex, i think some issue with my below trigger, Can anyone plz suggest where i went wrong.
In Contract object, i have the " Payments" as the related object.
In Payments i have the field " Opp renew check" where the values can be 01,02,03 etc. Multiples Payments records can have the value as 01 or 02 or 03 etc.
Now i want that, where its all say, 01, those all record's amount will sum up and show in the Contract object, when new records comes with 02, the latest( highest number) will get sum up.
E.g if two records with 01, and values as 1. $1000
2. $500
Those two will sum up as $1500.
When new records come up with 02, say 1. $2000, 2. $ 400
It will change to $2400.
Both are relatd as Master-detail. I tried in trigger as below. But as new to apex, i think some issue with my below trigger, Can anyone plz suggest where i went wrong.
trigger paymentTrigger on Payments__c(After insert,after delete){ List<Contract> conListToUpdate=new List<Contract>(); Set<Id> conIdSet= new Set<Id>(); if(Trigger.isAfter){ if(Trigger.isInsert){ for(Payments__c pay : trigger.new){ if(pay.Opp_Renewal_Check__c!= null){ conIdSet.add(pay.Paid_to_date_2__c); } } } if(Trigger.isDelete){ for(Payments__c pay : trigger.old){ if(pay.Opp_Renewal_Check__c!= null){ conIdSet.add(pay.Paid_to_date_2__c); } } } } if(!conIdSet.isEmpty()){ List<AggregateResult> aggRes=[SELECT SUM(Id)adding ,Opp_Renewal_Check__c chk FROM Payments__c WHERE Opp_Renewal_Check__c IN : conIdSet AND MAX(Integer.valueOf(Opp_Renewal_Check__c)) Group By Opp_Renewal_Check__c]; for(AggregateResult agg : aggRes){ Contract con =new Contract(id=(Id)agg.get('chk'),Paid_to_date_2__c = (Decimal)agg.get('adding')); conListToUpdate.add(con); } if(!conListToUpdate.isEmpty()){ try{ update conListToUpdate; }catch(DmlException de ){ System.debug(de.getMessage()); } } } } Replace Task_Count__c with your Opportunity API field API
(Just change the obejcts to your obejcts and logic is there)
Parent Object: Contact
Fields: Gorss_Amount_Total__c
Child Object : Credit__c
Fields: Value__c, Gross_Amount__c
Requirement: Sumup only child records with highest-value in Value__c number field into the parent.
(e.g. if Contact name is John Doe and he has 4 credit scores such as
Credit record -1 With Value__c = 1, Gross_Amount__c = 10,
Credit record -2 With Value__c = 2, Gross_Amount__c = 5,
Credit record -3 With Value__c = 3, Gross_Amount__c = 25,
Credit record -4 With Value__c = 3, Gross_Amount__c = 75
At this point only two credit records needs to be summed up which are record -3 and record - 4 and Gorss_Amount_Total__c should be = 100.
Trigger Code:
Hope this helps!
All Answers
I think we can acheieve this using roll up summary field.Below are the steps that you can follow to acheive you requirement.
1. Goto to the parent object (Master)
2. Create a Roll up Summary Field
3. In that we have an option to add condition (See my Screenshot below)
4. Create three fields as per your requirement. (for 01, 02 and 03)
Mark it as answer if it solves your query.
Thanks
Manoj
I think i Misunderstood the requirement. Let me try to correct your trigger.
Thanks
Manoj S
You can have a multiple Payment records for one contract.
Every Payment record is going to have two fields in it. 1) Value, and 2) Opp Renew Check
This field named value can be 1000, or 2000 etc...
and Opp Renew Check has to be 01, or 02, or 03 etc.. correct ? I mean towards smaller to a larger number.
Now, let's say if you have 8 child records of Payment for ONE parent Contract
such as
Contract Name = My Contract
Payment record - 1 = Opp Renew Check = 01, Value = 5000
Payment record - 2 = Opp Renew Check = 01, Value = 2000
Payment record - 3 = Opp Renew Check = 02, Value = 3000
Payment record - 4 = Opp Renew Check = 02, Value = 10
Payment record - 5 = Opp Renew Check = 02, Value = 20
Payment record - 6 = Opp Renew Check = 02, Value = 50
Payment record - 7 = Opp Renew Check = 03, Value = 900
Payment record - 8 = Opp Renew Check = 03, Value = 900
So at this moment / stage, then Sum__c on Contract would be = 1800 correct?
(Just change the obejcts to your obejcts and logic is there)
Parent Object: Contact
Fields: Gorss_Amount_Total__c
Child Object : Credit__c
Fields: Value__c, Gross_Amount__c
Requirement: Sumup only child records with highest-value in Value__c number field into the parent.
(e.g. if Contact name is John Doe and he has 4 credit scores such as
Credit record -1 With Value__c = 1, Gross_Amount__c = 10,
Credit record -2 With Value__c = 2, Gross_Amount__c = 5,
Credit record -3 With Value__c = 3, Gross_Amount__c = 25,
Credit record -4 With Value__c = 3, Gross_Amount__c = 75
At this point only two credit records needs to be summed up which are record -3 and record - 4 and Gorss_Amount_Total__c should be = 100.
Trigger Code:
Hope this helps!