+ Start a Discussion
Samuel JohnsonCBESamuel JohnsonCBE 

counting opp and update account field

I am tyring to build a process that will count the number of opportunities in a particular stage and based upon results will determine with picklist value on the account page will have.  Example
Opportunity Status is set to Status 1.  update Opportunity 2 to a higher status, it would change the account picklist value.  If that same opportunity were to be lost it would then lower the account field.  Then if the 1st opportunity also loses it would then change the account picklist field again.  Basisally which ever has the highest opportunity status between creating opportunity, higher statuses or lost would determine with value it is.  

Any thoughts. 
NRD 4 UNRD 4 U
Hello Samuel,

I suppose you want to count opportunities which are open or won and not lost. You have given another scenario that if you change the opp 2 status to a higher value it shoud increase the count. It is a bit contradictory.
However This can be done with process builder as well as apex trigger.
Process Builder/ Trigger- create count and status fields on account. Whenever opp is created or updated (status) with account check if the status on account is null then put opp status. Check if status on account is higher than the created/updated opp then don't update the status if lower then update the status. Accordingly increase the count.

If you just want to count the opportunities of certain stage then create the same number of fields on account as opportunity stage.
Now create a process builder or write a trigger. you can check the prior value of status and the currecnt value of status.
say you have 3 status open, abandon and closed. If prior value was open reduce 1 count fropm open in account, if current value is closed add 1 in closed opp count. 

Please let me know if it clarifies.

Thanks!
NRD
Samuel JohnsonCBESamuel JohnsonCBE
The issue is the order of executive process and workflow comes before rollup summary formulas.  I need to process/workflow to fire after rollup summary formula has happened.
NRD 4 UNRD 4 U
Hi Samuel,

In this case you can go with After trigger and that should work.
Please let me if you have any doubt..

Thanks!
NRD
Samuel JohnsonCBESamuel JohnsonCBE
The trigger would be "After Insert" that would then do the update on the account status field.  I've never writting an trigger, I wouldn't even know where to start to build this after trigger.  Would you have a template of some coding that I can start with.  please
NRD 4 UNRD 4 U
Hi Samuel,

Please follow the below link-
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_trigger.htm

Thanks!
Neelesh
NRD 4 UNRD 4 U
PFB the dummy code according to your requirement:

trigger updateAccount on Opportunity(after insert, after update) {
    // account set
    set <account> oppAccountSet = new set <account>();
    list <account> oppAccountList = new list <account>();
    list <account> accToUpdate = new list <account>();
    for (opportunity opp :Trigger.new){
        // add linked account in account list
        oppAccountSet.add(opp.account);
    }
    //oppAccountList write a querry to fetch all accounts whose id in oppAccountList
    oppAccountList = (select id, name,status from account where id in : oppAccountSet); // say account has status and countOpen (to count open opportunity) fields and is null
    
    // create loop to check the condition
    for(opportunity opp :Trigger.new){
        for(account acc : oppAccountList){
            if(opp.account=acc.id && (acc.status==null || acc.status=='')){  
                acc.status=opp.status;
                countOpen=countOpen+1;
                oppAccountList.add(acc);
            } else if(opp.account=acc.id && acc.status!=null && opp.status!=acc.status && opp.status=="Open" && acc.status!="open"){
                acc.status=opp.status;
                oppAccountList.add(acc);
                countOpen=countOpen+1;
            }// put more similar condition if required
        }
    }
    if(oppAccountList.size>0){
        update oppAccountList;
    }
 }


Hope it helps.
Please mark the answer as best if it helps you.

Thanks!
NRD