How can I count mber of empty fields in a record?

246    Asked by elonji_3393 in Salesforce , Asked on Mar 2, 2023

My question is about challenges when Opportunity is created specific fields must be populated. How can I count the fields which are not null from specific fields?

 integer counter;
 integer oppValue;
    for (Opportunity : Trigger.new) {
        if (myOpp.Amount != null) {
            oppValue= 1;}
        else if {oppValue= 0;}
     // Same loop repeats for several fields assigning value to oppValue. 
    counter = oppValue ++;
    System.debug(counter);
Answered by elonjigar

To count the mber of empty fields in a record:


/*Put all the fields to check if those are blank, You can also create a custom label OR custom setting */
Set fields = new Set{'Amount','Stagename'};
Integer counter = 0;
Map oppCounterMap = new Map();
for (Opportunity myOpp: Trigger.new) {
    for(String fieldName : fields){
        // Check if the field is Blank
        if(myOpp.get(fieldName) != null)
        counter++;
    }
    /*Map of Opportunity and the number of fields which are blank on the
      respective Object */
    oppCounterMap.put(myOpp.Id, counter);
    counter= 0;
}


Your Answer

Interviews

Parent Categories