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
Mahendar ManupatiMahendar Manupati 

i am not able to get the current user id on javascript button

Hi ,
Here is the code for javascript button.
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
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.getuuserid;
f.Cancelled_Date_Time__c=new Date().toISOString();
sforce.connection.update([f]);
Best Answer chosen by Mahendar Manupati
pconpcon
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>

I am guessing that you are using this as JavaScript in a button.  So you may need to add the sforce.connection.sessionId line such as

{!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]);


All Answers

pconpcon
You should have:

f.Cancelled_By__c = user.userId;

that should get you the user's id.
Mahendar ManupatiMahendar Manupati
Hello Pcon,


I am not getting the userid.I have tried in my code.
Thanks for the reply.
pconpcon
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>

I am guessing that you are using this as JavaScript in a button.  So you may need to add the sforce.connection.sessionId line such as

{!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]);


This was selected as the best answer
Mahendar ManupatiMahendar Manupati
Hello pcon,

Now its working without  including "sforce.connection.sessionId".
Working only when ........      "f.Cancelled_By__c = sforce.connection.getUserInfo().userId"
Thanks for the help.
R R MR R M
HI folks, 
i am facing same kind of issue. 
actually am trying to capture Userid and Datetime, 
Its working fine for SystemAdministrator profile but not working for custom profile users(Not capturing UserId and DateTime).
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")} 
var x; 
if (confirm("Are you Sure! You want to Activate this Customer?") == true) { 
x = "Yes"; 
} else { 
x = "Cancel"; 
} 
if(x == 'Cancel'){ 
window.location.replace('/{!Customer__c.Id}') 
} 
else { 
var journystatus = "SELECT id,Customer_Status__c FROM Customer__c WHERE Id = '{!Customer__c.Id}' limit 1"; 

var records = sforce.connection.query(journystatus); 
var values = records.getArray('records'); 
var status = values[0].Customer_Status__c ; 

if(status == 'Approved') 
{ 
var result = sforce.apex.execute("CustomerStatusChanginginDBUsingAPI","StatusChange", {extrnlid:"{!Customer__c.Customer_External_Id__c}", 
CusJurSt:"Active"}); 
location.reload(); 
} 
else 
{ 
alert('Cannot change the customer status as customer already in ACTIVATED/REFFERED/REJECTED state'); 
} 
var f = new sforce.SObject("Customer__c"); 
f.id = "{!Customer__c.Id}"; 

var user = sforce.connection.getUserInfo(); 
f.Activated_By__c = "{!$User.Username}"; 
f.Activation_Date__c = new Date().toISOString(); 
sforce.connection.update([f]); 
}

 please help.
Thanks In Advance