Simple use of SFDC AJAX Toolkit to update a list of related records for an object

Often we have a need to do simple updates to a list of Related records from the Parent Object. This can be accomplished by creating a simple AJAX Toolkit based function and attaching that to an SFDC Custom List button on the Related (Child) object.

  1. Create a custom button or link under Custom Buttons and Links on the Related/Child object
  • Behavior should be “Execute Javascript”
  • Type will be List Button

2. Copy and paste the following method, replacing the My_Parent_Object__c and Related_Child_Object__c  with actual existing object names for your org

{!REQUIRESCRIPT(“/soap/ajax/26.0/connection.js”)}

var relatedRecsList = sforce.connection.query(“Select Id from Related_Child_Object__c where My_Parent_Object__c ='{!My_Parent_Object__c.Id}'”);

var relatedRecsArray =relatedRecsList .getArray(“records”);

if ( relatedRecsArray == null || relatedRecsArray[0] == null ) {
alert(“Nothing to update”);
} else {
var objList = [];
for (var recCount = 0; recCount < relatedRecsArray.length; recCount++) {

var aRelatedObj= new sforce.SObject(‘Related_Child_Object__c’);
aRelatedObj.Id = relatedRecsArray[recCount].Id;
aRelatedObj.Status__c = ‘UpdatedByAJAXTK’;

objList.push(aRelatedObj);
}
upObjs = sforce.connection.update (objList);

location.reload(true);
}

3. Put this List Button onto the right page layout of the My_Parent_Object__c

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s