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
Kim AdilKim Adil 

update ONLY records that meets criteria in Javascript button.

I am new to Javascript language. The code in Bold below is triggered by on-click javascript button. It's working fine; however, I was asked to tweak it to only update the records where the lookup field " agreement " is equal to 'Agr3'. We have 3 agreements (Agr1 - Agr2 -  Agr3). Right now, the Javascript button is updating all the records in the "Request__c" object, but I want to be able to have the code filter and ONLY update the records where Request__c.agreement__r.name = 'Agr3'

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
var url = parent.location.href; 
var records = {!GETRECORDIDS($ObjectType.Request__c)};
var updateRecords = []; 
if (records[0] == null) { 
alert("Please select at least one record to update."); 
} else { 
for (var a=0; a<records.length; a++) { 
var update_Request__c = new sforce.SObject("Request__c"); 
update_Request__c.Id = records[a]; 
update_Request__c.Due_Date__c = new Date(); 
updateRecords.push(update_Request__c ); 

result = sforce.connection.update(updateRecords); 
parent.location.href = url; 
}


I tried using SOQL query to get only the ids for the records that meets the criteria. 

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
var url = parent.location.href; 
var records = {!GETRECORDIDS($ObjectType.Request__c)}; 
var Agr3Records= sforce.connection.query("SELECT id, Agreement__c  FROM records WHERE Agreement__r.Name = 'Agr3' ");
records = Agr3Records.getArray("records");
for(var i=0; i<records.length; i++)
    {
    var update_Request__c = new sforce.SObject("Request__c"); 
    update_Request__c.Id = records[i]; 
    update_Request__c.Due_Date__c = new Date(); 
    updateRecords.push(update_Request__c ); 
    }
result = sforce.connection.update(updateRecords); 
parent.location.href = url; 
}
Can you please help out. I can't see to figure out the get the code to work without getting errors. Thanks
Best Answer chosen by Kim Adil
Deepak Kumar 138Deepak Kumar 138

i have updated the code to query only the records which are selected on the UI. that will solve the problem.

Here is the updated code.

 

{ !REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{ !REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}
var updateRecords = [];
var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Request__c)};


if (records.length) {
 if (records.length <= 200) {
  var filterString = "( ";
  for (var i = 0; i < records.length; i++) {
   filterString += "'" + records[i] + "'";
   if (i < records.length - 1) {
    filterString += ",";
   }
  }
  filterString += ") ";

  var Agr3Records = sforce.connection.query("SELECT id, Agreement__c FROM Request__c WHERE Agreement__r.Name = 'Agr3' And ID in " + filterString);
  records = Agr3Records.getArray("records");
  for (var i = 0; i < records.length; i++) {
   var update_Request__c = new sforce.SObject("Request__c");
   update_Request__c.Id = records[i].Id;
   update_Request__c.Due_Date__c = new Date();
   updateRecords.push(update_Request__c);
  }
  result = sforce.connection.update(updateRecords);
  parent.location.href = url;
 } else {
  alert('Select 200 or less');
 }
} else {
 alert('Select one or more records');
}

Note - Please mark this a Best answer if it solves your problem. So that it will help others as well.

All Answers

Deepak Kumar 138Deepak Kumar 138
There are some minor errors in your code. i have corrected those, try this - 
 
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")} 
var updateRecords = [];
var url = parent.location.href; 
var records = {!GETRECORDIDS($ObjectType.Request__c)}; 
var Agr3Records= sforce.connection.query("SELECT id, Agreement__c FROM Request__c WHERE Agreement__r.Name = 'Agr3' "); 
records = Agr3Records.getArray("records"); 
for(var i=0; i<records.length; i++) 
{ 
var update_Request__c = new sforce.SObject("Request__c"); 
update_Request__c.Id = records[i].Id; 
update_Request__c.Due_Date__c = new Date(); 
updateRecords.push(update_Request__c ); 
} 
result = sforce.connection.update(updateRecords); 
parent.location.href = url;

Note - This as Best answer if it solves your issue, as it will help others as well.
Kim AdilKim Adil
Hi Deepak,
I really appreciate your prompt answer. I tried your code and I am getting this error now.
A problem with the OnClick JavaScript for this button or link was encountered:

{faultcode:'sf:EXCEEDED_ID_LIMIT', faultstring:'EXCEEDED_ID_LIMIT: record limit reached. cannot submit more than 200 records into this call', detail:{UnexpectedErrorFault:{exceptionCode:'EXCEEDED_ID_LIMIT', exceptionMessage:'record limit reached. cannot submit more than 200 records into this call', }, }, }

How can I break the array to multiple arrays of 200 and call them multiple times? Can you please advise? 
Thank you
Kim
Deepak Kumar 138Deepak Kumar 138

i have updated the code to query only the records which are selected on the UI. that will solve the problem.

Here is the updated code.

 

{ !REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{ !REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}
var updateRecords = [];
var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Request__c)};


if (records.length) {
 if (records.length <= 200) {
  var filterString = "( ";
  for (var i = 0; i < records.length; i++) {
   filterString += "'" + records[i] + "'";
   if (i < records.length - 1) {
    filterString += ",";
   }
  }
  filterString += ") ";

  var Agr3Records = sforce.connection.query("SELECT id, Agreement__c FROM Request__c WHERE Agreement__r.Name = 'Agr3' And ID in " + filterString);
  records = Agr3Records.getArray("records");
  for (var i = 0; i < records.length; i++) {
   var update_Request__c = new sforce.SObject("Request__c");
   update_Request__c.Id = records[i].Id;
   update_Request__c.Due_Date__c = new Date();
   updateRecords.push(update_Request__c);
  }
  result = sforce.connection.update(updateRecords);
  parent.location.href = url;
 } else {
  alert('Select 200 or less');
 }
} else {
 alert('Select one or more records');
}

Note - Please mark this a Best answer if it solves your problem. So that it will help others as well.
This was selected as the best answer
Kim AdilKim Adil
Hi Deepak,
In order to have the code ONLY update the records selected on the UI, and that meets the criteria of "WHERE Agreement__r.Name = 'Agr3'". Shouldn't we be quering the records that meet the criteria in ( var records = {!GETRECORDIDS($ObjectType.Request__c)};
in other words, var records = {!GETRECORDIDS($ObjectType.Request__c)}; will get us all the selected records on the UI, then, our SOQL query should look those selected records and see which ones meet criteria; which means we should use "SELECT id, Agreement__c FROM results" Not, "SELECT id, Agreement__c FROM Request__c"

Can you explain if I approacing the issue with the right logic or not? I will mark the question as solved when I test the new code in a bit

Kim
Deepak Kumar 138Deepak Kumar 138
yeah the logic is right.
if you see the code from line 10 to 17, i am preparing the filter String using Ids selected and using that in the query on line -  19.
Kim AdilKim Adil
Thanks Deepak, you're awesome. I marked your answer as the best answer