0

This question already has an answer here:

I've read a lot of answered questions about this error but I can't seem to resolve it. I would very much appriciate any help. I am trying to test my Extension Controller from a SF Page but I keep getting the the above message along with this: Class.AutoCreateActivityControlExtension.: line 10, column 1 Class.AutoCreateActivityControlExtensionTest.EngProjectAutoCreateActivityTest: line 30, column 1.

Here is the Code for the Page:

<apex:page standardController="Engineering_Projects__c" extensions="EngineeringProjectControlExtension" >
    <apex:messages /><br/>
    <apex:form >   
        <apex:pageBlock title="Activity Generation Warning" id="selectionBlock">         

        <apex:outputText id="triggerText" value="{!generateWarningText}"/>

        </apex:pageBlock>
        <apex:commandButton value="Generate" action="{!autorun}" rendered="{!allowActivityGeneration}" />
        <apex:commandButton value="Cancel" action="{!cancel}"/>
        <br/>

    </apex:form>
</apex:page>

and the Code for the Extension Controller:

public with sharing class AutoCreateActivityControlExtension {

    private Engineering_Projects__c engProject;
    private boolean allowActivityDeletes {get;set;}
    private boolean allowActivityGeneration {get;set;}
    private String activityType = 'Generate';

    public AutoCreateActivityControlExtension (ApexPages.StandardController stdController) {

       this.engProject = [SELECT Id, Activity_Template__c,
                                     Configured__c,
                                     Nuclear__c,
                                     Commercial_Non_Standard__c,
                                     Submittal_Approval_Required_valve__c,
                                     Submittal_Approval_Required__c,
                                     Preliminary_O_M_Approval_Required__c,
                                     Final_Doc_Required__c,
                                     Engineering_Required__c,
                                     Customer_Specs_valve__c,
                                     Customer_Inspection_Required_valve__c,
                                     Vendor_Quotes_valve__c,
                                     Quality_Plan_Required_valve__c,
                                     Check_Marked_Specifications__c,
                                     Curve_Approval_Required__c,
                                     Customer_Witness_Required__c,
                                     T_C_Review_Required__c,
                                     Start_up_Required__c,
                                     Order_Hand_off_Date__c,
                                     Industry_Code__c,
                                     Order_Received_Date__c,
                                     Expected_Ship_Date__c,
                                     Customer_Requested_Delivery_Date__c,
                                     Lead_Time_Weeks__c,
                                     Contract_Administrator__c,
                                     OwnerId

                                     FROM Engineering_Projects__c
                                     WHERE Id = :ApexPages.currentPage().getParameters().get('id')];                                     

        List <Project_Activities__c> projActivitiesNotForDeletion = [Select id 
                                                                    from Project_Activities__c
                                                                    where Project_Name__c  = :engProject.id and
                                                                            (Status__c = 'In Progress' or Status__c = 'Complete')];

        if (projActivitiesNotForDeletion.size() > 0) {
            allowActivityDeletes = false;    // Do Not allow deletion
        } else {
            allowActivityDeletes = true;    //  allow deletion
        }

        List <Project_Activities__c> projActivities = [Select id 
                                                        from Project_Activities__c
                                                        where Project_Name__c  = :engProject.id];

        if (projActivities.size()>0) {
            allowActivityGeneration = false;    // Do Not allow deletion
        } else {
            allowActivityGeneration = true; //  allow deletion
        }
    }

    public PageReference autoRun() {        
        activityType = 'Generate';

        ActivityButtonStatus.ButtonPressed = True ;        

        String theId = ApexPages.currentPage().getParameters().get('id');

        if (theId == null) {
            return null;
            }

        AutoCreateActivityHelper projActHelper = new AutoCreateActivityHelper();
        projActHelper.buildActivityList(engProject);

        PageReference pageRef = new PageReference('/' + theId);
        pageRef.setRedirect(true);
        return pageRef;
    }

    public PageReference deleteActivities() {      
        activityType = 'Delete';

        String theId = ApexPages.currentPage().getParameters().get('id');

        if (theId == null) {
            return null;
            }

        AutoCreateActivityHelper projActHelper = new AutoCreateActivityHelper();
        projActHelper.deleteActivityList(engProject);

        PageReference pageRef = new PageReference('/' + theId);
        pageRef.setRedirect(true);
        return pageRef;
    }

    public PageReference cancel() {     
        String theId = ApexPages.currentPage().getParameters().get('id');
        PageReference pageRef = new PageReference('/' + theId);
        pageRef.setRedirect(true);
        return pageRef;
    }

    public boolean getAllowActivityDeletes() {      
        return allowActivityDeletes;
    }  

    public String getGenerateWarningText() {
        if(allowActivityGeneration) {
            return 'Please confirm auto-creation of activities.';
        } else {
            return 'It is not possible to create a schedule if activities already exist.';
        }
    }  

    public String getDeletionWarningText() {

        if(allowActivityDeletes) {
            return 'Are you sure you want to delete all activities? Activities will need to be re-created if deleted.';
        } else {
            return 'It is not possible to delete acrivities if any activity has a status of In Progress or Complete.';
        }
    }  

    public boolean getAllowActivityGeneration() {       
        return allowActivityGeneration;
    }  

    public boolean allowActivityGeneration() {      
        return allowActivityGeneration;
    }  

    public String getProjectId() {      
        return engProject.id;
    }

public String getEngineeringRequired() {      
        return engProject.Engineering_Required__c;
    }
}

Here is the test method:

@isTest(SeeAllData=true)
private class AutoCreateActivityControlExtensionTest {

    Public boolean allowActivityDeletes {get;set;}
    Public boolean allowActivityGeneration {get;set;}    

    static testMethod void EngProjectAutoCreateActivityTest() {

        Test.startTest();

        PageReference pageRef = Page.EngProjectAutoCreateActivity;        
        Test.setCurrentPageReference(pageRef);        

        Engineering_Projects__c engProject  = new Engineering_Projects__c(
                Activity_Template__c = 'Configured Commercial',
                Configured__c = 'YES',
                Product_Type_valve__c = 'Isolation Valves',
                Product_SubType_valve__c = 'Isolation Valves',  
                Operating_Company__c = 'Isolation Valves',              
                Order_Received_Date__c = System.today(),
                Customer_Requested_Delivery_Date__c = System.today()+40,
                Lead_Time_Weeks__c = 40,
                Expected_Ship_Date__c = System.today()+40 );

        insert engProject;

        Engineering_Projects__c thisengProject = [SELECT Id FROM Engineering_Projects__c LIMIT 1];   

        ApexPages.StandardController ac = new ApexPages.StandardController(thisengProject) ;
        AutoCreateActivityControlExtension controller = new AutoCreateActivityControlExtension(ac) ;
        controller.autoRun() ;
        controller.deleteActivities() ;        
       Test.stopTest();           
    }
}

marked as duplicate by Adrian Larson, Eric, Vamsi Krishna, Boris Bachovski, Mohith Shrivastava Jul 31 '15 at 14:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

You are getting this error because this.engProject is a single sObject not a list.

In the code it is not finding a record because you are not adding the id parameter to the page in the test method.

You should:

  1. In your class check for the ID being in the page parameters and handle appropriately

    if(ApexPages.currentPage().getParameters().get('id') == null){
     //throw an error or add an apex message that no ID was presented
     return;
    }
    

1b. Change Engineering_Projects__c engProject to Engineering_Projects__c[] engProject and then add a check for engProject.isEmpty()

  1. in your test add the id parameter to the page reference before instantiating your controller

    pageRef.getParameters().put('id',engProject.id);
    test.setCurrentPage(pageRef);
    
1

Your controller extension is looking for a parameter in the URL. your test code is not setting any parameter, so when the controller runs the query, its checking for an Engineering_Projects__c with an ID of null, thus the error. you need to set the parameter in your test class. Something like below

PageReference pageRef = Page.EngProjectAutoCreateActivity;        
Test.setCurrentPageReference(pageRef);        

Engineering_Projects__c engProject  = new Engineering_Projects__c(
        Activity_Template__c = 'Configured Commercial',
        Configured__c = 'YES',
        Product_Type_valve__c = 'Isolation Valves',
        Product_SubType_valve__c = 'Isolation Valves',  
        Operating_Company__c = 'Isolation Valves',              
        Order_Received_Date__c = System.today(),
        Customer_Requested_Delivery_Date__c = System.today()+40,
        Lead_Time_Weeks__c = 40,
        Expected_Ship_Date__c = System.today()+40 );

insert engProject;
**system.currentPageReference().getParameters().put('id', engProject.Id);**

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