2

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(){
    try{
        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);
        pg.setRedirect(true);
        return pg;
    }Catch(Exception e){
        return null;
    }
}

Javascript :

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 
sforce.apex.execute("QuoteGroupCreateController","StartSync",{});

Can anybody tell what i am doing wrong in this ?

2 Answers 2

3

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.

Eg:

global class QuoteGroupWebservice {
    webService static String startSync(Id oppId, Id quoteId, Double tax, Double freight){
        try{
            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(
   'QuoteGroupWebservice',
   'startSync',
   {'quoteId':'{!Quote.Id}',
    'oppId':'{!Quote.OpportunityId}',
    'tax':'{!Quote.Tax__c}',
    'freight':'{!Quote.Freight__c}'
    });

  if (/*check for exception here*/){
    return;
  }
  window.open(window.open(url););

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

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;
}

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 
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.

1
  • 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

You must log in to answer this question.

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