You need to sign in to do that
Don't have an account?
Sumanth Kumar 17
Trigger for Account amount field that to be updated based on related opportunity amount field
Hello Developers....I have a task where Account amount field to be updated based on opportunity amount field. I have total value field in account and for its related opportunity, if we enter amount for those opportnities automatically the total value field in the account should be updated. I need to write a trigger for that. Can anyone help me with this task...
Thanks in advance
Sumanth kumar
Thanks in advance
Sumanth kumar
Hi Sumanth,
You can use below trigger:
trigger Opportunity_AIUD on Opportunity (after insert, after update, after delete) {
Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
Set<Id> acctIds = new Set<Id>();
List<Opportunity> opptyList = new List<Opportunity>();
if(trigger.isUpdate || trigger.isInsert){
for(Opportunity oppty : trigger.New){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(trigger.isDelete){
for(Opportunity oppty : trigger.old){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(acctIds.size() > 0){
opptyList = [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds];
for(Opportunity oppty : opptyList){
if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
}
acctIdOpptyListMap.get(oppty.AccountId).add(oppty);
}
List<Account> acctList = new List<Account>();
acctList = [SELECT Total_Opportunity_Amount__c FROM Account WHERE Id IN: acctIds];
for(Account acct : acctList){
List<Opportunity> tempOpptyList = new List<Opportunity>();
tempOpptyList = acctIdOpptyListMap.get(acct.Id);
Double totalOpptyAmount = 0;
for(Opportunity oppty : tempOpptyList){
if(oppty.Amount != null){
totalOpptyAmount += oppty.Amount;
}
}
acct.Total_Opportunity_Amount__c = totalOpptyAmount;
}
update acctList;
}
}
All Answers
Hi Sumanth,
This can be achieved by making the Roll-up summary field on Account.
If you want to achieve it by trigger plz let me know.
Thanks
Thanks
Sumanth Kumar
Hi Sumanth,
You can use below trigger:
trigger Opportunity_AIUD on Opportunity (after insert, after update, after delete) {
Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
Set<Id> acctIds = new Set<Id>();
List<Opportunity> opptyList = new List<Opportunity>();
if(trigger.isUpdate || trigger.isInsert){
for(Opportunity oppty : trigger.New){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(trigger.isDelete){
for(Opportunity oppty : trigger.old){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(acctIds.size() > 0){
opptyList = [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds];
for(Opportunity oppty : opptyList){
if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
}
acctIdOpptyListMap.get(oppty.AccountId).add(oppty);
}
List<Account> acctList = new List<Account>();
acctList = [SELECT Total_Opportunity_Amount__c FROM Account WHERE Id IN: acctIds];
for(Account acct : acctList){
List<Opportunity> tempOpptyList = new List<Opportunity>();
tempOpptyList = acctIdOpptyListMap.get(acct.Id);
Double totalOpptyAmount = 0;
for(Opportunity oppty : tempOpptyList){
if(oppty.Amount != null){
totalOpptyAmount += oppty.Amount;
}
}
acct.Total_Opportunity_Amount__c = totalOpptyAmount;
}
update acctList;
}
}
Error: Compile Error: Incorrect SObject type: Opportunity should be Account at line -1 column -1
Can you please fix this?
Are you writing trigger on Account?
Please write it on Opportunity
Cheers
Sumanth Kumar
Hi Sumanth,
Did your requirement was not fulfilled by the above trigger?
Please let me know
Thanks
Sumanth Kumar
Thanks
SUmanth Kumar
Hi Sumanth,
1.) Here AccountId is lookup field of Account on Opportunity.
2.) Set is used for storing Id of Account corresponding to each Opportunity.
3.) Map is key-value pair. Here key is AccountId and value is List of Opportunities corresponding to that AccountId.
If you have any more query then you can drop me mail on below address:
dpkm20@gmail.com
I have following doubts regarding that trigger.
1) I do not have AccountID lookup field on Opportunity. But how did the trigger getting fired without that field.
2) In Map<Id, List<Opportunity>> and Set<Id>, are both ID's used in set and map are same?
3) As you said that key used in map is AccountId, is ID are AccountID same?
sorry for more queries as am in process of understanding each and every line of code.
Thanks
Sumanth kumar