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
Supriya P 24Supriya P 24 

Wanted to update a filed in opportunity(multiple status) with different condition

Hi All,

Could you please help me for below scenerio.

Opportunity object has field called "review" and it has 3 picklist values.
there are 4 different fields which needs to be considered to update the field.

1. when al the 4 fields are true then update "review" field = pass

2 When 2 or less field are true then update "review" field= reject

3. when 3 of the fields are true then update "review" field= awaiting.

Thanks in advance 
 
Best Answer chosen by Supriya P 24
sachinarorasfsachinarorasf
Hi Supriya,

Use the below trigger for your requirement it may helpful for you

trigger TriggerForUpdateOpportunity on Opportunity(before insert, before update) {
    if( (trigger.isUpdate && trigger.isBefore) || (trigger.isInsert && trigger.isBefore)){
        for(Opportunity opp : trigger.new){
        
            // Count how many input field has true value
            Integer count = 0;
            if(opp.Active_1__c == true){
                count = count + 1;
            }
            if(opp.Active_2__c == true){
                count = count + 1;
            }
            if(opp.Active_3__c == true){
                count = count + 1;
            }
            if(opp.Active_4__c == true){
                count = count + 1;
            }
            
            // Update Review__c field value according to count
            if(count == 4){
                opp.Review__c = 'pass';
            }
            if(count <= 2){
                opp.Review__c = 'reject';
            }
            if(count == 3){
                opp.Review__c = 'awaiting';
            }
        }
    }
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com

All Answers

sachinarorasfsachinarorasf
Hi Supriya,

Use the below trigger for your requirement it may helpful for you

trigger TriggerForUpdateOpportunity on Opportunity(before insert, before update) {
    if( (trigger.isUpdate && trigger.isBefore) || (trigger.isInsert && trigger.isBefore)){
        for(Opportunity opp : trigger.new){
        
            // Count how many input field has true value
            Integer count = 0;
            if(opp.Active_1__c == true){
                count = count + 1;
            }
            if(opp.Active_2__c == true){
                count = count + 1;
            }
            if(opp.Active_3__c == true){
                count = count + 1;
            }
            if(opp.Active_4__c == true){
                count = count + 1;
            }
            
            // Update Review__c field value according to count
            if(count == 4){
                opp.Review__c = 'pass';
            }
            if(count <= 2){
                opp.Review__c = 'reject';
            }
            if(count == 3){
                opp.Review__c = 'awaiting';
            }
        }
    }
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
This was selected as the best answer
ravi soniravi soni
hi supriya,
try following code
trigger updateReviwOnOpp on Opportunity (before insert,before update) {
   
    for(Opportunity Opp : trigger.new){
    integer count = 0;
        if(Opp.Field_1__c){
            count++;
        } if(Opp.Field_2__c){
            count++;
        } if(Opp.Field_3__c){
            count++;
        } if( Opp.Field_4__c){
            count++;
        }
        
        if(count==4){
           Opp.review__c = 'pass';
        }
        else if(count == 3){
           Opp.review__c = 'awaiting'; 
        }
        else if(count == 2 || count == 1){
          Opp.review__c = 'reject';   
        }
        
        
    }
   
}

let me know if it's works and  mark it as Best Answer so that it's helps others too. 
Supriya P 24Supriya P 24
Thank You Veer and Sachin.

The fields are are populating from Account object and they have some criteria to compare before the "review" field gets updtaed. 
Say like acc.Active_2__c >50k or acc.Active_1__c <=10k are the values and opportunity record should only chnage when the Stage value is "code live".

But when i update account object with the values then opportunity "review" field is not changing to meet the requirement. Only when new opportunity is created then the values are setting. after account update its not changing and with code live status not working.

Coul you please help me how can i fix this?

Thanks in advance,
supriya
Supriya P 24Supriya P 24
Thanks it worked