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
BrenzoBrenzo 

OnClick Javascript Button to Display Alert AFTER Page Reload?

I have an OnClick Javascript button on the lead record that when clicked, changes the Lead status and refreshes the page. What'd I'd like is for a pop-up window to appear after the page refreshes displaying a message. 

I started playing around with the .onload and .reload function, but the alerts I wrote kept appearing upon the button being clicked, not after the page refreshes/reloads. I just want something to pop up and say "Your lead has been updated."

Here's the bottom portion of my code:

// assign values to fields using LeadObj variable
LeadObj.Status = 'Inactive';
LeadObj.Status_Reason__c = 'Lead Unresponsive';
}

//save the change
var result = sforce.connection.update([LeadObj]);

//refresh the page
window.location.reload();


Thanks in advance for any help!

Best Answer chosen by Brenzo
kaustav goswamikaustav goswami
Hi Brenzo,

The call that you done to update the record is synchronous in nature.

Thus you can check the result object for the success flag and show the alert based on that.

Can you please try the following:

Below the line : var result = sforce.connection.update([LeadObj]);

add the following code:

if (result[0].getBoolean("success")) {
    alert("Operation Successful");
  } else {
    alert("Operation Failed");
  }

Thanks,
Kaustav

All Answers

Grazitti TeamGrazitti Team
Hi Brenzo,

Please follow the steps below:

1. First add parameter to URL which is set to true before page refresh code i.e window.location.reload(); like code below:

    https:abc.com?id=123&param=true

2. You can use a label in vf page and make it's text visible based on value of parameter passed in URL as below:

    jQuery(document).ready(function(){              
                if({!$CurrentPage.parameters.param== 'true'}){                
                   document.getElementById("thankyoutextid").style.visibility = "visible";              
                };              
            });


<apex:outputLabel id="thankyoutextid">Thank You</apex:outputLabel>

Please mark this as best answer if it answers your question, it helps others.

Thanks!
kaustav goswamikaustav goswami
Hi Brenzo,

The call that you done to update the record is synchronous in nature.

Thus you can check the result object for the success flag and show the alert based on that.

Can you please try the following:

Below the line : var result = sforce.connection.update([LeadObj]);

add the following code:

if (result[0].getBoolean("success")) {
    alert("Operation Successful");
  } else {
    alert("Operation Failed");
  }

Thanks,
Kaustav
This was selected as the best answer
BrenzoBrenzo
@Kaustav that worked perfectly! I was worried that it wouldn't execute the javascript without click 'OK' to dismiss the button, but it seems to being executing as designed. Thanks.

@Grazitti, I was hoping to avoid working with visualforce pages, but thank you for chiming in!