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
KS KumaarKS Kumaar 

How to get count of all selected fields in a record?

Let consider an object has 10 fields and one more field called CountField. If I enter a value for field1 and if i save it, Then the CountField value should be updated with 1. Same like that if I enter value for field1 and field2, Then the CountField field value should be updated with 2. Even if i enter a values for field1, field3, field9 and field5, then the CountField should be updated with 4 . Like that i enter values for all fields, then countfield vlaue should be 10. Which means how many fields i entered values, the same value should be updated with CountField.
I have tried alot. but i could not get any logic. Please provide some solution

User-added imageUser-added image
Note: The value for countfield in image is entered by me manually. I want it automatically updated.

Thanks 
KS Kumar

Best Answer chosen by KS Kumaar
SujilaSujila
Hi,

As far as I understood, your requirement is to check how many fields are filled by the user and count them. You can simply check against the NOT NULL fields in a trigger and  update the count. Please check below for a smaple trigger. Here I have manually given  a list of fields to check. You can dynamically get fields from Schema class as well.
 
trigger HellowWorldTrigger on HelloWorld__c (before insert, before update) {
    for(HelloWorld__c hello: Trigger.new){
        Integer counter = 0;
        SObject obj = (HelloWorld__c)hello;
        List<String> Fields = new List<String>{'Email__c', 'Father_Name__c', 'Father_Phone_Number__c', 'First_Name__c',
            'Industry__c', 'Last_Name__c', 'Mother_Name__c', 'Mother_Phone_Number__c', 'Phone_Number__c', 'Title__c'};
        for(String fieldName: Fields){
            System.debug('Now checking: '+fieldName+ ' Value: '+ obj.get(fieldName));
            if(obj.get(fieldName) != null){
                counter++;
                obj.put('CountField__c', counter);
            } 
        }
    }
}

User-added image

All Answers

sslodhi87sslodhi87
Is this is the custom vf page or salesforce standard page?

If it is custom page then we can use java script or jquery else we need to write the trigger and compare the old and new value.. on the basis of that we can update that field.

Please let me know if this helps you.

Thanks,
 
KS KumaarKS Kumaar
I wanna it through trigger
ManojjenaManojjena
Hi Admin User 4105,

You can achive it through trigger ,however i have  questions here .Why you want to count this ? If you will updated the record for first time and modified two fields then the count will be updated to 2 .When you wil update second time and updated 3 fields then your count will be 5 or 3 .

Problem is that when you will add any fields in the object you need to modify the trigger .
If you are ok with above problem then let us know what exactly how you want to update the field ,so that we wil help you out .
Thanks
Manoj
KS KumaarKS Kumaar

Hi Manoj....! 

I unsterstood your question. let we take 10 fields only and we can do on that

ManojjenaManojjena
HI ,
try like below and modify accordigly .
 
trigger testTrigger on Account(before update){
  Integer count=0;
  for(Account acc : Trigger.new ){
    if(Trigger.oldMap.get(acc.Id).firstField__c != acc.FirstField__c){
	  count++;
	}if(Trigger.oldMap.get(acc.Id).SecondField__c != acc.SecondField__c){
	count++;
	}if(Trigger.oldMap.get(acc.Id).thirdField__c != acc.thirdField__c){
	count++;
	}if(Trigger.oldMap.get(acc.Id).forthField__c != acc.forthField__c){
	count++;
	}if(Trigger.oldMap.get(acc.Id).fifthField__c != acc.fifthField__c){
	count++;
	}if(Trigger.oldMap.get(acc.Id).SixthField__c != acc.SixthField__c){
	count++;
	}if(Trigger.oldMap.get(acc.Id).seventhField__c != acc.seventhField__c){
	count++;
	}if(Trigger.oldMap.get(acc.Id).eightField__c != acc.eightField__c){
	count++;
	}if(Trigger.oldMap.get(acc.Id).nineField__c != acc.nineField__c){
	count++;
	}if(Trigger.oldMap.get(acc.Id).tenthField__c != acc.tenthField__c){
	count++;
	}
	acc.countField__c=count;
  }
}
Thanks
Manoj
 
SujilaSujila
Hi,

As far as I understood, your requirement is to check how many fields are filled by the user and count them. You can simply check against the NOT NULL fields in a trigger and  update the count. Please check below for a smaple trigger. Here I have manually given  a list of fields to check. You can dynamically get fields from Schema class as well.
 
trigger HellowWorldTrigger on HelloWorld__c (before insert, before update) {
    for(HelloWorld__c hello: Trigger.new){
        Integer counter = 0;
        SObject obj = (HelloWorld__c)hello;
        List<String> Fields = new List<String>{'Email__c', 'Father_Name__c', 'Father_Phone_Number__c', 'First_Name__c',
            'Industry__c', 'Last_Name__c', 'Mother_Name__c', 'Mother_Phone_Number__c', 'Phone_Number__c', 'Title__c'};
        for(String fieldName: Fields){
            System.debug('Now checking: '+fieldName+ ' Value: '+ obj.get(fieldName));
            if(obj.get(fieldName) != null){
                counter++;
                obj.put('CountField__c', counter);
            } 
        }
    }
}

User-added image
This was selected as the best answer
KS KumaarKS Kumaar
Thank you @Sujila and @Manoj