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
finalistfinalist 

Refreshing Lead Detail from Console or directly, after update via S-Control

Hi everyone,

I've posted about similar issues before, and searched the forums, and I apologize if there's a solution that I've missed in here somewhere; here's the problem:

From a Lead (or anywhere, really, but let's keep it simple), I have custom buttons triggering S-Controls that
  • update fields on the lead, and
  • create new custom objects with lookups to the lead
A page is displayed with a brief comment indicating what's happening in the background, and then the user is returned to the original page so that they can continue with what they're doing. 

The Lead detail is not refreshed, however, so there is no indication that anything really happened.  I've tried using setTimeout() to give the page time to refresh, to no avail.

I've also tried to determine whether the user was in the Console at the time they clicked the button or whether they were on the Leads tab, by checking whether window.parent.location.href == "https://(server).salesforce.com/ui/desktop/DesktopPage", but this isn't working either.

I am using dojo, and opener.location.reload is returning them to the right place, but not refreshing the page (as I mentioned.)

Here are the two relevant bits of code; the first, an attempt to identify whether it's the console or not:
Code:
          if (window.parent.location.href=="https://na3.salesforce.com/ui/desktop/DesktopPage") {
                alert("Opened from console ...");
                finishedLocation = '/{!Lead.Id}—isdtp=mn';
          } else {
                alert("Opened from leads tab ...");
                finishedLocation = '/{!Lead.Id}';
          }
  // followed by
          window.location = finishedLocation

and the second, to refresh the lead detail, irrespective of the location:
Code:
/* I am not using both this code AND 
   window.location = finishedLocation; since finishedLocation wasn't
   doing its job, I've just been using this:

*/ // do the processing that needs to be done, then setTimeout("updateComplete()", 750); } function updateComplete() { opener.location.reload; //opener.focus(); // window.parent.location.reload; // parent.parent.frames.location.replace("/{!Lead.Id}"); }

Right now, setTimeout() is only serving the purpose of displaying a page with the message "Logging voicemail ..." or something
to that effect, leaving enough time for the message to be read and then returning to the lead detail.

Thanks for your help! :)
 

werewolfwerewolf
Opener.location.reload or window.location.reload should work, have you run it with firebug to ensure that your opener variable is not null?

Remember that the console is framed, and the opener may in fact be a frame and not a whole window.  If you want to check whether the user is in the console, look for the isdtp=mn parameter in the frame's URL.
finalistfinalist
Thanks for the prompt reply!

As to your question re: Firebug, no, I hadn't specifically looked at opener to see if there was a value.  Is there a better/more precise way of 'phrasing' the 1st bit of code to determine the URL, i.e. whether it's called from the Console?
C
Code:
if (window.parent.location.href=="https://na3.salesforce.com/ui/desktop/DesktopPage") {
                alert("Opened from console ...");
                finishedLocation = '/{!Lead.Id}—isdtp=mn';
          } else {
                alert("Opened from leads tab ...");
                finishedLocation = '/{!Lead.Id}';
          }
  // followed by
          window.location = finishedLocation

 Any (more :) ) help would be appreciated.

werewolfwerewolf
Well, as I said, the opening page probably isn't DesktopPage -- that's the frameset.  The opening page will still be the lead page but with an isdtp=mn parameter.
werewolfwerewolf
and that would be more like /{!Lead.Id}?isdtp=mn.  You have a dash or something there.
finalistfinalist
So if the code went more like
Code:
if (window.parent.location.href=="https://na3.salesforce.com/" & {!Lead.Id} & "?isdtp=mn") 
   {
       ... something to do with the console
   } else {
       ... something to do with the Lead detail directly
   }

 I might have better luck?   (the errant - was supposed to be a ?; something weird with the paste-job there)
werewolfwerewolf
I would probably just use an indexOf to see if the URL contains "isdtp=mn".  If you try to match on the full URL and your org ever gets split to another server (say, na7 or na8) then your Scontrol will stop working.
finalistfinalist
I'll try, if I can find an example of that -- I have never used indexOf, and a quick search returns so many different types of uses that it isn't very helpful.

I do have an O'Reilly guide to JavaScript, so hopefully it's in there ...
werewolfwerewolf
It's all about the w3schools, best JavaScript reference on the web:

http://www.w3schools.com/jsref/jsref_indexOf.asp
finalistfinalist

Okay - it is working now.  It does not have the (necessary) elegance suggested above for using index.of, so it will need to be modified if the server is migrated, but otherwise this does the job.

To refresh your memory, this is called from a button on the Contact record, and creates a new Lead from the Contact.  Based on whether it is called from within the Console or not, the new Lead is loaded either full-screen or in the Console window.

Thanks for your help!

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
  <link  href="/sCSS/Theme2/en/common.css" media="handheld,print,projection,screen,tty,tv" rel="stylesheet" type="text/css" />
  <script type="text/javascript" language="javascript" src="/soap/ajax/11.1/connection.js"></script>
  <script language="javascript">

    function displayError(error) {

         // displays a user-friendler error message when an error occurs
         alert("An error was encountered while creating the Lead record \n" + error);
    }

     var baseURL = "https://na3.salesforce.com/";
     var finishedLocation;
     var newLead = new sforce.SObject("Lead");

    function createNewLeadRecord() {
       try {
              var currId = '{!Contact.Id}';
              var acctName = '{!Account.Name}';

              // Set the Lead record type to 'Standard' (default behaviour; no code required)

              // pass Contact field values to Lead

              newLead.set("FirstName","{!Contact.FirstName}");
              newLead.set("LastName","{!Contact.LastName}");
              newLead.set("Company", acctName);
              newLead.set("Title","{!Contact.Title}");
              newLead.set("MobilePhone","{!Contact.MobilePhone}");
              newLead.set("Phone","{!Contact.Phone}");
              newLead.set("Email","{!Contact.Email}");

              newLead.set("City","{!Contact.MailingCity}");
              newLead.set("Country","{!Contact.MailingCountry}");
              newLead.set("PostalCode","{!Contact.MailingPostalCode}");
              newLead.set("State","{!Contact.MailingState}");
              newLead.set("Street","{!Contact.MailingStreet}");
              newLead.set("LeadSource","{!Contact.LeadSource}");

            } catch(ex) {

                displayError(ex);

            }
       // end function createNewLeadRecord()
       };

       function saveNewLead() {

             try {
                    // save new record
                    saveResult = sforce.connection.create([newLead]);
                    var LeadId = saveResult[0].id;
             
                   // determine if call was made from the Console (/desktop/DesktopPage) or not
                  if(window.parent.location.href=="https://na3.salesforce.com/ui/desktop/DesktopPage") 
                      { 
                         finishedLocation = baseURL  + LeadId  + "\?isdtp=mn";
// Note: the "\? in the line above is an attempt to escape the question mark,
// which was being replaced by a -. Leave the \ mark out in actual code
 } else { finishedLocation = baseURL + LeadId; }; } catch(ex) { displayError(ex); } // end function saveNewLead() }; createNewLeadRecord(); saveNewLead(); loadLead(); close(); function loadLead() { try { window.location = finishedLocation; } catch(ex) { displayError(ex); } }; </script> </head> <body> </body> </html>

 




Message Edited by finalist on 07-30-2008 02:02 PM

Message Edited by finalist on 07-30-2008 02:03 PM
JustinCJustinC
I know this is an older thread, but it helped me and will likely keep helping others.

I've written a javascript function that will check if you are in the console that works between servers (using indexOf, as suggested earlier)

Code:
// checks if the window's parent URL is running under the console
// calls isConsoleCheckURL()
function isConsole () {
    return isConsoleCheckURL(window.parent.location.href);
}

// checks if the given url is running under the console
function isConsoleCheckURL (url) {
    if (url.indexOf('isdtp=mn') == -1)
        return false;
    else
        return true;
}

 
You could combine these two into the same function if you prefer.

Then before you call your redirection code you check if the user is in the console and add the appropriate 'isdtp=mn' to the query string.

Example:
Code:
if (isConsole()) {
    redirect('/' + leadid + '?isdtp=mn');
}
else {              
    redirect('/' + leadid);
}

Note: redirect() is a function I created for handling re-directs, obviously you'd use whatever your redirect code is there. I use:
Code:
function redirect (url) {
 if (url != '') {
     parent.frames.location.replace(url);
 }
}
 
Note: This console ditection code only works inside an S-Control, not inside VisualForce pages that are embedded into the Page Layout (through an iframe). That's because the Visualforce server runs under a different domain as S-Controls and doesn't have access (through cross-site scripting) to the window.parent.location variable. I'm not sure if anyone's found a workaround to that yet - if so, it would be great to hear about it.
Mahmoud AbdelsalamMahmoud Abdelsalam
To navigate to another url from within a lightening component you should use           
sforce.one.navigateToURL("Your_Url");

For Salesforce Classic you could still use `window.location.href`
This is the documentation url for refrence
https://help.salesforce.com/articleView?id=sf.lex_prepare_vf_window.htm&type=5 (http://https://help.salesforce.com/articleView?id=sf.lex_prepare_vf_window.htm&type=5)