You need to sign in to do that
Don't have an account?
B Tulasi
I want to get total amount from child records
Hi all,
i have one field in child and parent, in child object i.e. bank_balance__C and in parent object i.e. Totalbankbalance__C. so, i need to get totalbankbalance__C from bank_balance__C .
For example,
record1------- bank_balance__C =100
record2------- bank_balance__C =100
record3------- bank_balance__C =100
my required field is totalbankbalance__C =bank_balance__C+bank_balance__C+bank_balance__C
totalbankbalance__C=300.
can any one help me.
thanks in advance
Thulasi
i have one field in child and parent, in child object i.e. bank_balance__C and in parent object i.e. Totalbankbalance__C. so, i need to get totalbankbalance__C from bank_balance__C .
For example,
record1------- bank_balance__C =100
record2------- bank_balance__C =100
record3------- bank_balance__C =100
my required field is totalbankbalance__C =bank_balance__C+bank_balance__C+bank_balance__C
totalbankbalance__C=300.
can any one help me.
thanks in advance
Thulasi
This is right code
trigger getTotalAmount on New_Leads__c(before insert) {
set<id> setId=new set<id>();
list<New_Leads__c> list1= new list<New_Leads__c>();
list<Bank__c> list2= new list<Bank__c>();
decimal count;
for(New_Leads__c nlead:trigger.new){
if(nlead.Bank_Name__c!=null){
setId.add(nlead.Bank_Name__c);
list1.add(nlead);
}
}
map<id,Bank__c> maprecord = new map<id,Bank__c>([select id, Name, TotalBankBalance__c from Bank__c where id in :setId ]);
for(Bank__c bank:maprecord.values()){
for(New_Leads__c nlead:list1){
if(bank.TotalBankBalance__c==null){
bank.TotalBankBalance__c=nlead.BankBalance__c;
list2.add(bank);
}
else
{
count=decimal.valueof(bank.TotalBankBalance__c) + decimal.valueof(nlead.BankBalance__c);
bank.TotalBankBalance__c = string.valueof(count);
list2.add(bank);
}
}
}
if(list2!=null && list2.size()>0)
update list2;
}
All Answers
It is very simple to implement using rollup summary fields on parent object.
A roll-up summary field calculates values from related records, such as those in a related list. You can create a roll-up summary field to display a value in a master record based on the values of fields in a detail record. The detail record must be related to the master through a master-detail relationship. For example, you want to display the sum of invoice amounts for all related invoice custom object records in an account’s Invoices related list. You can display this total in a custom account field called Total Invoice Amount.
Please mark this answar a best answer if it helped to you so that it can be helpful to others as well.
Thanks
Sandeep Singhal
http://www.codespokes.com/
Sorry, I didn't mentioned there, Actually my requirement is calculate sum by using triggers. I know how to use Relationships. But, i am trying with trigger
Thanks
Thulasi
You are trying to create Trigger in Parent object or Child object?
Parent Object
trigger getTotalAmount on New_Leads__c (before insert) {
set<id> setId=new set<id>();
for(New_Leads__c nlead:trigger.new){
setId.add(nlead.Bank_Name__c);
}
map<id,Bank__c> maprecord=new map<id,Bank__c>([select id, Name, TotalBankBalance__c from Bank__c where id=:setId ]);
for(New_Leads__c n:trigger.new){
if(maprecord.size()>0 && maprecord.containsKey(n.Bank_Name__c)){
if(maprecord.get(n.Bank_Name__c).TotalBankBalance__c == null || maprecord.get(n.Bank_Name__c).TotalBankBalance__c == ''){
maprecord.get(n.Bank_Name__c).TotalBankBalance__c = n.BankBalance__c;
system.debug('>>>>>allParentRecords00000' +maprecord);
}
else {
maprecord.get(n.Bank_Name__c).TotalBankBalance__c = maprecord.get(n.Bank_Name__c).TotalBankBalance__c + '+' + n.BankBalance__c;
system.debug('>>>>>maptRecords1111' +maprecord);
}
}
}
update maprecord.values();
system.debug('>>>>>maprecords' +maprecord);
}
Here, i am getting output like this 100+100+100. But i want my actual results is 300 means direct count
Try the above code. I have removed the concatination of '+';
This is right code
trigger getTotalAmount on New_Leads__c(before insert) {
set<id> setId=new set<id>();
list<New_Leads__c> list1= new list<New_Leads__c>();
list<Bank__c> list2= new list<Bank__c>();
decimal count;
for(New_Leads__c nlead:trigger.new){
if(nlead.Bank_Name__c!=null){
setId.add(nlead.Bank_Name__c);
list1.add(nlead);
}
}
map<id,Bank__c> maprecord = new map<id,Bank__c>([select id, Name, TotalBankBalance__c from Bank__c where id in :setId ]);
for(Bank__c bank:maprecord.values()){
for(New_Leads__c nlead:list1){
if(bank.TotalBankBalance__c==null){
bank.TotalBankBalance__c=nlead.BankBalance__c;
list2.add(bank);
}
else
{
count=decimal.valueof(bank.TotalBankBalance__c) + decimal.valueof(nlead.BankBalance__c);
bank.TotalBankBalance__c = string.valueof(count);
list2.add(bank);
}
}
}
if(list2!=null && list2.size()>0)
update list2;
}