+ Start a Discussion
Zachary Alexander 30Zachary Alexander 30 

Javascript Button: How to convert "GETRECORDIDS" into parent list(s)

I am trying to modify the technique of Deepak Anand found here: https://success.salesforce.com/answers?id=90630000000hW5tAAE. The objective is create a "NEXT" and "PREVIOUS" button for records.

Basically, I've confirmed that this code works:

var cycleAccounts = {!GETRECORDIDS( $ObjectType.ts2__Application__c )};

if(cycleAccounts.length >= 1){
   localStorage['CycleAccounts'] = cycleAccounts;
   location.replace('/' + cycleAccounts[0]);
}


The problem is that I need to get a list of Master-Detail field values instead of ID field values (I'm trying to find the parents, not the records themselves) and I cannot get the following modification of the code to work.

{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
{!REQUIRESCRIPT('/soap/ajax/29.0/apex.js')}
 
var cycleApplications = {!GETRECORDIDS( $ObjectType.ts2__Application__c )};
 
var cycleCandidates = sforce.connection.retrieve('ts2__Candidate_Contact__c', 'ts2__Application__c', cycleApplications);
 
var cycleAccounts = [];
 
var count = 0;
 
if (cycleApplications[0] == null) {
alert(‘Please select at least one record to update.’);
 
} else
 
{
for (var a = 0; a < cycleCandidates.length; a++) {
var desiredID = cycleCandidates[a].ts2__Candidate_Contact__c;
count++;
cycleAccounts.push(desiredID);
}}
 
if(cycleAccounts.length >= 1){
   localStorage['CycleAccounts'] = cycleAccounts;
   location.replace('/' + cycleAccounts[0]);
}


Does anybody know where I am going wrong? It seems to work at first (I am successfully getting the ID of the first parent record), but I do not appear to be getting any other values.
Best Answer chosen by Zachary Alexander 30
Zachary Alexander 30Zachary Alexander 30
Hmm... I never got an answer (but solved it myself a while back).

Recap of the problem:
(1) Three objects (Candidate, Application, Job)
(2) Application is a junction object between Candidate and Job
(3) I needed to be able to select Application records from the Job page, which would take me to Candidate records.
(4) After arriving at the first Candidate record, I needed a "Next" and "Previous" button to go through the list of Candidates (the parents of the Application records I selected).

Anyhoo... incase anyone ever needs to know how to do it, here you go (note that I kept the "Account" language to show the similarity between this modified code and Deepak's original code).

------------------------

Methods Retrieved From:
https://success.salesforce.com/answers?id=90630000000hW5tAAE
https://success.salesforce.com/answers?id=90630000000hiHJAAY
https://developer.salesforce.com/forums/?id=906F00000009ApxIAE
https://www.codecademy.com/en/forum_questions/553685f676b8fe551e000312

Next and Previous Buttons.
Description: Want to go to candidate.
                       List view is on the Job Requisition Object.
                       List view is of applications, which is a child of candidate.

Button to get Application record IDs and store to local:

var cycleAccounts = {!GETRECORDIDS( $ObjectType.ts2__Application__c )};

if(cycleAccounts.length >= 1){
   localStorage['CycleAccounts'] = cycleAccounts;
   location.replace('/' + cycleAccounts[0]);
}

Next Application Button:

if(localStorage['CycleAccounts']){
var cycleAccounts = localStorage['CycleAccounts'].split(',');
var currentAccountPos = cycleAccounts.indexOf('{!ts2__Application__c.Id}');

if(currentAccountPos == cycleAccounts.length - 1){
alert('This was the Last Application!');
}
else{
location.replace('/' + cycleAccounts[currentAccountPos + 1]);
}
}

Previous Application Button:

if(localStorage['CycleAccounts']){
var cycleAccounts = localStorage['CycleAccounts'].split(',');
var currentAccountPos = cycleAccounts.indexOf('{!ts2__Application__c.Id}');

if(currentAccountPos == 0){
alert('This was the First Application!');
}
else{
location.replace('/' + cycleAccounts[currentAccountPos - 1]);
}
}


Button to get Candidate record IDs and store to local:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}

var cycleAccounts = new Array();

//required a custom formula field to grab candidate ID (Candidate_ID__c) instead of referencing the lookup field -- don't know why.
var result = {!GETRECORDIDS($ObjectType.ts2__Application__c)}.toString().replace(/,/g , "','"); 
var myquery = "SELECT Id, Candidate_ID__c FROM ts2__Application__c WHERE Id in ('"+result+"')";
var queryResults = sforce.connection.query(myquery);
var records = queryResults.getArray("records");

for (i=0; i < records.length ; i++)

var child = queryResults.records[i].Candidate_ID__c;

cycleAccounts.push(child);

 }

if(cycleAccounts.length >= 1){
   localStorage['CycleAccounts'] = cycleAccounts;
   location.replace('/' + cycleAccounts[0]);
} 

Next Candidate Button:

if(localStorage['CycleAccounts']){
var cycleAccounts = localStorage['CycleAccounts'].split(',');
var currentAccountPos = cycleAccounts.indexOf('{!Contact.Id}');

if(currentAccountPos == cycleAccounts.length - 1){
alert('This was the Last Application!');
}
else{
location.replace('/' + cycleAccounts[currentAccountPos + 1]);
}
} 

Previous Candidate Button:

if(localStorage['CycleAccounts']){
var cycleAccounts = localStorage['CycleAccounts'].split(',');
var currentAccountPos = cycleAccounts.indexOf('{!Contact.Id}');

if(currentAccountPos == 0){
alert('This was the First Application!');
}
else{
location.replace('/' + cycleAccounts[currentAccountPos - 1]);
}
}