You need to sign in to do that
Don't have an account?
Naveen Ashok Sirivella
Creating rollup summary values for lookup by using triggers
Hi All,
I want to create rollup summary fields on Account object.
Scenario : calculate how many open opportunities (except closed won,closed lost) and populate on account record.
please find the below code
trigger CalculatingOpenOpportunity on Opportunity (After insert)
{
if(Trigger.isInsert && Trigger.isAfter)
{
Set<id> accountids = new Set<id>();
List<Account> acclist = new List<Account>();
for(Opportunity opp : trigger.new)
{
if(opp.StageName != 'Closed Won' && opp.StageName != 'Closed Lost')
{
accountids.add(opp.id);
system.debug('values in the accountids:========================================>'+accountids);
}
}
// get a map of the accounts
Map<id,Account> accountMap = new Map<id,Account>([select id,Open_opportunities_count__c from Account where id IN :accountids]);
System.debug('values in the accountMap :=============================================>'+accountMap);
//Quering related opportunities and accounts.
for(Account acc:[select id,name,Open_opportunities_count__c,(select id,name,StageName from opportunities) from Account where ID IN :accountids])
{
accountMap.get(acc.Id).Open_opportunities_count__c = acc.Opportunities.size();
System.debug('values in the accountMap.get(acc.Id).Open_opportunities_count__c:================================>'+accountMap.get(acc.Id).Open_opportunities_count__c);
//Adding count and opportunities to list
acclist.add(accountMap.get(acc.Id));
}
if(acclist.size() > 0)
{
System.debug('Size of the acclist :==============================>'+acclist.size());
database.update(acclist);
}
}
}
Please help on above scenario's
I want to create rollup summary fields on Account object.
Scenario : calculate how many open opportunities (except closed won,closed lost) and populate on account record.
please find the below code
trigger CalculatingOpenOpportunity on Opportunity (After insert)
{
if(Trigger.isInsert && Trigger.isAfter)
{
Set<id> accountids = new Set<id>();
List<Account> acclist = new List<Account>();
for(Opportunity opp : trigger.new)
{
if(opp.StageName != 'Closed Won' && opp.StageName != 'Closed Lost')
{
accountids.add(opp.id);
system.debug('values in the accountids:========================================>'+accountids);
}
}
// get a map of the accounts
Map<id,Account> accountMap = new Map<id,Account>([select id,Open_opportunities_count__c from Account where id IN :accountids]);
System.debug('values in the accountMap :=============================================>'+accountMap);
//Quering related opportunities and accounts.
for(Account acc:[select id,name,Open_opportunities_count__c,(select id,name,StageName from opportunities) from Account where ID IN :accountids])
{
accountMap.get(acc.Id).Open_opportunities_count__c = acc.Opportunities.size();
System.debug('values in the accountMap.get(acc.Id).Open_opportunities_count__c:================================>'+accountMap.get(acc.Id).Open_opportunities_count__c);
//Adding count and opportunities to list
acclist.add(accountMap.get(acc.Id));
}
if(acclist.size() > 0)
{
System.debug('Size of the acclist :==============================>'+acclist.size());
database.update(acclist);
}
}
}
Please help on above scenario's
Below is the code for you, just copy paste:
Mark solved if it works as expected for your requirment
All Answers
https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000B45gWEAR
Below is the code for you, just copy paste:
Mark solved if it works as expected for your requirment