You need to sign in to do that
Don't have an account?
sonam guptha
apex trigger show total sum as wrong value on updating the previous records
Hi,
Here iam adding sum of revenue__c to account column acc.Finance__c.
for Example :--
if opportunity.member__c.Revenue__c is 10
& opportunity.member__c.Revenue__c is 20
total = 30
this value iam printing on acc.Finance__c column,but now the problem is its keep on adding the values when we are changing the value on same record.
for example :--
for the same above record the value is 30.
if i change the same opportunity.member__c.Revenue__c from 10 to 20
& opportunity.member__c.Revenue__c is 20
the value should be 40 but here it counting has 50.
its counting the old value even.
can any one help me to resolve it??
Here iam adding sum of revenue__c to account column acc.Finance__c.
for Example :--
if opportunity.member__c.Revenue__c is 10
& opportunity.member__c.Revenue__c is 20
total = 30
this value iam printing on acc.Finance__c column,but now the problem is its keep on adding the values when we are changing the value on same record.
for example :--
for the same above record the value is 30.
if i change the same opportunity.member__c.Revenue__c from 10 to 20
& opportunity.member__c.Revenue__c is 20
the value should be 40 but here it counting has 50.
its counting the old value even.
can any one help me to resolve it??
trigger updateAccount on member__c (After Insert,After update,After Delete,) { List<Account> UpdateList = new List<Account>(); Id recTypeFinance = [select Id,name from RecordType where name='Finance' and SObjectType='member__c' limit 1].Id; string ids; if(Trigger.isInsert || Trigger.isUpdate) { for(Asset__c asst : Trigger.new) { if(asst.Opportunity__c != null) ids= asst.id; } For(member__c ast:[select Id,Opportunity__c,Revenue__c,RecordTypeId from member__c where ID=:ids]){ if(ast.RecordTypeId == recTypeFinance){ for(opportunity opp : [Select Id,AccountId from Opportunity where Id =: ast.Opportunity__c]){ for(Account acc : [Select Id,Finance__c from Account where Id =: opp.AccountId]){ if(acc.Finance__c == null){ acc.Finance__c= ast.Revenue__c; }else{ acc.Finance__c += ast.Revenue__c; } UpdateList.add(acc); } } } } update UpdateList; }
All Answers
In above trigger is on member__C object but your mentioned triiger.new on Asset__c.
for(Asset__c asst : Trigger.new) {
if(asst.Opportunity__c != null)
ids= asst.id;
}
could you please confirm why we are using Asset here.
Thanks
Varaprasad
Any clues?
Please check once below code :
Thanks
Varaprasad
on line 15 it was giving me a compile error,so i removed braces of oldmap(),and now its adding extra 1 number to total.
example :-- if i enter 10 and updated it,it was adding 11 to total,and its not working as we expected,can you please have a look
acc.Finance__c += ast.Revenue__c;
its working for updated records now, but for new records its giving me below error
Please add above line in else condition check once debug logs.
Thanks
Varaprasad
double revenue = '';
Try below and let me know if it works
trigger accUpdate on member__c (After Insert,After update,After Delete) {
List<Account> UpdateList = new List<Account>();
Id recTypeFinance = [select Id,name from RecordType where name='Finance' and SObjectType='member__c' limit 1].Id;
string ids;
if(Trigger.isAfter && Trigger.isInsert || Trigger.isUpdate) {
for(member__c asst : Trigger.new) {
if(asst.Opportunity__c != null)
ids= asst.id;
}
For(member__c ast:[select Id,Opportunity__c,Revenue__c,RecordTypeId from member__c where ID=:ids]){
system.debug('ast '+ast);
if(ast.RecordTypeId == recTypeFinance){
for(opportunity opp : [Select Id,AccountId from Opportunity where Id =: ast.Opportunity__c]){
for(Account acc : [Select Id,Finance__c from Account where Id =: opp.AccountId]){
if(acc.Finance__c == null){
acc.Finance__c= ast.Revenue__c;
}else
if (Trigger.isUpdate ){
Decimal revn= Trigger.oldMap.get(ast.Id).Revenue__c;
if(revn != ast.Revenue__c)
acc.Finance__c =acc.Finance__c - revn + ast.Revenue__c;
}else{
acc.Finance__c += ast.Revenue__c;
}
UpdateList.add(acc);
}
}
}
}
update UpdateList;
}
}
Thanks