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
BehzadBehzad 

JavaScript List button to update selected records

Hey guys,

 

I have a JavaScript List Button that I'm using on a Related List.

 

Users are to select a number of records from the serviceVendorPartnership Related List, and click on Disable button that automatically changes the branchType__c picklist field to "None" for selected records. 

 

I know that I have to update a list of records, and I'm using an update connection method to update all records in the list, as highlighted in my code below:

 

{!REQUIRESCRIPT("/soap/ajax/16.0/connection.js")} 
records = {!GETRECORDIDS($ObjectType.serviceVendorPartnership__c)};

var newRecords = [records];

//Making Sure user has selected at least one row
if(records.length<1) {
alert("Please choose at least one Service Vendor Location to disable.");
} else {

//Confirming user's action
var r = confirm("Click ''OK'' to disable selected Service Vendor Locations.");
if (r == true) {
try {

records.branchType__c = 'None';
result = sforce.connection.update([newRecords]);
window.location.reload();

if (result[0].getBoolean("success")) {
alert("Service Vendor Location with id " + result[0].id + " updated");
} else {
alert("failed to update Service Vendor Location" + result[0]);
}
}
catch (e) {
alert(e);
}
}

 

 

My button is throwing the following error message:

failed to update Service Vendor Location{errors:{message:'sObject type 'sObject' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.', statusCode:'INVALID_TYPE', }, id:null, success:'false', }

 

Does anyone know why the id list is returning null?

 

I greatly appreciate your feedbacks.

 

Thank you,

 

Behzad

Best Answer chosen by Admin (Salesforce Developers) 
BehzadBehzad

Resolved.

 

Correct code:

 

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

var records = {!GETRECORDIDS($ObjectType.serviceVendorPartnership__c)};

var newRecords = [];

if(records.length<1) {
alert("Please choose at least one Service Vendor Location to disable.");
} else {
var r = confirm("Click ''OK'' to disable selected Service Vendor Locations."); 
if (r == true) { 
try {
for (var n=0; n<records.length; n++){

var sv = new sforce.SObject("serviceVendorPartnership__c");
sv.id = records[n];

sv.branchType__c = 'None';

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

if (result[0].getBoolean("success")) {
alert("Service Vendor Location with id " + result[0].id + " updated");
} else {
alert("failed to update Service Vendor Location" + result[0]);
}
}
catch (e) {
alert(e);
}
}
}