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
Nicholas PatellaNicholas Patella 

Adding Null/Not Null Field Value Check to List Button Script (Multiple Records)

I created a List Button on the Contacts related list that allows users to select multiple Contacts and update a specified field, which then triggers an email alert to the selected Contacts (in this case, a survey). I wanted to add an additional validation check that does the following:
  • Check if the value of a specific field (Settings_Survey_Type__c) on all selected Contacts is null or not null
  • If any Contacts have a null value in this field, throw an error message
  • If all selected Contacts have a not null value in this field, continue executing the script
The thinking of adding the above is that this would allow me to determine which version of the survey to send based on the value in that field (which I can do via process builder). If it's blank, it wouldn't know which survey to send and should throw an error message.

Here is the current script:
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")}; 
{!REQUIRESCRIPT("/soap/ajax/36.0/apex.js")}; 

if(confirm('Send survey(s) now?')) { 

var records1 = {!GETRECORDIDS($ObjectType.Contact)}; 
var newRecords = []; 
var contactRec = sforce.connection.query('select id,name,Send_Settings_Survey__c from Contact where id in (\'' + records1.join('\',\'') + '\')' ); 

var contacts = contactRec.getArray("records"); 
var arrayLength = contacts.length; 
if(records1==null || records1=='') { 
alert('No records selected'); 
} 
else{ 
for(var i=0;i<arrayLength;i++){ 
var con = new sforce.SObject("Contact"); 
con.Id = contacts[i].Id; 
con.Send_Settings_Survey__c = true; 
newRecords.push(con); 
} 

result = sforce.connection.update(newRecords); 
window.location.reload(); 
} 

}else{ 
}

I'm assuming I need to add the Settings_Survey_Type__c to the sforce.connection.query portion, but I'm not sure how to then check if that field is null or not for all Contacts in the array. Any ideas on how I would modify my script to add this check?