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
Umair Khan 19Umair Khan 19 

java script -how open the record inserted in same page

I created a custom button on the account detail page and when I click the button a new contact record is created using account data and the page is reloaded. I want to open the contact page after it is inserted,.

This is what i tried
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}

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

var con=new sforce.SObject("contact");
con.Lastname='{!Account.Name}';
con.accountId='{!Account.Id}';
result = sforce.connection.create([con]);
window.location.reload();


and this is for referring the page but is showing Undefined.
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}

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

var con=new sforce.SObject("contact");
con.Lastname='{!Account.Name}';
con.accountId='{!Account.Id}';
result = sforce.connection.create([con]);

window.location.assign("https://ap15.salesforce.com/"+con.id);


 
Best Answer chosen by Umair Khan 19
Khan AnasKhan Anas (Salesforce Developers) 
Hi Umair,

Greetings to you!

Just add below line in your code:
window.location = "/"  + result[0].id;

Please try below code:
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}
 
var con=new sforce.SObject("contact");
con.Lastname='{!Account.Name}';
con.accountId='{!Account.Id}';
result = sforce.connection.create([con]);

window.location = "/"  + result[0].id;

If you want to open in edit mode then you need to add + "/e"
window.location = "/" + result[0].id + "/e";


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Umair,

Greetings to you!

Just add below line in your code:
window.location = "/"  + result[0].id;

Please try below code:
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}
 
var con=new sforce.SObject("contact");
con.Lastname='{!Account.Name}';
con.accountId='{!Account.Id}';
result = sforce.connection.create([con]);

window.location = "/"  + result[0].id;

If you want to open in edit mode then you need to add + "/e"
window.location = "/" + result[0].id + "/e";


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Umair Khan 19Umair Khan 19
Thank You so much khan anas.
It worked!!    :)