I have the following apex method and this method i am going to use in custom detail button but the problem is it will give me an error as following :

enter image description here

Apex Method :

public pagereference StartSync(){
        opp = [Select Id, AccountId from Opportunity where id =: Quote.OpportunityId];

        opp.SyncedQuoteId = Quote.Id;
        opp.Tax__c = Quote.Tax__c;
        opp.Freight_Amount__c = Quote.Freight__c;

        update opp;
        pagereference pg = new Pagereference('/apex/QuoteDetail?id='+this.Quote.Id);
        return pg;
    }Catch(Exception e){
        return null;

Javascript :


Can anybody tell what i am doing wrong in this ?

2 Answers 2


You need to define a webservice static method inside a global class.

Something with this sort of signature:

global class QuoteGroupWebservice {
  webService static String startSync(){}

So you could do something pretty similar to what you have now, except you would need to perform the redirection in javascript.


global class QuoteGroupWebservice {
    webService static String startSync(Id oppId, Id quoteId, Double tax, Double freight){
            opp = [Select Id, AccountId from Opportunity where Id =: oppId];

            opp.SyncedQuoteId = quoteId;
            opp.Tax__c = tax;
            opp.Freight_Amount__c = freight;
            update opp;

            return '/apex/QuoteDetail?id=' + quoteId;

        }catch(Exception e){
            Return ('Error: ' + e.getLineNumber() + ' ' + e.getMessage());

You'd call it like this (omitting much of the error checking, confirmations etc that you'd normally have:

(function () {
  result = sforce.apex.execute(

  if (/*check for exception here*/){

  • thank you !! I did modification according to my scenario and it worked great.
    – Nail David
    Aug 11, 2017 at 10:53

See Apex in AJAX, but in short, you haven't followed the rules for it to work.

You must declare your method as a static webService method. This means your class you should be "global with sharing", and your method should be "webservice static".

global with sharing class QuoteGroupCreateController {
    webService static PageReference StartSync(){

You don't have any access to normal variables, so if you need data, you need to pass that in:

webService static PageReference StartSync(Id quoteId){
    Quote quoteRecord = [select opportunityid from quote where id = :quoteId];

Next, your not in Visualforce, so if you really want to redirect, consider arranging some sort of protocol with your JavaScript instead of a PageReference:

global class StartSyncResponse {
    global Boolean success = false;
    global String message;
    global String redirectURI;
webservice static StartSyncResponse StartSync(Id quoteId) {
    StartSyncResponse response = new StartSyncResponse();
    // rest of code here
    return response;

var result = sforce.apex.execute("QuoteGroupCreateController","StartSync",{ quoteId: "{!Quote.Id}" });
if(result.success) {
    window.top.location.href = result.redirectURI;
} else {
    alert('Error: '+result.message);

Finally, you'll need to clean up your Apex Code's try-catch block to make sure that you're using the right variables and always returning a "result" that you can use in your JavaScript.

  • Thanks for your answer but i am using the same method in custom visualforce page commandbutton. so, does this static method works in my visualforce page commandbutton ?
    – Nail David
    Aug 11, 2017 at 8:50

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.