Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a button on the opportunity detail page which currently directs my user to a visualforce wizard. However, if the user has already been through the wizard for that opportunity, I want it to take them straight to the contract they created with the wizard last time.

Initially I tried checking whether a contract existed where my opportunity Id matched an Id from a custom lookup field on Contract in the controller constructor, but apparently you aren't allowed to do that from constructors (using PageReference anyway, I don't know another way).

So I decided to try and add the logic in the button javascript, but my knowledge of js has a lot of holes in, so I'm asking here does anyone know what's wrong with this? The error I'm getting says "Unexpected identifier" and nothing more. It doesn't tell me what the identifier is.

Here's the code:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var qr = sforce.connection.query("SELECT Id FROM Contract WHERE Opportunity__c.Id = '" + "{!Opportunity.Id}" + "' LIMIT 1");
if (qr.size() == 1) 
{
    window.location.href("/800" + ((Contract)qr[0]).Id + "/e");
} else {
    window.location.href("/apex/CreateContract?oid=" + "{!Opportunity.Id}");
}
share|improve this question

2 Answers 2

I see a couple immediate problems:

  1. window.location.href is a property with a special setter, not a function:

    //instead of this
    window.location.href('/800...');
    
    //do something like this
    window.location.href = '/800...';
    
  2. JavaScript is untyped; there is no need to attempt to cast objects returned by the sforce API:

    //instead of this
    "/800" + ((Contract)qr[0]).Id + "/e"
    
    //try something like this
    "/800" + qr[0].Id + "/e"
    
  3. Counting the items in an array for JavaScript is different from Apex!

    //instead of this
    qr.size() == 1
    
    //try something like this
    qr.length == 1
    

Worth checking out a quick refresher or crash course in JavaScript like codecademy!

share|improve this answer
    
Thanks :) They fixed the errors, but it still doesn't seem to go to the right page. Also the Opportunity__c should have been Opportunity__r. –  Adam Apr 24 at 15:56

I'm pretty sure its

((Contract)qr[0]).Id

Contract is not declared in the JS fucntion. It would work if you put it in a controller, because SF knows Contract is an object.

If you are trying to redirect them to the contract edit page, you can create a second query that will get the contract ID and pass that into the if statement.

if (qr.length > 0)
conID = 'query here'
window.location.href("/800" + conID + "/e");
else
....

also refer to this: http://stackoverflow.com/questions/14202601/array-size-vs-array-length

Be sure to use length not size in JavaScript.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.