I made a class that assigns cases to the correct person's queue when they click a button. That button runs some javascript that executes a method in a global class. This button resides on the Case page layout. How can i get the case number of the current case using a query. I tried something like shown below, but it returns null. Is there a way to do this? Any thoughts? Thanks!

c = [SELECT Id FROM Case WHERE Id =: '{!Case.Id}'];

This is my Javascript:

sforce.apex.execute("assignToMyQueue","AssignCaseMethod", {sUserID:"{!User.Id}"}, {caseId:'{!Case.Id}'});

1 Answer 1


While you can do queries from within your JavaScript, I recommend you simply pass the Id to your global class method and then do the query in the Apex code. This encapsulates more of your buttons logic in the server code as well, which is good best practice.

So your JavaScript button code would look something like this...

var result = sforce.apex.execute('CaseWebService', 'assignToMyQueue', {caseId:'{!Case.Id}', userId:'{!User.Id}'});
alert('Result is ' + result);

Your Apex code to retrieve the Case number would then be..

global with sharing class CaseWebService
    webService static String assignToMyQueue(ID caseId, ID userId)
        Case caseRecord = [Select Id, CaseNumber from Case Where Id =: caseId];
        String caseNumber = caseRecord.CaseNumber;
        return 'Case Number is ' + caseNumber + ', User Id is ' + userId;

This results in a Javascript popup showing the Case Number and User Id.

enter image description here

  • I added the Case Id and I'm passing it through now. I think this is exactly what I need, I have something more or less exactly like that. I'm getting an error that says onSuccess not defined in the callback for the button though.
    – user988
    Jan 22, 2013 at 22:35
  • That is strange, I cannot reproduce this, I've updated my sample a little further so you can compare. I notice that your using version 27 of the client libraries. Maybe trying using 26, perhaps there is a bug in the latest? Jan 22, 2013 at 22:54
  • I managed to get it going. I think the problem is I was trying to pass two arguments in. It doesn't like. Question: how can I get the current user logged in from within my class? String sUserID = '{!$User.Id}'; doesn't work.
    – user988
    Jan 22, 2013 at 23:24
  • 1
    Ok I've reproduced your latest issue, you need to pass the parameters as a single JSON object, so try {sUserID:"{!User.Id}", {caseId:'{!Case.Id}'} Jan 22, 2013 at 23:24
  • userId:'{!$User.Id}' though you could more easily not pass the parameter and use UserInfo.getUserId() in Apex. :-) Jan 22, 2013 at 23:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .