+ Start a Discussion
Snita LalSnita Lal 

JavaScript Button to Redirect to Related Record


We have created a cutom JavaScript button on Opportunity to tick a check box which kicks off a process. This creates an OCS Contract record and links to the opportunity.

We would like the return URL to redirect to the new related record but we keep getting unidentified indentifier or redirect to the home page.

Our code is below, any help would be very much appreciated! 

if({!Opportunity.Contract_Ready_to_Convert__c} == false) 
var c = new sforce.SObject("Opportunity"); 
c.id = "{!Opportunity.Id}"; 
c.Contract_Ready_to_Convert__c = true; 
result = sforce.connection.update([c]); 
if ( result[0].getBoolean( "success" )) 
window.location.href = ('/'+c.OCS_Contract__c); 
alert( result[0].errors.message); 
alert("A contract already exists for this opportunity"); 

Deepali KulshresthaDeepali Kulshrestha
Hi Snita,

If you want to execute JavaScript when the button is clicked (for your conditional logic), you need to use the "Execute JavaScript" and "OnClick JavaScript" options.

For more information please go through the given links:-

- https://stackoverflow.com/questions/8497442/custom-button-or-link-to-a-visualforce-page-with-a-custom-controller

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha