Is there a way i can get a currently logged in user in a custom button running on javascript?

I have custom button on where i am trying to update certain fields like activated by. Is there a way i can get in a currently logged in user in the javascript?





var retVal = confirm("Are you sure?");
var OrderObj = new sforce.SObject("Order__c");
OrderObj.id = '{! Order__c.Id}'; /* Need Id field to update Order*/
OrderObj.Status__c = 'Activated'; /* Change status */
OrderObj.Activated_By__c= {!User.Id};

/* update method takes an array of Orders; init to 1 element - 'OrderObj' */
var result = sforce.connection.update([OrderObj ]);

if (result[0].success == 'false') {
else {
location.reload(true); /* redisplay the detail page */

Here is the error

enter image description here


You can use a global User object:

<a href="/{!$User.Id}">{!$User.Alias}</a>

In a custom button set the Behavior to "OnClick JavaScript", add following javascript location.href = "/{!$User.Id}" and add the button to the page layout:

enter image description here

  • No idea how to use it in Custom Javascript button – kurunve Mar 23 '16 at 13:43
  • @kurunve Check my update – Sergej Utko Mar 23 '16 at 13:49
  • @SergeyUtko i tried using the global User variable in the custom button, but it was throwing an error "Illegal" I couldnt make anything out of that error – Prady Mar 23 '16 at 14:07
  • 1
    @Prady, could you post your code? That example works for me as well – kurunve Mar 23 '16 at 14:18
  • 1
    @Prady just do OrderObj.Activated_By__c= "{!User.Id};" to ensure that those assigned variable is string – kurunve Mar 24 '16 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.