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
Nitish AgrawalNitish Agrawal 

Unexpected Identifier Error - Custom Button to invoke Apex Code

Hi All,

I am trying to invoke Apex code from custom button on a custom object (Trainee__c) page layout. I am getting error : Unexpected Identifier when I am passing the name field of the custom object in he parameter to Apex method. Following is the code:
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

sforce.apex.execute("PopulateTraineeModule","PopulateTraineeModuleData",
{TraineeName:{!Trainee__c.Name});

alert('Module Data for {!Trainee__c.Name} refreshed');
If I replace  {!Trainee__c.Name} with a hard coded value.. Say 'Atticus Finch' the logic is working fine.
I am also able to see the correct alert message with {!Trainee__c.Name}

Not sure where the problem is.
Following is my Apex Class and method
global class PopulateTraineeModule {

    
    webservice static void PopulateTraineeModuleData (String TraineeName )
    {
...
     }
}




alert('Module Data for {!Trainee__c.Name} refreshed');


 
Best Answer chosen by Nitish Agrawal
Hemant_JainHemant_Jain
Can you try adding the parameter in double/single quotes, like {TraineeName:"{!Trainee__c.Name}"); or {TraineeName:'{!Trainee__c.Name}');

If that doesn't work, declare a variable and pass that variable. See below example:
 
var name = "{!Trainee__c.Name}";
alert(name);
sforce.apex.execute("PopulateTraineeModule","PopulateTraineeModuleData",{TraineeName:name});

 

All Answers

Hemant_JainHemant_Jain
Can you try adding the parameter in double/single quotes, like {TraineeName:"{!Trainee__c.Name}"); or {TraineeName:'{!Trainee__c.Name}');

If that doesn't work, declare a variable and pass that variable. See below example:
 
var name = "{!Trainee__c.Name}";
alert(name);
sforce.apex.execute("PopulateTraineeModule","PopulateTraineeModuleData",{TraineeName:name});

 
This was selected as the best answer
Nitish AgrawalNitish Agrawal
Thanks Hemant,
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

var name = "{!Trainee__c.Name}";
sforce.apex.execute("PopulateTraineeModule","PopulateTraineeModuleData",{TraineeName:name});

alert('Module Data for ' +name +' is refreshed');

This code isn't throwing any errors. However, I am still not getting the desired outcome. 

I am able to get the outcome if I hardcode the parameter value in the called method body and execute through debug. 

Is there any way by which I can see the complete processing in debug view or joblog kind of thing.. Just wondering? Wanted to see what valus is being received by my method (I am new to salesforce)

Button Click>>Javascript>>Apex Method 

 
Hemant_JainHemant_Jain
Just try to put alerts in JavaScript and use debugs in Apex to identify the problem