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
developerjdk470developerjdk470 

Update rendered var after DML on page load

I had a problem where my variable for rendering buttons wasn't getting reset prior to page load.  

 

Short story - never use "Static" in autoRun, then recalc the var values and your components will be correctly shown / hidden.

 

More detail on my use case - hide the "deactivate" button if there are no scheduled jobs, show it if they exist.  For an oAuth flow, I have a flow like this:

1) Click button

2) Redirected to 3rd party website

3) Login using UN/pw

4) Approve access

5) Redirected back to my VF page with a param in the URL

6) Controller Constructor fires, setting redered variable values

7) AutoRun() method fires, making an API call to get the accessToken & save it in a custom settting.  Also, schedule some jobs

8) Render page with Deactivate button shown (couldn't get this to work initially)

 

When #7 was defined as "public static void autoRun()", the var never got set to the correct value.  When I took out the "static" and reran my method for setting the rendered vars, it worked fine.

 

It seems the order of operations for page load is:

1) Controller constructor runs

2) AutoRun method runs

3) Page loads

 

If you want to do DML, you can't do it in the Controller Constructor, but have to do it in AutoRun.  However, since the constructor has already run, you need to re-run the code to calculate any rendered vars if they depend on the DML.

 

In other words, this was wrong:

public class myController{

    public Boolean schedulersActive { get; set; }

    public myController(){
//render method for button & method to grab param from URL to go here
        renderButtons();
    }//method myController

    public PageReference authenticateOAuth(){
        return new PageReference(g2wUtils.g2wOAuthURL());
    }//authenticateOAuth
    
    public static void autoRun(){
        PageReference pr=ApexPages.currentPage();
        Map<String, String> params=pr.getParameters();
        String responseKeyParamName='code';
        String responseKey;
        if(params.size()>0){
             if (params.get(responseKeyParamName)!=null){
                responseKey=params.get(responseKeyParamName);
                someOtherMethod.accessTokenURL(responseKey);
             }//if 2
        }else{
//need to handle the case where authorization is denied         
        }//if 1

    }//autoRun
    
    public void renderButtons(){
        List<CronTrigger> cts=someOtherMethod.getCronTriggers();
        //If there isn't a result here, that means the scheduler hasn't been started
        if (cts.size()==g2wUtils.numScheduledJobs){
            schedulersActive = TRUE;
        } else {
//            system.debug('ct.CronExpression= '+ct[0].CronExpression);
            schedulersActive = FALSE;
        }//if 1
        system.debug('The number of cron jobs active is: '+cts.size()+' and the value of SchedulersActive is:'+schedulersActive);
    }//renderScheduler
	

}//class 

 Corrected:

public class myController{

    public Boolean schedulersActive { get; set; }

    public myController(){
//render method for button & method to grab param from URL to go here
        renderButtons();
    }//method myController

    public PageReference authenticateOAuth(){
        return new PageReference(g2wUtils.g2wOAuthURL());
    }//authenticateOAuth
    
    public void autoRun(){
        PageReference pr=ApexPages.currentPage();
        Map<String, String> params=pr.getParameters();
        String responseKeyParamName='code';
        String responseKey;
        if(params.size()>0){
             if (params.get(responseKeyParamName)!=null){
                responseKey=params.get(responseKeyParamName);
                someOtherMethod.accessTokenURL(responseKey);
             }//if 2
        }else{
//need to handle the case where authorization is denied         
        }//if 1
        renderButtons();
    }//autoRun
    
    public void renderButtons(){
        List<CronTrigger> cts=someOtherMethod.getCronTriggers();
        //If there isn't a result here, that means the scheduler hasn't been started
        if (cts.size()==g2wUtils.numScheduledJobs){
            schedulersActive = TRUE;
        } else {
//            system.debug('ct.CronExpression= '+ct[0].CronExpression);
            schedulersActive = FALSE;
        }//if 1
        system.debug('The number of cron jobs active is: '+cts.size()+' and the value of SchedulersActive is:'+schedulersActive);
    }//renderScheduler
	

}//class 

 

Best Answer chosen by Admin (Salesforce Developers) 
developerjdk470developerjdk470

See above