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
Simon BairdSimon Baird 

Help with OnClick Javascript List view button

Hi there,

I was hoping someone could shed some light on why this button won't fire when added to a list view. It works fine when added as a detail page button and used on one record. Basically I want to reduce the value of a field by 1 when the button is clicked. I am hoping to add it to a related object's list view so our users can select multiple records to apply the change to.

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}

var records = {!GETRECORDIDS($ObjectType.Patient__c)};
if (records[0] == null) {

alert("Please select at least one record to update.");}
else {

var update_patient = new sforce.SObject("Patient__c");
update_patient.id = "{!Patient__c.Id}";
update_patient.Claims_remaining__c = {!Patient__c.Claims_Remaining__c} - 1;
result = sforce.connection.update([update_patient]);
}

window.location.reload();
Best Answer chosen by Simon Baird
PrasanntaPrasannta (Salesforce Developers) 
Hi,

Update the get the selected patient session ids
var records = {!GETRECORDIDS($ObjectType.Patient_Session__c)};

query the patient ids and remaining claims of the patients thru the sessions (replace the field names )
var patientSessions = sforce.connection.retrieve('Id,Patiend__c,Patient__r.Claims_remaining__c', 'Patient_Session__c', records);

then loop thru the sessions and get the related patient record and update it

for (var a=0; a<patientSessions.length; a++) {
var update_Patient = new sforce.SObject('Patient__c'); 
update_Patient.Id = patientSessions[a].Patient__c;
if(patientSessions[a].Patient__r.Claims_remaining__c > 1)
  update_Patient.Claims_remaining__c = patientSessions[a].Patient__r.Claims_remaining__c - 1;
updateRecords.push(update_Patient);
}

update your records
result = sforce.connection.update([updateRecords]);
THis will on how to update the related records..
Hope it helps.