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
apexsutherlandapexsutherland 

Better way to convert !GETRECORDIDS output to quoted string for SOQL query

I am using the !GETRECORDIDS merge function to get record Ids from a related list and query SFDC for additional information before doing further processing. However, this is the only way that I could get the output of the !GETRECORDIDS function to be palatable for the WHERE IN part of my SOQL query:

Code:
idArray = {!GETRECORDIDS($ObjectType.Quoted_Asset__c )};


if (idArray[0] == null) 
{ 
alert("Please select at least one row");
} 
else 
{ 

var idString = "";

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

alert(idString);

var qaRecords = sforce.connection.query("Select Opportunity__c, Asset__c From Quoted_Asset__c Where Id in (" +idString+")");

Does anyone have a cleaner way to put the single quotes and commas in the string of Ids than the for loop that I wrote? I tried using the .toString() method, but it doesn't provide the single quotes around the Ids when it concatenates them with the rest of the SOQL query string.

sfdcfoxsfdcfox
It's an array, so I don't see why you can't end up with:

Code:
idValues = {!GetRecordIds($ObjectType.Quoted_Asset__c)}
queryPart = "'" + idValues.join("','") + "'"

There you go. Instant query generation for a list of values from a list view. And yes, it could probably be shorter, but I did want it to still be readable by the time I was done.

sfdcfoxsfdcfox
But if you're retrieving values from the objects those IDs represent, why wouldn't you use:

Code:
quoteIds = {!GetRecordIds('Quoted_Asset__c')}
if(quoteIds.length < 1)
{ alert('Please select at least one row')
}
else
{ Quotes = sforce.connection.retrieve('Opportunity__c, Asset__c','Quoted_Asset__c',quoteIds)
  // Do stuff with Quotes here.
}

Generally, you only need to use query when you don't know the ID values, or you need data from related records, and you don't have their ID values.

apexsutherlandapexsutherland
Thanks sdfcfox, that latest code you posted worked beautifully. I hadn't ever tried the 'retrieve' command before. Thanks for introducing it to me!! I'll have to go back and re-write a bunch of s-controls now...