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

calling javascript after saving visual force page

Hi,  I have a visual force page that updates a contact record.

After saving the visual force page I need to fire off some javascript.  

The javascript is ajax that creates a session connection and updates the Account object.


I have to use ajax because I am calling sforce.connection.assignmentRuleHeader to update the Account territory based on information coming from the page that is being saved.  You cannot do this with a trigger or with apex code.


My question is, should i fire the javascript on the window.unload of the page or is their a better way?

Or should I call a 2nd vf page after saving 1st vf page and pass account id to 2nd vf page which contains javascript ajax calls?


Also  the javascript has to know the account id.  

How can I reference the account id of the current contact  being displayed in the vf page?


Thanks, Kevin.




Hi Kevin,


    window.unload event in javascript has some inconsistensies over different browsers. Instead of using page unload events, what you can do after performing save action in controller is to redirect to a vf page with account id as paramter. this vf will only have the javascript function that will run and update your account territory.



<script type="text/javascript">

function updateAccount(){
// Open a new modal dialogue"","msg","height=100 width=300,left=420,top=300");
msg.document.write("<html><center><p>Processing... Please Wait..</p>");
msg.document.write("<img src='/img/waiting_dots.gif'/></center></html> ");
//extracting account Id from url paramter
var currentUrl = window.parent.location.href;  var parameterSegment = currentUrl.split("?")[1]; var accountId = parameterSegment.split("=")[1]; //accountId variable has the actual Id of account.
// furthur processing using sforce.connection.assignmentHeaders msg.close(); //after processing
window.parent.location.href = "/"+accountId; // your choice of navigation
} </script>