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
Kondal PatchalaKondal Patchala 

unable to get Opportunity record url into java-script

Hi All,
i am trying to create new button(custom survice item) with javascript to autopopulate record type when create child record . so all the work has done and place the button on parent related list layout and its works when click button on view page related list . but have a issue when click on related list summary ( top of the record )

getting url code in java script is 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
var refurl = window.location.href;

i am placing alert as part of debugging , when i click on related list on detail page, refurl stores correct page url , which is expected (https://cs17.salesforce.com/006g0000004UHMh)
  User-added image
User-added image

But when i click on the same button(custom survice item) from the related list summary on detaild page, different url stores in refurl.
User-added image

User-added image

Can anyone please suggest me what is the wrong in the above case or any process or tag to store current page url in java script 

Kind Regards,
Patchala

Best Answer chosen by Kondal Patchala
Grazitti TeamGrazitti Team
Hmm. You wre trying to access an object as a string. Anyways Use following code, it should work:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
alert("hiiiiiiii111111");
var refurl = window.location.href;
if (refurl.indexOf("emptyHtmlDoc") > -1) {
   refurl = window.parent.location.href;
}
alert(refurl);
var stpos = refurl.lastIndexOf("/")+1;
alert(stpos);
var endpos = refurl.length;
alert(endpos);
 
var strOppId = refurl.substring(stpos,endpos);
alert(strOppId);
Appreciate if you mark it as Best Answer if it solves your problem :)



All Answers

Grazitti TeamGrazitti Team
Hi Kondal,

The related list you see in the quick navigation menu renders in an iframe which is the reason why you see different ref url.

You can add a small check in your javascript code for this and get the parent url in both cases as below:


var refurl = window.location.href;
if (refurl.indexOf("emptyHtmlDoc") > -1) {
   refurl = window.parent.location;
}
alert(refurl);

Let me know if you need any other help.

 
Kondal PatchalaKondal Patchala
Hi Grazitti ,

Thank for your reply, after inserting the code you give is work fine and it bring parent url for both cases but i am facing another issue after refurl alert, when click the button through quick navigation menu , please find bellow for new error and i am posting complete code where i am bring opp id from url.

User-added image

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
alert("hiiiiiiii111111");
var refurl = window.location.href;
if (refurl.indexOf("emptyHtmlDoc") > -1) {
   refurl = window.parent.location;
          }
alert(refurl);
var stpos = refurl.lastIndexOf("/")+1;
alert(stpos);
var endpos = refurl.length;
alert(endpos);
 
var strOppId = refurl.substring(stpos,endpos);
alert(strOppId);
}

Regards,
Patchala

Grazitti TeamGrazitti Team
Hmm. You wre trying to access an object as a string. Anyways Use following code, it should work:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
alert("hiiiiiiii111111");
var refurl = window.location.href;
if (refurl.indexOf("emptyHtmlDoc") > -1) {
   refurl = window.parent.location.href;
}
alert(refurl);
var stpos = refurl.lastIndexOf("/")+1;
alert(stpos);
var endpos = refurl.length;
alert(endpos);
 
var strOppId = refurl.substring(stpos,endpos);
alert(strOppId);
Appreciate if you mark it as Best Answer if it solves your problem :)



This was selected as the best answer