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
Srinivas223Srinivas223 

referring currently logged in user and checking for user name in custom setting in Onclick Javascript button

Hello All,

Hope you had a great weekend!
I have a problem in referring to custom setting data and checking if current logged in user name is there in the custom setting. Sounds simple to me but it seems I am doing some mistake and couldnt get the functionality working.
Here is the sample code
var user = sforce.connection.getUserInfo();
var customSettingUsers ="select name from Opportunity__c where name = '{!User.Name}' limit 1";
data = sforce.connection.query(customSettingUsers); 
var customSettingUsersData = data.getArray("records");
if(customSettingUsersData == '')
alert('Do you have a reason to click me? If yes contact your System Administrator with your reason');

I appreciate your help.

Thanks,
Srinivas​
Best Answer chosen by Srinivas223
Raj VakatiRaj Vakati
Use this code
 
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var user = sforce.connection.getUserInfo();
alert(user)
var customSettingUsers ="select Id,Name from Opportunity__c where Name= \'"+user.userName+"\' limit 1";
alert(customSettingUsers );
data = sforce.connection.query(customSettingUsers); 
var customSettingUsersData = data.getArray("records");
alert(customSettingUsersData)
if(customSettingUsersData == '')
alert('Do you have a reason to click me? If yes contact your System Administrator with your reason');

 

All Answers

Raj VakatiRaj Vakati
Use this code
 
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var user = sforce.connection.getUserInfo();
alert(user)
var customSettingUsers ="select Id,Name from Opportunity__c where Name= \'"+user.userName+"\' limit 1";
alert(customSettingUsers );
data = sforce.connection.query(customSettingUsers); 
var customSettingUsersData = data.getArray("records");
alert(customSettingUsersData)
if(customSettingUsersData == '')
alert('Do you have a reason to click me? If yes contact your System Administrator with your reason');

 
This was selected as the best answer
Srinivas223Srinivas223
Thank you.