Hi We have two fields created for reporting and administrtion purpose.

  1. Product_Book_View_Count__c
  2. Product_Book_Download_Count__c

We track how many time a user is visiting the record and updating the 1st field Product_Book_View_Count__c,

Secondly we try to check how many time a user prints a VF page which is a PDF rendered. I have tried to implement both but first one worked as expected but the second one does not update the count rather it will take me to the vf page.

Please find the code snippet below.

<apex:page standardController="SG_ProductBook__c">
   <apex:includeScript value="/soap/ajax/28.0/connection.js"/>
   <script type="text/javascript">
       sforce.connection.sessionId = '{!GETSESSIONID()}';

       var object = new sforce.SObject('SG_ProductBook__c');
       object.Id = "{!SG_ProductBook__c.Id}";
       object.Product_Book_View_Count__c = {!SG_ProductBook__c.Product_Book_View_Count__c } + 1;

       var result = sforce.connection.update([object]);
       if(result[0].success != 'true'){
           alert('Could not Update the Record View Tracking Information. \r\nError: ' + result[0].errors.message);
    var object2 = new sforce.SObject('SG_ProductBook__c');
       object2.Id = "{!SG_ProductBook__c.Id}";
       object2.Product_Book_Download_Count__c = {!SG_ProductBook__c.Product_Book_Download_Count__c} + 1;
    var result2 = sforce.connection.update([object]);
     if(result2[0].success != 'true'){
           alert('Could not Update the Record download Tracking Information. \r\nError: ' + result[0].errors.message);

when I go into salesforce record detail page automatically it goes to the to the pdf even before I click on the PDF export custom lightning button.


The very instant that you set window.location (or any variant), all scripts are immediately halted, all resources cease loading, all server communications are severed, and the navigation occurs. You must place the window.location code after anything else you want to do.

  • here iam trying to count the number of time this VF page is opened. Will it work if I use it after it? Sep 15 '20 at 9:28
  • @SamuelRobert It should, although you could also just perform the count from within the PDF page if you were willing to use some Apex code (like 7-0 lines worth of code, not much at all).
    – sfdcfox
    Sep 15 '20 at 12:54
  • @ SFDCFOX yes I did the same and it worked perfectly thanks. Sep 16 '20 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.