+ Start a Discussion
suresh143suresh143 

need trigger for requriement

Ihave an account object there is one field no of opportunitys.once i had created new record with account and no of opportunitys  count displayed in Account object field like no of opportunitys in detail page  

hitesh90hitesh90

Hi suresh,

 

No need to write apex trigger for your requirement. You can create new Roll-up summary field on account object which

automatically count the number of opportunities in particular account.

see below links

Roll up Summary fields in Salesforce

Defining Roll-Up Summaries

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

suresh143suresh143
Now want to through trigger
hitesh90hitesh90

Hello,

 

Try to use below sample trigger code.

 

Apex Trigger:

trigger trgCountOpptrigger on Opportunity (after insert, after update) {
    List<Opportunity> lstOpportunity = new List<Opportunity>();
    lstOpportunity = [select id, AccountId from Opportunity where id in: trigger.newmap.keyset()];    
    set<Id> sAccId = new set<Id>();
    for(Opportunity t: lstOpportunity){
        if(t.AccountId != null){
            sAccId.add(t.AccountId);
        }
    }
    if(sAccId != null && sAccId.size() > 0){
        List<Account> lstAccount = [select id, test89__Count_Opportunities__c, (select id from Opportunities) from Account where id in: sAccId];
        if(lstAccount.size() > 0){
            for(Account acc: lstAccount){               
                acc.Count_Opportunities__c = acc.Opportunities.size();
            }
            
            update lstAccount;
        }
    }
}

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

suresh143suresh143
Once i have created record in account object and automatically created releted list contact record created how to achive this