You need to sign in to do that
Don't have an account?
pranavshah
opportunity update trigger
Hi,
can someone explain me the below code. i am facing some difficulty in learning... the trigger is for geeting opportunity amount updated into Account
public class opportunityhandler
{
public void opportunityamount(list<opportunity> newopportunity)
{
set<Id> setOppName=new set<Id>();
List<Account> lstActs =new list<Account>();
List<Opportunity> opps=new list<Opportunity>();
for(opportunity opp:newopportunity)
{
setOppname.add(opp.Accountid);
}
list<Account> LstAccs= [select id,Name,Total_Opportunity_Amount__c,(select id, Amount from opportunities) from account where Id IN:setOppname];
for(Account acc: lstAccs)
{
double TotalAmount=0;
for(Opportunity opp:acc.opportunities)
{
if(opp.Amount!=null)
{
totalAmount=TotalAmount+opp.Amount;
}
}
acc.Total_Opportunity_Amount__c=totalAmount;
lstacts.add(acc);
}
update lstacts;
}
}
can someone explain me the below code. i am facing some difficulty in learning... the trigger is for geeting opportunity amount updated into Account
public class opportunityhandler
{
public void opportunityamount(list<opportunity> newopportunity)
{
set<Id> setOppName=new set<Id>();
List<Account> lstActs =new list<Account>();
List<Opportunity> opps=new list<Opportunity>();
for(opportunity opp:newopportunity)
{
setOppname.add(opp.Accountid);
}
list<Account> LstAccs= [select id,Name,Total_Opportunity_Amount__c,(select id, Amount from opportunities) from account where Id IN:setOppname];
for(Account acc: lstAccs)
{
double TotalAmount=0;
for(Opportunity opp:acc.opportunities)
{
if(opp.Amount!=null)
{
totalAmount=TotalAmount+opp.Amount;
}
}
acc.Total_Opportunity_Amount__c=totalAmount;
lstacts.add(acc);
}
update lstacts;
}
}
This is a trigger handler class which is being referenced in the Trigger.
In this class, you are passing the List of Opportunity which will fire the Trigger. First of all you are creating the collections i.e List and Set to Store Account and Opportunities details.
for(opportunity opp:newopportunity)
{
setOppname.add(opp.Accountid);
}
Above snippet will store AccountId of Opportunities to Set of Id which will again be used to Query Account record from the Account Object.
list<Account> LstAccs= [select id,Name,Total_Opportunity_Amount__c,(select id, Amount from opportunities) from account where Id IN:setOppname];
In the below section:
for(Account acc: lstAccs)
{
double TotalAmount=0; // Variable declared to store the Opportunity Amount
for(Opportunity opp:acc.opportunities) //Iterate over all related Opportunity of the Account
{
if(opp.Amount!=null)
{
totalAmount=TotalAmount+opp.Amount; //Add Amount field of all related Opportunities
}
}
acc.Total_Opportunity_Amount__c=totalAmount;
lstacts.add(acc); //Add the Updated Account record to the Account List
}
update lstacts; //Update List
}
}
P.S. Added the comment in Bold/Italics to the Code for Understanding purpose
Your above code snippet is doing all the processing of calculating the Total_Opportunity_Amount__c on the Account Object of all the related Opportunities.
Say for Eg: Account XYZ have 5 related Opportunities of $1000 each, then the above snippet will calculate the Sum of Amount of all the related Opportunities and will update the same in the Account Object in Total_Opportunity_Amount__c field.
Please let me know if it solved your purpose or if you need any more assistance.
Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com