1

I am new to VF and I have a requirement to override custom task page in lead level.

when the user tries to create a task instead of normal save button to display popup window & include custom fields in it.Everything is working fine and i am unable to setup test classes for this.

I Created a list button with following.

/apex/CustomTask?whoid={!Lead.Id}&retURL=%2F{!Lead.Id}

Vf page:

<apex:page standardController="Task" extensions="LogACallControllerExtension">
  <apex:sectionHeader title="New Task" />
  <apex:form id="pageForm">
  <apex:pageBlock title="Task Edit" mode="edit">
  <apex:pageBlockButtons location="both">
  <apex:commandButton value="Save" action="{!showPopup}" rerender="tstpopup"/>
  <apex:commandButton value="Cancel" action="{!cancel}" />
  </apex:pageBlockButtons>
  <apex:pageBlockSection title="Additional Information" columns="2" collapsible="false">
  <apex:inputField value="{!Task.WhoId}"/>
  <apex:inputField value="{!TAsk.Priority}"/>
  <apex:outputField value="{!Task.who.phone}"/>
  <apex:inputField value="{!Task.Time_zone__c}"/>
  <apex:outputField value="{!Task.who.Email}"/>
  </apex:pageBlockSection>
  <apex:pageBlockSection title="Reminder    
" columns="1" collapsible="false">
  <apex:pageBlockSectionItem >
  <apex:panelGrid columns="3" style="margin: 0 auto;">
  <apex:outputLabel >Reminder</apex:outputLabel>
  <apex:inputField value="{!Task.IsReminderSet}"/>
  <apex:inputField value="{!Task.ReminderDateTime}"/>
  </apex:panelGrid>
  </apex:pageBlockSectionItem>
  </apex:pageBlockSection>
  </apex:pageBlock>
  <apex:outputPanel id="tstpopup">
        <apex:outputPanel styleClass="popupBackground" layout="block" rendered="{!displayPopUp}"/>
            <apex:outputPanel styleClass="custPopup" layout="block" rendered="{!displayPopUp}">
              <apex:panelgrid columns="1" style="margin: 0 auto;">
              Please update if it is relevant party conversation
              </apex:panelgrid>
              <apex:panelgrid columns="2" style="margin: 0 auto;">
              <apex:outputLabel ><b>Connect</b></apex:outputLabel>
              <apex:inputfield value="{!Task.Connect__c}"/>
              <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!closePopup}" rerender="tstpopup"/>
              </apex:panelgrid> 
            </apex:outputPanel>
        </apex:outputPanel>
</apex:form>
<style type="text/css">
        .custPopup{
            background-color: white;
            border-width: 2px;
            color:red;
            border-style: solid;
            z-index: 9999;
            left: 50%;
            padding:10px;
            position: absolute;
            /* These are the 3 css properties you will need to change so the popup 
            displays in the center of the screen. First set the width. Then set 
            margin-left to negative half of what the width is. You can add 
            the height property for a fixed size pop up if you want.*/
            width: 500px;
            margin-left: -250px;
            top:100px;
        }
        .popupBackground{
            background-color:white;
            opacity: 0.90;
            filter: alpha(opacity = 20);
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            z-index: 9998;
        }
    </style>
</apex:page>

Apex Class

public class LogACallControllerExtension {
public Task task {get; set;}
public String inputValue { get; set; }
public boolean displayPopup {get; set;}

    public LogACallControllerExtension(ApexPages.StandardController controller) {
            this.task = (Task)controller.getRecord();
            task.whoId = ApexPages.currentPage().getParameters().get('whoid');    
            task.activitydate = Date.today();
            task.ownerid=UserInfo.getUserId();
            task.Subject='Call';
            task.Activity_Type__c='NA';
            task.IsReminderSet=true;
            task.ReminderDateTime=System.now();
    }
     public void closePopup() {        
        displayPopup = false;    
    }     
    public void showPopup() {        
        displayPopup = true;    
    }
}

I am following this link.But unable to setup because i am using standard controller and without pagereference.

[https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm][1]

My Test Class

public static testMethod void testActivity() 
    {
        List<Lead> leads = new List<Lead>{};
            Lead l = new Lead(lastName='TestCavalry',RecordTypeId ='0126F0000016MZP',
                                  Company ='Azuga Cavalry',Status='New',Account_Category_new__c='Prospect',
                                  Account_Category__c='Regular',Lead_Category__c='Sales',Lead_Type__c='Association List',
                                 Lead_Source__c='NEFBA',LGS_Owner_new__c='Cavalry Test',SIC_Code_1__c='123');
        leads.add(l);
        insert l;
        System.debug('****Data'+leads.size());
        PageReference pageRef = Page.CustomTask;
        pageRef.getParameters().put('whoid',l.id);
        Test.setCurrentPage(pageRef);

        //create a Task record
        Task tsk = new Task();
        insert tsk;
        ApexPages.StandardController std = new Apexpages.StandardController(tsk);
        LogACallControllerExtension  controller = new LogACallControllerExtension (std);

        List<Task> tsks=new List<Task>();
        tsks=[Select id,whoid from task where whoid=:l.id];
        System.assertEquals(1, tsks.size());
    }

1 Answer 1

1

This is the way you can create testmethod

PageReference pageRef = Page.CustomTask;

//create a Contact record and pass the ID 
pageRef.getParameters().put('whoid',contactId);

Test.setCurrentPage(pageRef);

//create a Task record
Task tsk = new Task();
//add other attributes

insert tsk;

ApexPages.StandardController std = new Apexpages.StandardController(tsk);

LogACallControllerExtension  controller = new LogACallControllerExtension (std); 

One more point to be noted that, if you try to access archived task record then this line will throw the error

this.task = (Task)controller.getRecord();

In that scenario, better to fetch Task from SOQL query upon passing TaskId.

[SELECT Id, Subject FROM Task WHERE IsDeleted = false AND Id = :myTaskId LIMIT 1 ALL ROWS];
5
  • Thank you!! I have a question how to use assert statements is it like normally I need to query task and need to check!! AND right now I am overriding the creation of a task in lead level.So, how to replace this this.task = (Task)controller.getRecord();
    – Pavan tej
    Jul 24, 2018 at 10:06
  • for that refer any trailhead or documentation, System.assert(); Jul 24, 2018 at 10:08
  • I created test class based on your answer but i am getting empty activities from test class
    – Pavan tej
    Jul 24, 2018 at 10:15
  • In my controller, I am already updating all fields by default. Still, I need to provide data for the task
    – Pavan tej
    Jul 24, 2018 at 10:20
  • Thank you for your suggestions & sharing knowledge i will make changes accordingly.
    – Pavan tej
    Jul 24, 2018 at 10:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .