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
elpaso750elpaso750 

Help with test class controller extension

Hi,

I'm pretty new to Apex development.

Upon request I was able to create a simple apex class which based on some logic, saves and clones a contact to a new Account. A Visual force page complets the set.

 

Here's the VF page :

 

 

<apex:page standardController="Contact" showHeader="true" tabStyle="contact" extensions="Contactedit">
<chatter:feedWithFollowers entityId="{!contact.id}"/>
<apex:sectionHeader title="Contact" subtitle="{!Contact.name}"/>
  <apex:form id="theForm">
  <apex:pageBlock title="Edit Contact" id="thePageBlock" mode="edit">
  <apex:pageMessages />
    <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Cancel" action="{!cancel}"/>               
    </apex:pageBlockButtons>
 
 <apex:pageBlockSection title="Contact Information" columns="2" collapsible="True">
                <apex:inputField value="{!Contact.Salutation}"/>
                <apex:inputField value="{!Contact.Phone}"/>
			    <apex:inputField value="{!Contact.FirstName}"/> 
			    <apex:inputField value="{!Contact.Fax}"/>
			    <apex:inputField value="{!Contact.LastName}"/>
			    <apex:inputField value="{!Contact.MobilePhone}"/>
			    <apex:inputField value="{!Contact.AccountId}"/>
			    <apex:inputField value="{!Contact.Email}"/>
			    <apex:inputField value="{!Contact.Title}"/>
			    <apex:inputField value="{!Contact.Email_2__c}"/> 
			    <apex:inputField value="{!Contact.Department}"/>    
				<apex:inputField value="{!Contact.Email_Opt_Out_Date__c}"/>
   </apex:pageBlockSection>
   <apex:pageBlockSection id="Description" title="Description" columns="1" collapsible="True">
                 <apex:inputTextArea id="newDesc" value="{!Contact.Description}"/>    
   </apex:pageBlockSection>
   <!-- apex:pageBlockSection title="Contact Photo" columns="1">
       <apex:inputField value="{!Contact.Foto__c}"/>
   </apex:pageBlockSection !--> 
   
   <apex:pageBlockSection id="Profile" title="Contact Profile" columns="2" collapsible="True">
          <apex:inputField value="{!Contact.CMF_Profile__c}"/>  
		  <apex:inputField value="{!Contact.BV_Profile__c}"/>
		  <apex:inputField value="{!Contact.MMF_Profile__c}"/> 
          <apex:inputField value="{!Contact.Swapswire_Profile__c}"/>
          <apex:inputField value="{!Contact.Trade_Cancellation_CMF__c}"/>
          <apex:inputField value="{!Contact.BV_Approval__c}"/>
          <apex:inputField value="{!Contact.BV_Fees__c}"/>
          <apex:inputField value="{!Contact.Accounting__c}"/>
          <apex:inputField value="{!Contact.MTS_Data__c}"/>
          <apex:inputField value="{!Contact.DataFeed_UserName__c}"/>
          <apex:inputField value="{!Contact.AssistantName}"/>
		  <apex:inputField value="{!Contact.AssistantPhone}"/>
	      <apex:inputField value="{!Contact.Inactive_Contact__c}"/> 	  
   <apex:pageBlockSectionItem >
   <apex:actionRegion >
   <B><apex:outputLabel value="Left the Company"/></B>
   <apex:outputPanel >
      <apex:inputCheckbox value="{!Contact.Left_the_Company_Flag__c}">
      <apex:actionSupport event="onclick" rerender="Profile" status="status"/>
    </apex:inputCheckbox>
    <apex:actionStatus startText="applying value..." id="status"/>
   </apex:outputPanel>
   </apex:actionRegion>
   </apex:pageBlockSectionItem>
 
  <apex:pageBlockSection Title="Left the Company" columns="2" rendered="{!Contact.Left_the_Company_Flag__c}">
        <apex:inputField value="{!Contact.Moved_to__c}"/>
        <apex:inputField value="{!Contact.Clone_to_new_Account_without_Profile__c}"/>
        <apex:inputField value="{!Contact.Comment__c}"/>
        <apex:inputField value="{!Contact.Clone_to_new_Account_with_Profile__c}"/>
  </apex:pageBlockSection>
  </apex:pageBlockSection>
  
   <apex:pageBlockSection id="Address2" title="Address" columns="2">
       <apex:inputField value="{!Contact.MailingStreet}"/>   
       <apex:inputField value="{!Contact.MailingCity}"/> 
       <apex:inputField value="{!Contact.MailingPostalCode}"/>
       <apex:inputField value="{!Contact.MailingState}"/>   
       <apex:inputField value="{!Contact.MailingCountry}"/>   
    </apex:pageBlockSection>
    
    <apex:pageblocksection id="MTS" Title="MTS Market Configuration" Columns="2">
    
    <apex:inputField value="{!Contact.Linked_Membercodes__c}"/> 
    <apex:inputField value="{!Contact.MTS_CertificateType__c}"/>   
    
      <apex:pageblocksection id="CMF" Title="CMF Platform" Columns="1">
        <apex:inputField value="{!Contact.CMFI_UserID__c}"/>
        <apex:inputField value="{!Contact.CMFI_User_Profile__c}"/>
        <apex:inputField value="{!Contact.CMFI_Platform__c}"/>
	    <apex:outputField value="{!Contact.CMF_Software_Signature__c}"/>
        <apex:outputField value="{!Contact.CMFI_Last_Login__c}"/>
        <apex:outputField value="{!Contact.Multimarket_SW_version__c}"/>           
        <apex:outputField value="{!Contact.Network_Type__c}"/>
      </apex:pageblocksection>
      <apex:pageblocksection id="MMF" Title="MMF Platform" Columns="1">
       <apex:inputField value="{!Contact.MMFI_UserID__c}"/> 
       <apex:inputField value="{!Contact.MMFI_User_Profile__c}"/>
       <apex:inputField value="{!Contact.MMF_Swap_User_ID__c}"/>
       <apex:inputField value="{!Contact.Swapswire_Account__c}"/>
       <apex:inputField value="{!Contact.MMFI_Platform__c}"/> 
      </apex:pageblocksection>
    </apex:pageblocksection>
  
 </apex:pageBlock>
</apex:form>
</apex:page>

 

 

and the class :

 

 

public class Contactedit {
   public Contact Contact {get;set;}
   private ApexPages.standardController controller {get;set;}
   
   Public boolean clonawo {get;set;}
   Public boolean clonawi {get;set;}
   
    	// recovers from Accounts the billing details
    	public Account accountmoved {
        	get{
        		if (accountmoved == null) {
        			accountmoved = [select BillingStreet,BillingCity,BillingCountry,BillingPostalCode,BillingState  from account where id =: contact.moved_to__c];
        		}
                return accountmoved;
        	}
        	set;	
        }
    	
    	public contact clonecontact {
    	get{
    		//initialize the new contact
			if (clonecontact == null){
				contact t = new contact();
				//fill the fields of the new contact record
    			t.firstname = contact.firstname;
    			t.lastname = contact.lastname;
    			t.salutation = contact.salutation;
        		t.accountid = contact.moved_to__c;
        		t.title = contact.Title;
        		t.OwnerId =Userinfo.getUserId();
        		t.Left_the_Company_flag__c = false;
        		t.MailingStreet = accountmoved.BillingStreet;
        		t.MailingCity = accountmoved.BillingCity;
        		t.MailingCountry = accountmoved.BillingCountry;
        		t.MailingPostalCode = accountmoved.BillingPostalCode;
        		t.MailingState = accountmoved.BillingState;
        		t.Phone = '+00';
				
				System.debug('clonawi = ' + clonawi);
				if ((contact.Clone_to_new_Account_with_Profile__c == true)|| (clonawi == true)) {
					t.accounting__c = contact.accounting__c;
				    t.bv_approval__c = contact.bv_approval__c;
				    t.bv_fees__c = contact.bv_fees__c;
				    t.bv_Profile__c = contact.bv_Profile__c;
				    t.CMF_Profile__c = contact.CMF_Profile__c;
				    t.MMF_Profile__c = contact.MMF_Profile__c;
				    t.Swapswire_Profile__c = contact.Swapswire_Profile__c;
				    t.Trade_Cancellation_CMF__C = contact.Trade_Cancellation_CMF__C;
				    }
    			clonecontact = t;
     		}
    		return clonecontact;
    	}
    	set;
    }
    
        public Contactedit(ApexPages.StandardController stdController) {
    	// constructor
    	controller = stdController;
        this.contact= (contact)stdController.getRecord();
        
    }
        public void createcontactAndclone(){
    	// check the ownerId of the position before creating the jobApp & task
    	//  the Task cannot be owned by a Queue
    	
    	// String ownerId = checkOwnerIdForQueue(SelectedPosition.OwnerId);
    	
        
        // We update the current contact instead of insert
        
         try{
        	//insert contact;
        	
        	contact.Clone_to_new_Account_without_Profile__c = false;
        	contact.Clone_to_new_Account_with_Profile__c = false;
        	update contact;
        } catch (Exception e){
        	ApexPages.addMessages(e);
        }
        // checks if either clone was selected and of course the company where moved.
        
        //if ((contact.Moved_to__c != null) && ((contact.Clone_to_new_Account_without_Profile__c == true)||(contact.Clone_to_new_Account_with_Profile__c== true)))
        if ((contact.Moved_to__c != null) && ((clonawo == true)||(clonawi == true)))
        {
        try{
        	system.debug('#######' + clonecontact);
        	// insert the new record on contact
        	insert clonecontact;  
        	//catch exception
        } catch (Exception e){
        	ApexPages.addMessages(e);	
        }
    }
        }
    public PageReference save(){
          clonawo = contact.Clone_to_new_Account_without_Profile__c;
          clonawi = contact.Clone_to_new_Account_with_Profile__c;
    	// updated the Contact and eventually create a new one.
    	createcontactAndclone();
		
		// use the standard controller to perform the save and redirect
    	return controller.view();
    }
}

 

 

 

Making the above and testing the results took me about 2hours. OK maybe it's not the best code, but  hey I'm the new kid.
Now I'm loosing my sleep trying to get the TEST class working, it's more than 4 days now, I'm getting all different kind of errors, this is the test class :
@isTest
private class Test_Contactedit {
    public static testMethod void Contactedit_Test() {
     
       PageReference pageRef = Page.Contact_Edit_layout;
       Test.setCurrentPage(pageRef);
	   
	  Contactedit controller = new Contactedit(new ApexPages.StandardController());  
      String nextPage = controller.save().getUrl();
   
      ApexPages.currentPage().getParameters().put('ContactId', [select id from contact limit 1].id);
                  
      //extension = new contactedit(new ApexPages.StandardController(cont));              
      controller.Left_the_company_flag__c=true;
      controller.Moved_to__c='001R000000bn4Dg';
      controller.Clone_to_new_Account_with_profile__c=true;
      nextPage = controller.save().getUrl(); 
      
    }
}

 

the error I'm getting is : 
Save error: Constructor not defined: [ApexPages.StandardController].<Constructor>()	Test_Contactedit.cls	
 	line 30	Force.com save problem

 

Any help would be highly appreciated.
Alex

 

Best Answer chosen by Admin (Salesforce Developers) 
Hani MHani M

Hi,

 

   I think u are trying to set some filed in Contact object, if so need to try,

controller.Contact.Left_the_company_flag__c = ...

 

cheeers..

All Answers

Hani MHani M

Try This,

 

@isTest
private class Test_Contactedit {
    public static testMethod void Contactedit_Test() {

        Test.setCurrentPage( Page.Contact_Edit_layout );
        Contact opp = new Contact();       
        ApexPages.StandardController stdController = new ApexPages.StandardController( opp );             
        Contactedit controller = new Contactedit ( stdController );
      
 
      String nextPage = controller.save().getUrl();
  
      ApexPages.currentPage().getParameters().put('ContactId', [select id from contact limit 1].id);
                 
      //extension = new contactedit(new ApexPages.StandardController(cont));             
      controller.Left_the_company_flag__c=true;
      controller.Moved_to__c='001R000000bn4Dg';
      controller.Clone_to_new_Account_with_profile__c=true;
      nextPage = controller.save().getUrl();
     
    }
}

elpaso750elpaso750

Hi,

 

        thanks for your suggestion, I think in my previous attempts I had somethign similar but this is coming up with the following error : 

 

 

Save error: Variable does not exist: Left_the_company_flag__c Test_Contactedit.cls  line 37 Force.com save problem
Alex

 

Hani MHani M

Hi,

 

   I think u are trying to set some filed in Contact object, if so need to try,

controller.Contact.Left_the_company_flag__c = ...

 

cheeers..

This was selected as the best answer
elpaso750elpaso750

Thank you so much.

 

shame on me, I just now realised how stupid I've been.

 

well, now I feel I wont make that mistake again :-)

 

Thanks again.