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
miiWorksmiiWorks 

Test Method for a Lookup Field

Hello

 

We are looking for assistance on how to write a test class for the below code. If anyone could assist or direct me to another page that would be appreciated. We are using this lookup field and 'converting' it to a pick list type field for ease of use.

 

public class PropertyStagePL {

    private MyApp__Property__c property;
    public PropertyStagePL(ApexPages.StandardController controller) {
        this.property = (MyApp__Property__c)controller.getRecord();
    }
    public List <SelectOption> getStage {        
        Get{
            // GET CURRENT RECORD RECORD TYPE INFO 
            List<RecordType> bList = [SELECT Id, Name,SobjectType,DeveloperName
                FROM RecordType 
                WHERE Id =:property.RecordTypeId];  
            // FIND MATCHING STAGE RECORD TYPE FOR CURRENT RECORD
            List<RecordType> cList = [SELECT Id, Name,SobjectType,DeveloperName
                FROM RecordType 
                WHERE DeveloperName =:bList[0].DeveloperName AND SobjectType='MyStage__Stage__c'];
             
            List <SelectOption> stage = new List <SelectOption>();
            for( MyStage__Stage__c st: [SELECT Name, RecordType.Id, RecordType.Name, MyStage__Value__c, MyStage__Order_Number__c
                FROM MyStage__Stage__c 
                WHERE MyStage__Active__c = TRUE AND RecordTypeId = :cList[0].Id ORDER BY MyStage__Order_Number__c ASC ])
                stage.add(new selectOption(st.id, '('+st.MyStage__Order_Number__c +') '+st.MyStage__Value__c));
            return stage;
        }
        Set;
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Laxman RaoLaxman Rao

replace PSPL1.getStage();  with this

List<SelectOption> sp = PSPL1.getStage;  

All Answers

miiWorksmiiWorks

hi Lexman,

 

Thx for your quick reply. I have seen those pages. I regularly write classes for Apex Triggers, so this is the first test class for a class attached to a visualforce page. I just need a point in the right direction for the Extention class test method to get code coverage, not a whole visualforce page

 

Would you please run out a brief example/outline? The visualforce code below if you wanted to see why the class was created.

 

<apex:page standardController="Property__c" extensions="PropertyStagePL">
    <apex:form >
        <apex:pageBlock >
        <apex:pageBlockSection title="Property Status" columns="2" collapsible="false">
            <apex:pageblockSection >
        <apex:selectList value="{!Property__c.Stage__c}" required="True" size="1" id="stage">
            <apex:selectOptions value="{!getStage}"/>
         </apex:selectList>
         <apex:commandButton value="Save Status Change" action="{!save}"/>
            </apex:pageblockSection>
        </apex:pageBlockSection>
        </apex:pageBlock>
         <apex:pageMessages ></apex:pageMessages>
         <apex:detail inlineEdit="true" />            
    </apex:form>    
</apex:page>

Laxman RaoLaxman Rao

Hope this will help you.

 

//first populate and inset MyStage__Stage__c records

MyStage__Stage__c st = new MyStage__Stage__c();

insert st;


MyApp__Property__c property = new MyApp__Property__c();
ApexPages.Standardcontroller stdController = new Apexpages.Standardcontroller(property);
PropertyStagePL propertyStagePL = new PropertyStagePL(stdController);
propertyStagePL.getStage();

miiWorksmiiWorks

Hi Lexman,

 

Almost there!

 

Stage list and Prop2 is in static above testMethod. Last bit of covereage is the getStage Method.

 

    public static testMethod void TestPropertyStagePL() {        
        ApexPages.Standardcontroller stdController = new Apexpages.Standardcontroller(Prop2);
        PropertyStagePL PSPL1 = new PropertyStagePL(stdController);
        PSPL1.getStage();  
       
    }

 

still throwing an error at this line PSPL1.getStage(); 

 

Error: Compile Error: Method does not exist or incorrect signature: [PropertyStagePL].getStage() at line 81 column 9

 

Do you have any ideas on why it's not picking up that method in the class?

Laxman RaoLaxman Rao

replace PSPL1.getStage();  with this

List<SelectOption> sp = PSPL1.getStage;  

This was selected as the best answer
miiWorksmiiWorks

Thank you Lexman! 100% coverage :-)