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
Maha Kothuri 7Maha Kothuri 7 

Java Script button trying to get username and profile

Hello,
I am trying to get the profile and username in JavaScript button but I am getting an error when trying to click on the button
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var currentuser = sforce.connection.getUserInfo();
if('{!Contract_Approval_Form__c.Approval_Status__c}' == 'Approved') { alert('{!$Profile.Name}') if('{!$User.Id}' == '005E00000088akt') { window.open("/apex/echosign_dev1__AgreementTemplateProcess?masterid={!Contract_Approval_Form__c.Id}&templateId=aH0V0000000CcVSKA0 ","_self"); }else if('{!$User.Id}' == '005E0000001noF2IAI') { window.open("/apex/echosign_dev1__AgreementTemplateProcess?masterid={!Contract_Approval_Form__c.Id}&templateId=aH0V00000008PXg ","_self"); }else if('{!$Profile.Id}' == '00eE0000000eOCd') { window.open("/apex/echosign_dev1__AgreementTemplateProcess?masterid= {!Contract_Approval_Form__c.Id}&templateId=aH0V00000008PXl ","_self"); } }else { window.alert("Please make sure Approval Status to be Approved"); }
When ever I add else if('{!$Profile.Id}' == '00eE0000000eOCd') condition , button is failing .Can you please let me know what I am missing
NagendraNagendra (Salesforce Developers) 
Hi Maha,

Sorry for this issue you are facing.

If I do the following (in VisualForce) I get my current users' Id logged to the console
<apex:page>
     <script src="../../soap/ajax/29.0/connection.js" type="text/javascript"></script>
     <script src="../../soap/ajax/29.0/apex.js" type="text/javascript"></script>
     <script type="text/javascript">
          sforce.connection.sessionId = "{!$Api.Session_ID}";
         console.log(sforce.connection.getUserInfo().userId);
     </script>
</apex:page>
But as you said you are using this as JavaScript in a button you may need to add the sforce.connection.sessionId and tweak the code as per the need.
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}

sforce.connection.sessionId = "{!$Api.Session_ID}}";
var records = result.getArray("records");
var f = new sforce.SObject("Appointment__c");
f.id = "{!Appointment__c.Id}";
f.Fini_Status__c = "Appt Canceled";
var user = sforce.connection.getUserInfo();
f.Cancelled_By__c = user.userId;
f.Cancelled_Date_Time__c = new Date().toISOString();
sforce.connection.update([f]);
Thanks,
Nagendra