• developerjdk470
  • NEWBIE
  • 50 Points
  • Member since 2012

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies

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 

 

I made a button to disable scheduled apex jobs, and I want to show a confirmation once completed.  I also want that button to disappear once completed as it serves no purpose if there aren't any active jobs.


I can't make both of these work at the same time though :(  Any tips?

 

VF Page:

 

<apex:page controller="c" >
  <apex:pagemessages />


    <apex:form >
        <apex:pageBlock id="thePageBlock"  >
            <apex:pageBlockButtons >
                <apex:commandButton id="disableButton" value="Disable Integration" rendered="{!schedulersActive}" action="{!deactivateScheduler}">
	                <apex:actionSupport event="oncomplete" action="{!renderButtons}"/>
	                <apex:actionSupport event="oncomplete" rerender="disableButton"/> 
                </apex:commandButton>
            </apex:pageBlockButtons>    
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

Controller methods:

public class c{

    public Boolean schedulersActive { get; set; }

    public c(){
        renderButtons();
    }//method c

	public static void deactivateScheduler(){
		myUtils.deactivateScheduler();
        ApexPages.Message confirmation=new ApexPages.Message(ApexPages.severity.CONFIRM, 'Confirmed: The daily synchronization has been deactivated.');
        ApexPages.addmessage(confirmation);
	}//deactivateScheduler
    
    public void renderButtons(){
        List<CronTrigger> cts=myUtils.getCronTriggers();
        //If there isn't a result here, that means the scheduler hasn't been started
        if (cts.size()==myUtils.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 c

 

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 

 

I made a button to disable scheduled apex jobs, and I want to show a confirmation once completed.  I also want that button to disappear once completed as it serves no purpose if there aren't any active jobs.


I can't make both of these work at the same time though :(  Any tips?

 

VF Page:

 

<apex:page controller="c" >
  <apex:pagemessages />


    <apex:form >
        <apex:pageBlock id="thePageBlock"  >
            <apex:pageBlockButtons >
                <apex:commandButton id="disableButton" value="Disable Integration" rendered="{!schedulersActive}" action="{!deactivateScheduler}">
	                <apex:actionSupport event="oncomplete" action="{!renderButtons}"/>
	                <apex:actionSupport event="oncomplete" rerender="disableButton"/> 
                </apex:commandButton>
            </apex:pageBlockButtons>    
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

Controller methods:

public class c{

    public Boolean schedulersActive { get; set; }

    public c(){
        renderButtons();
    }//method c

	public static void deactivateScheduler(){
		myUtils.deactivateScheduler();
        ApexPages.Message confirmation=new ApexPages.Message(ApexPages.severity.CONFIRM, 'Confirmed: The daily synchronization has been deactivated.');
        ApexPages.addmessage(confirmation);
	}//deactivateScheduler
    
    public void renderButtons(){
        List<CronTrigger> cts=myUtils.getCronTriggers();
        //If there isn't a result here, that means the scheduler hasn't been started
        if (cts.size()==myUtils.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 c