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
Warjie Malibago 15Warjie Malibago 15 

How to get non-ID fields in List Button?

Hi everyone,

I'm using a list button and calling a method from an external system (all good). However I need to do some validation first before I call the actual method. I'm using GETRECORDSID to get the IDs of the selected items. So I also need to get a field of those selected records to be used for the validation. How can I achieve it? Below's my code:
 
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")} 
{!REQUIRESCRIPT("//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js")} 

setTimeout(function(){ 
	try{
		var selected = {!GETRECORDIDS( $ObjectType.my_custom_object__c)};
		for(i=0; i<selected.length; i++){
			if({!selected[i].my_custom_field__c} == true){ //this is where I get the error. I cannot access the custom field.
				alert('Error here = ' + selected[i] '. Click OK to continue');
			}
			else{
				result = sforce.apex.execute('external_class','external_method',{purchaseId:selected[i]});
			}
		} 		
	}
	catch(error){
		$('#overlayBackground').remove(); 
		$('#overlayOverlay').remove();	
		alert('An error ocurred'); 		
	}
},300);
If there's no other way around, my last resort would be to use VF page.

Appreciate your help. Thanks!
Best Answer chosen by Warjie Malibago 15
VineetKumarVineetKumar
Fire a SOQL query from your button.
Something like below :
var recordsArray = sforce.connection.query("SELECT Id, Name FROM my_custom_object__c WHERE Id IN: <Ids List>");
records = recordsArray.getArray("records"); 
var recordName = records[0].Name;