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
Varun AnnadataVarun Annadata 

how to access multiple values from a custom setting in javascript on click button?

{!REQUIRESCRIPT("/xdomain/xdomain.js")}
{!REQUIRESCRIPT("/soap/ajax/39.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/39.0/apex.js")}
{!REQUIRESCRIPT("/support/console/39.0/integration.js")}

var Program='{!Program_Member_MVN__c.Program_Name__c}';
var Pname=sforce.connection.query("Select Program__c from Program_Names__c");
var rec16 = Pname.getArray('records');
var status='{!Program_Member_MVN__c.Status_MVN__c}';
var PmId='{!JSENCODE(Program_Member_MVN__c.Id)}'; 
var PName='{!Program_Member_MVN__c.Program_Name__c}'; 
var url;
if (sforce.console.isInConsole()) {
if(Program == rec16){
alert("You are on the wrong Program")
} 

else if(status == 'On Therapy')
{
url = "/apex/Milestone_skip?id="+PmId;
window.open(url,'_blank','toolbar=0,location=0,menubar=0,width=800,scrollbars'); 
}  
else
{
alert("Patient must be ‘On-Therapy’ before skipping to a milestone");
}
}

This is my java script on click button code.In this i am querying  custom setting here:var Pname=sforce.connection.query("Select Program__c from Program_Names__c");
And i want to compare to field to show a alert.So i have to multiple values in custom setting.So how can i access all the values given in the custom setting in the java script code?
Best Answer chosen by Varun Annadata
Sukanya BanekarSukanya Banekar
why are you using for loop? Follow below code
 
{!REQUIRESCRIPT("/xdomain/xdomain.js")}
{!REQUIRESCRIPT("/soap/ajax/39.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/39.0/apex.js")}
{!REQUIRESCRIPT("/support/console/39.0/integration.js")}


var Program='{!Program_Member_MVN__c.Program_Name__c}';
var status='{!Program_Member_MVN__c.Status_MVN__c}';
var PmId='{!JSENCODE(Program_Member_MVN__c.Id)}'; 
var url;
var Pname=sforce.connection.query("SELECT Stage_Name__c 
FROM Program_Names_del1__c");
var record = Pname.getArray("records");

var json = JSON.stringify(record).replace(/^\[(.+)\]$/,'$1');
var obj = JSON.parse(json);
if (sforce.console.isInConsole()) {
if(obj.Stage_Name__c == Program ){
alert("You are on the wrong Program");
} 
else if(status == 'On Therapy')
{
url = "/apex/Milestone_skip?id="+PmId;
window.open(url,'_blank','toolbar=0,location=0,menubar=0,width=800,scrollbars'); 
}  
else
{
alert("Patient must be ‘On-Therapy’ before skipping to a milestone");
}
}


 

All Answers

Sukanya BanekarSukanya Banekar
Hi Varun ,
The result you get in var rec16 i.e(var rec16 = Pname.getArray('records')) is in json format you need to parse it and can easily get the field value one by one for e.g.
 
var Pname=sforce.connection.query("Select Program__c, field2, field3 from Program_Names__c");
var rec16 = Pname.getArray('records');
var json = JSON.stringify(rec16).replace(/^\[(.+)\]$/,'$1')
var obj = JSON.parse(json);
if(obj.Program__c == '')
alert()
else if(obj.field2 == '')
alert ()

I hope this will help you to solve your problem

Thanks,
Sukanya Banekar
Varun AnnadataVarun Annadata
{!REQUIRESCRIPT("/xdomain/xdomain.js")}
{!REQUIRESCRIPT("/soap/ajax/39.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/39.0/apex.js")}
{!REQUIRESCRIPT("/support/console/39.0/integration.js")}


var Program='{!Program_Member_MVN__c.Program_Name__c}';
var status='{!Program_Member_MVN__c.Status_MVN__c}';
var PmId='{!JSENCODE(Program_Member_MVN__c.Id)}'; 
var url;
var Pname=sforce.connection.query("SELECT Stage_Name__c 
FROM Program_Names_del1__c");
var record = Pname.getArray("records");
for (var i=0; i< record.length; i++)
    var record = records[i].Stage_Name__c;
var json = JSON.stringify(record).replace(/^\[(.+)\]$/,'$1');
var obj = JSON.parse(json);
if (sforce.console.isInConsole()) {
if(obj == Program ){
alert("You are on the wrong Program");
} 
else if(status == 'On Therapy')
{
url = "/apex/Milestone_skip?id="+PmId;
window.open(url,'_blank','toolbar=0,location=0,menubar=0,width=800,scrollbars'); 
}  
else
{
alert("Patient must be ‘On-Therapy’ before skipping to a milestone");
}
}

Hi sukanya banekar

I have to compare var program to what ever is there in custom settings.So i have changed my code as above.Is it the correct approach?And i am getting unexpected or invalid token error.How to solve that error?
Sukanya BanekarSukanya Banekar
why are you using for loop? Follow below code
 
{!REQUIRESCRIPT("/xdomain/xdomain.js")}
{!REQUIRESCRIPT("/soap/ajax/39.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/39.0/apex.js")}
{!REQUIRESCRIPT("/support/console/39.0/integration.js")}


var Program='{!Program_Member_MVN__c.Program_Name__c}';
var status='{!Program_Member_MVN__c.Status_MVN__c}';
var PmId='{!JSENCODE(Program_Member_MVN__c.Id)}'; 
var url;
var Pname=sforce.connection.query("SELECT Stage_Name__c 
FROM Program_Names_del1__c");
var record = Pname.getArray("records");

var json = JSON.stringify(record).replace(/^\[(.+)\]$/,'$1');
var obj = JSON.parse(json);
if (sforce.console.isInConsole()) {
if(obj.Stage_Name__c == Program ){
alert("You are on the wrong Program");
} 
else if(status == 'On Therapy')
{
url = "/apex/Milestone_skip?id="+PmId;
window.open(url,'_blank','toolbar=0,location=0,menubar=0,width=800,scrollbars'); 
}  
else
{
alert("Patient must be ‘On-Therapy’ before skipping to a milestone");
}
}


 
This was selected as the best answer
Varun AnnadataVarun Annadata
hi sukanya banekar
I am getting invalid or unexpected token error.I think syntax is correct and all flower brackets are closed still i am getting the error.What may be the issue?Thank you for the reply.
Sukanya BanekarSukanya Banekar
can you put alerts to print the data for e.g
alert('record----'+record)
alert('json----'+json)
alert('obj.Stage_Name__c----'+obj.Stage_Name__c)
alert('Program----'+Program);

Try to debug the code. and let me know the result.

Thanks,
Sukanya Banekar
Varun AnnadataVarun Annadata
Hi sukanya banekar 
Thank you for all your replies.It is now working:)