function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Venkateswarlu PVenkateswarlu P 

when we are inserting new opportunity for an account, that optycount field value in Account objeet should increment , when we are deleting opportunity corresponding account optycount field value should decrement.

public static void accountOptyCount(List<Opportunity> optyList){
        List<Account> accList=new List<Account>();
        Set<Id> optyIds = new Set<Id>();        
        For(Opportunity op:optyList){
           optyIds.add(op.AccountId);
           System.debug('optyIds List=======>: '+optyIds); 
        }
        accList=[SELECT Id,optyCount__c,(SELECT Id from Opportunities) from Account where Id in:optyIds];
        System.debug('accList List=======>: '+accList);
        For(Account a:accList){
            a.optyCount__c=a.Opportunities.size();
            System.debug('Opportunity count=======>: '+a.optyCount__c);
           /* Decimal sum=0;
            For(Opportunity opp:a.Opportunities){                
                sum=sum+1;
                System.debug('Sum=======>: '+sum);
            }
            a.optyCount__c=sum; */
        }
        update accList;
    }
===========
If(Trigger.isBefore && Trigger.isInsert){        
        Opportunity_Trigger_handler.accountOptyCount(Trigger.new);}
Best Answer chosen by Venkateswarlu P
Keyur  ModiKeyur Modi
Hi,

You need to execute your trigger on "After Insert" and "After Delete" event Because "before Event" will get execute before record commit to data base. Here in your case , you are query data from data base so as per the data base you have 5 opportunity only in "before Insert", if you execute your logic in "after insert" event then your new opportunity will get commint to data base so while querying info from data base it will return 6 opporutnity.

Here are few links which will help you to understand the trigger execution and salesforce transaction.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm

Thanks,
Keyur Modi

All Answers

Keyur  ModiKeyur Modi
Hi ,

As per the below code  your trigger will get only executed in before Insert event. 
If(Trigger.isBefore && Trigger.isInsert){        
        Opportunity_Trigger_handler.accountOptyCount(Trigger.new);}
you have to call your helped class in Delete event as well. 

Please check and let me know if I can help you further.

Thanks,
Keyur Modi
 
Sagar PatilSagar Patil
Hi Venkateswarlu,

Hope you have created trigger on Opportunity object and optyCount__c is Number field on Account object
 
trigger countOfOpp on Opportunity (after insert, after delete) {

  If(Trigger.isAfter && Trigger.isInsert){        
        Opportunity_Trigger_handler.accountOptyCount(Trigger.new);
        }
    If(Trigger.isAfter && Trigger.isDelete){        
        Opportunity_Trigger_handler.accountOptyCount(Trigger.old);
        }     
        
        
    
}

Here, you will need after insert and delete because you have to update count on other object so before trigger won't work here. Kindly modify your trigger as above and check once.

Regards,
Sagar
Venkateswarlu PVenkateswarlu P
Hi Keyur Modi,
Let say Account has 5 opportunities.created one more opportunity for the same account.The optyCount__c value need to 6,but it showing 5 only.
If we delete the opportunity the optyCount__c value working.
is there any issue with the code.
Keyur  ModiKeyur Modi
Hi,

You need to execute your trigger on "After Insert" and "After Delete" event Because "before Event" will get execute before record commit to data base. Here in your case , you are query data from data base so as per the data base you have 5 opportunity only in "before Insert", if you execute your logic in "after insert" event then your new opportunity will get commint to data base so while querying info from data base it will return 6 opporutnity.

Here are few links which will help you to understand the trigger execution and salesforce transaction.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm

Thanks,
Keyur Modi
This was selected as the best answer
Venkateswarlu PVenkateswarlu P
Issue Solved