function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Snita LalSnita Lal 

JavaScript Button to Redirect to Related Record

Hi,

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

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); 
} 
else 
{ 
alert( result[0].errors.message); 
} 
} 
else 
{ 
alert("A contract already exists for this opportunity"); 
}
Thanks,

Snita
 
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:-

-http://salesforce-stuff.blogspot.com/2011/11/visualforce-related-list-button.html
- 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