Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}'});
share|improve this question
up vote 4 down vote accepted

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

share|improve this answer
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 '13 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? – Andrew Fawcett Jan 22 '13 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 '13 at 23:24
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}'} – Andrew Fawcett Jan 22 '13 at 23:24
ok, thanks! will try that! – user988 Jan 22 '13 at 23:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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