+ Start a Discussion

JavaScript error

I get the error "Opportunity is Undefined" when clicking this button. So I am trying to pass in the Opportunity Id so that I can pull information out of that Opprotunity..
Here is the javascript
var accts = sforce.apex.execute("sumTotal","getTotal", {arg:!Opportunity.Id});
Here is the class I am calling
global class sumTotal{
    WebService static double getTotal(Id optid){
    Opportunity opt = [select Id, Name, shipping_method__c, accountid from Opportunity where Id=: optid];
        ShipCode__c code = [select id, Is_USPS__c, USPS_Service_Name__c from ShipCode__c where     USPS_Service_Name__c =: opt.shipping_method__c];
        Account accnt_info = [select shippingpostalcode,billingpostalcode from account where id=:opt.accountid];
        double total_shipping_charge=0;
Ok, so I have tried using an alert for the Opportunity.Id and it gives me the same error. I was assuming that the "Insert Merge field" would give me the Opportunity.Id of the Opportunity that the button was clicked on.. It doesnt seem to give an Id. This button is on the Opportunity, so I want it to pull the Id of that Opportunity. How do I do that?
You are on the right path, but I think a syntax thing has stymied you.

The syntax of a merge field in an s-control is:


So, I think you want:

var accts = sforce.apex.execute("sumTotal","getTotal", {arg:{!Opportunity.Id}});

Substitutions are done when the HTML generator in Salesforce sees "{!".     Note, just to be clearer:
var OppId = "{!Opportunity.Id}";

var accts = sforce.apex.execute("sumTotal","getTotal", {arg:OppId});

I think that if you did a "View Source" and looked at the line in question, you'd be able to see that the substitution wasn't done properly when the code for your page was generated.

Best, Steve.

p.s. I also think your queries might be tighter in your Apex code.  Couldn't you pull the account information in the same query as the Opportunity information?

Message Edited by SteveBower on 11-04-2008 12:18 AM
Thanks for the help. Yes it is the syntax that is getting me, but trying every possible combonation still hasnt made it work. Now it is giving me a "Missing; before statement" error. When you view code it does have an Id in there so i'm not sure why its erroring..


var opt = {!Opportunity.Id};

var accts = sforce.apex.execute("sumTotal","getTotal", {arg:opt});
because you're missing the quotes around the {!Opportunity.Id};
Yep, Thank you!