I am trying to pass the record ID from a button, clicked on a custom object, in salesforce, to both an APEX class and to a visual force page...

My button has the following code:

var visualForcePage = sforce.apex.execute("TestController", "getID", {myId : '{!ECS__RMA__c.Id}'});
var informationFetch = sforce.apex.execute("TestLabel", "execute", {myId : '{!ECS__RMA__c.Id}'});

On the TestLabel visualforce page I have the following code:

<apex:pageBlock title="Select return address for {!RMA}">

In the controller I have:

(to get the Id of the record the button was clicked on)

webservice static void getID(Id myId){
    myIdclone = myId;

(and to get the text associated with RMA)

public static string getRMA(){
    System.debug('ID: ' + myIdclone);
    TestMethods job = new TestMethods();
    String rmaNumber = TestMethods.getRMA(myIdclone);
    return rmaNumber;

On TestMethods I have:

public static string getRMA(Id recordId){
    sObject rmaSOBJECT = [SELECT Name FROM ECS__RMA__c WHERE Id =: recordId];
    String rmaNumber = (String)rmaSOBJECT.get('Name');
    return rmaNumber;

My error is:

18:11:04:055 FATAL_ERROR System.QueryException: List has no rows for assignment to SObject

18:11:04:052 USER_DEBUG [16]|DEBUG|ID: null

I believe my problem is coming from the deletion of the cache of the variables upon code execution. My question is: How can I get getRMA on the controller to properly use the myID variable previously assigned by the javascript?

I apologize if this code is cluttered, I am fairly novice at apex visualforce programming.

  • Question is bit unclear to me. Where are you getting these errors? Test classes? Jan 11, 2016 at 23:36
  • Sorry. These errors are coming from the controller and it's communication with the method class. In the above code assume 'Test' anywhere just means any word, there is no test class yet I just picked a random name. So essentially getRMA() from the controller class is executing the getRMA() method in the method class but myIdClone is null for some reason which is what I am trying to solve.
    – JMK
    Jan 11, 2016 at 23:39

2 Answers 2


When you are redirecting the page on "onclick" event of button, it flushes the earlier viewstate and creates new.

Standard approach to implement this

window.open('/apex/TestLabel?rmaID'+ visualForcePage);
//VisualforcePage is your variable in the code

Now when this new page will load, it will have the query parameter associated in the URL. You can get these parameters in your controller associated with the page and resolve values.


Each call to sforce.apex.execute is a separate transaction-- your variables are getting wiped out after each call; ultimately, when you open the new page, it also has null values associated. You need to just call your page directly:

window.open('{!URLFOR($Page.TestLabel, ECS__RMA__c.Id, [id=ECS__RMA__c.Id])}');

Your page will be called with the appropriate parameters:


Then, your TestLabel page should either use a StandardController or read the parameter from the URL via ApexPages.currentPage().getParameters().get('id').

You must log in to answer this question.

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