You need to sign in to do that
Don't have an account?
Kim 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
{!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
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.
Note - Please mark this a Best answer if it solves your problem. So that it will help others as well.
All Answers
Note - This as Best answer if it solves your issue, as it will help others as well.
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
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.
Note - Please mark this a Best answer if it solves your problem. So that it will help others as well.
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
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.