You need to sign in to do that
Don't have an account?
Himanshu Ghate
Can someone tell me what happing in this particular code and its not working.
trigger acctopp on Opportunity (after insert,after update,after delete) {
Set<id> accid = new Set<id>();
if(Trigger.isInsert || Trigger.isUpdate )
{
for(Opportunity opp:Trigger.new)
{
accid.add(opp.AccountId);//inserting accountid inside accid(inserting inside the set values)
}
}
if(Trigger.isdelete)
{
for(Opportunity opp:Trigger.old)
{
accid.add(opp.AccountId);
}
}
List<account> acclist = new List<account>();
acclist = [Select id , name ,Account.Total_Opp__c,(Select id , name ,amount from Opportunities) from Account where Id IN:accid];//Query of account
decimal total =0;
for(account acc : acclist)
{
for(opportunity opp : acc.opportunities)
{
total = total + opp.Amount;
}
acc.Total_Opp__c=total;
}
// update acclist;
}
The Scenario was-->When user go to Opportunity object and make some changes in Amount field or insert new record at that time in Account object there will be Total opp amount field will be there,And that changes which we made inside opportunity object in Amount field should reflect inside account object Total opp amount field automatically.
Set<id> accid = new Set<id>();
if(Trigger.isInsert || Trigger.isUpdate )
{
for(Opportunity opp:Trigger.new)
{
accid.add(opp.AccountId);//inserting accountid inside accid(inserting inside the set values)
}
}
if(Trigger.isdelete)
{
for(Opportunity opp:Trigger.old)
{
accid.add(opp.AccountId);
}
}
List<account> acclist = new List<account>();
acclist = [Select id , name ,Account.Total_Opp__c,(Select id , name ,amount from Opportunities) from Account where Id IN:accid];//Query of account
decimal total =0;
for(account acc : acclist)
{
for(opportunity opp : acc.opportunities)
{
total = total + opp.Amount;
}
acc.Total_Opp__c=total;
}
// update acclist;
}
The Scenario was-->When user go to Opportunity object and make some changes in Amount field or insert new record at that time in Account object there will be Total opp amount field will be there,And that changes which we made inside opportunity object in Amount field should reflect inside account object Total opp amount field automatically.
for more dynamic please create a helper class and peaste the code there and just call that class in switch case where you want, hope it will work
trigger triggeronopportunity on Opportunity (after insert , after update,after delete) {
try{
list<id> accountId = new list<id>();
for(opportunity opt:trigger.new){
if(opt !=null){
accountId.add(opt.Accountid);
}
}
switch on trigger.operationtype{
when AFTER_INSERT{
list<Account> newUpdate = new list<Account>();
newUpdate = [select id, name ,Total_opportunity__c,(Select id ,name,amount from Opportunities) from account where id In:accountId];
for(account act:newUpdate ){
Decimal amont =0;
for(opportunity opt:act.Opportunities){
amont = amont+opt.Amount;
}
act.Total_opportunity__c = amont;
}
update newUpdate;
}
when AFTER_UPDATE{
}
when AFTER_DELETE{
}
}
}catch(Exception ex){
system.debug('Exception::::'+ex.getLineNumber()+'at'+ ex.getMessage());
}
}
thank you if it is help full please mask sa best answer
If you find my answer helpful, please mark it as the best answer. Thanks!