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