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
Gwirizanani SinoiaGwirizanani Sinoia 

Update a record based on multiple field values

Hi everyone,
I have a total of five fields with data type picklist all with values low, meduim and high. I looking to update a record when at least four of the five fields have a value low or meduim. Please note the fields are on the same record.

thanks in advance
Best Answer chosen by Gwirizanani Sinoia
Abdul KhatriAbdul Khatri
Sorry here is the corrected one
 
trigger <triggerName> on <ObjectName> (before update) {
    
    Integer count;
    
    for (ObjectName obj : trigger.New) {
        
        count = 0;
        
        count = (obj.Field1__c != 'high') ? count+1 : count;
        count = (obj.Field2__c != 'high') ? count+1 : count;
        count = (obj.Field3__c != 'high') ? count+1 : count;
        count = (obj.Field4__c != 'high') ? count+1 : count;
        count = (obj.Field5__c != 'high') ? count+1 : count;        
        
        if(count == 4) {
            obj.FieldNametoupdate__c = //value;
            //other fields update
        }
            
    }
}


 

All Answers

Abdul KhatriAbdul Khatri
Since you haven't provided the exact field name so just prototype for you, replace names accordingly
 
trigger <triggerName> on <ObjectName> (before update) {
    
    Integer count;
    List<ObjectName> objListToUpdate = new List<ObjectName>();
    
    for (ObjectName obj : trigger.New) {
        
        count = 0;
        
        count = (obj.Field1 != 'high') ? count++ : count;
        count = (obj.Field2 != 'high') ? count++ : count;
        count = (obj.Field3 != 'high') ? count++ : count;
        count = (obj.Field4 != 'high') ? count++ : count;
        count = (obj.Field5 != 'high') ? count++ : count;        
        
        if(count == 4) {
            objListToUpdate.add(obj);
        }
            
    }
    
    for (ObjectName obj : objListToUpdate) {
        
        obj.FieldNametoupdate = //value;
        //other fields update
        
    }
    
    update objListToUpdate;

}

 
Abdul KhatriAbdul Khatri
Was this helpful?
 
Gwirizanani SinoiaGwirizanani Sinoia
Hi sorry, it did not work. Will try again 
Abdul KhatriAbdul Khatri
Sorry here is the corrected one
 
trigger <triggerName> on <ObjectName> (before update) {
    
    Integer count;
    
    for (ObjectName obj : trigger.New) {
        
        count = 0;
        
        count = (obj.Field1__c != 'high') ? count+1 : count;
        count = (obj.Field2__c != 'high') ? count+1 : count;
        count = (obj.Field3__c != 'high') ? count+1 : count;
        count = (obj.Field4__c != 'high') ? count+1 : count;
        count = (obj.Field5__c != 'high') ? count+1 : count;        
        
        if(count == 4) {
            obj.FieldNametoupdate__c = //value;
            //other fields update
        }
            
    }
}


 
This was selected as the best answer
Gwirizanani SinoiaGwirizanani Sinoia
still not working. however will keep trying
Abdul KhatriAbdul Khatri
Please share the code you are using, so that I can pin point the issue.
Gwirizanani SinoiaGwirizanani Sinoia
My fault,

Thanks Abdul. I have managed to resolve the issue.