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
ppiyushppiyush 

Testing Pagereferences

How can I test a pagereference method such as below:

 

 

public class childContact {
    
    private List<Contact> cntz;
    private Account acct; 

    public childContact(ApexPages.StandardController controller) {
        this.acct= (Account)controller.getRecord();
    }
    
    public Account getAt()
    {
        Account[] at = [Select id,name FROM Account where id= :acct.id];
        if(at.size()>0){
    		return at[0];
    	}
    	else{
    		return null;
    	}
    }
            
    public List<Contact> getCntz()
    {
        Account act = [Select id FROM Account where id = :acct.id];
        cntz = [Select id, Name, Client_Potential__c, title, Address_City__c, Direct_Phone__c, Email, Open_Projects__c, Days_Since_Last_Contact__c, Account.name, Ownerid from Contact where (account.parentid = :act.id OR account.id = :act.id)];
    	return cntz;
    }

    public PageReference UpdateRecords() {
        // this simple line of code finds out which column was changed and update the 
        // relevant account record accordingly!
        update cntz;
        return null;
      }


}

 

The Visual Force pageBlockButton using the pagereference method looks like this:

 

  <apex:pageBlockButtons location="top">
    <apex:commandButton value="Update" action="{!UpdateRecords}" id="theButton" rerender="page" status="status" onComplete="window.location.reload()"></apex:commandButton>
  </apex:pageBlockButtons>   

Any help is much appreciated - thanks!

Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd

Yup, but I would change it to a void method. PageReference will work but if you aren't sending the user anywhere it doesn't really make sense from a self documenting point of view.

 

 

public void UpdateRecords() {
    // this simple line of code finds out which column was changed and update the 
    // relevant account record accordingly!
    update cntz;

}

 

 

All Answers

JimRaeJimRae

Once you instantiate your controller, you can call the pagereference like a method.

 

 

TehNrdTehNrd

Yup, but I would change it to a void method. PageReference will work but if you aren't sending the user anywhere it doesn't really make sense from a self documenting point of view.

 

 

public void UpdateRecords() {
    // this simple line of code finds out which column was changed and update the 
    // relevant account record accordingly!
    update cntz;

}

 

 

This was selected as the best answer