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
Nick KeehanNick Keehan 

Basic test class for creating records

HI Guys.

Looking for some help writing  a test class. Very basic, just record creation.

Visualforce page is basic, just standard controller Account.

 
public class AccExtension4 {

private final Account objacc;


    public AccExtension4(ApexPages.StandardController controller) {
         this.objacc=(Account)controller.getrecord();
    }
    
     public Account acc {get; set;}
     public Opportunity opp {get; set;} 
     Account cust = new Account();   
     Opportunity opps = new Opportunity();


public PageReference Save() {


       //Add all the class information then insert the class
        cust.LastName= objacc.LastName;
        cust.FirstName= objacc.FirstName;
        cust.Phone= objacc.Phone; 
        cust.PersonMobilePhone= objacc.PersonMobilePhone; 
        cust.PersonEmail= objacc.PersonEmail;          
        cust.Siebel_References__c = objacc.Siebel_References__c ;           
        cust.Customer_Row_ID__c = objacc.Customer_Row_ID__c;           
        cust.RecordTypeid = '0120E000000aznK';
        upsert cust Customer_Row_ID__c;
    
    
         opps.AccountId = cust.id;
         opps.Name = 'New Sale';
         opps.Product_Name_String__c = objacc.Product_Names__c;
         opps.CloseDate = system.today();
         opps.RecordTypeID = '012200000000re7';
         opps.Customer_Type__c = 'Fixed';
         opps.Call_Reason__c = 'Non Retention';  
         opps.Call_Reason_Detail__c = objacc.Call_Reason_Detail__c;      
         opps.StageName = 'Closed Won';    
         opps.PriceBook2Id = '01sD0000000d3L2';                 
         opps.Retention_Promotions_Offered_V2__c = objacc.Promotions_Offered__c;                       
         insert opps;
 
 
return new PageReference('/'+ opps.id); 
         
         }

    }


I had a look online for an example to use.
This is what has given me a low code coverage.
@isTest 
public class AccExtension2Test 
{
 static testMethod void testMethod1() 
 {
 Account testAccount = new Account();
        testAccount.LastName= 'test last name';
        testAccount.FirstName= 'test first name';
        testAccount.Phone= '021000000'; 
        testAccount.PersonMobilePhone= '021000000'; 
        testAccount.PersonEmail= 'nihan@gmail.com';          
        testAccount.Siebel_References__c = 'tester' ;           
        testAccount.Siebel_Customer_Row_ID__c = '32pkj-32nm';           
        testAccount.RecordTypeid = '012D00000003KdL';
        upsert testAccount Siebel_Customer_Row_ID__c ;



 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
  AccExtension4 testAccPlan = new AccExtension4(sc);

  PageReference pageRef = Page.FibreSimplex; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
  Test.setCurrentPage(pageRef);

  //testAccPlan.save(); call all your function here
 Test.StopTest();
 }
}

 
Best Answer chosen by Nick Keehan
v varaprasadv varaprasad
Hi Nick,

Try this : 
 
isTest 
public class AccExtension2Test 
{
 static testMethod void testMethod1() 
 {
 //Add here your recordtype labelname instead Development
 Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Add Recodtypename').getRecordTypeId();
 Account testAccount = new Account();
        testAccount.LastName= 'test last name';
        testAccount.FirstName= 'test first name';
        testAccount.Phone= '021000000'; 
        testAccount.PersonMobilePhone= '021000000'; 
        testAccount.PersonEmail= 'nihan@gmail.com';          
        testAccount.Siebel_References__c = 'tester' ;           
        testAccount.Siebel_Customer_Row_ID__c = '32pkj-32nm';           
        testAccount.RecordTypeid = devRecordTypeId;
        insert testAccount;



 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
  AccExtension4 testAccPlan = new AccExtension4(sc);

  PageReference pageRef = Page.FibreSimplex; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
  Test.setCurrentPage(pageRef);

  PageReference pageRef1 = testAccPlan.save(); call all your function here
 Test.StopTest();
 }
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Thanks
Varaprasad
Salesforce Freelance Consultant/Developer/Administrator
@For Salesforce Project Support: varaprasad4sfdc@gmail.com


Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

All Answers

v varaprasadv varaprasad
Hi Nick,

Try this : 
 
isTest 
public class AccExtension2Test 
{
 static testMethod void testMethod1() 
 {
 //Add here your recordtype labelname instead Development
 Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Add Recodtypename').getRecordTypeId();
 Account testAccount = new Account();
        testAccount.LastName= 'test last name';
        testAccount.FirstName= 'test first name';
        testAccount.Phone= '021000000'; 
        testAccount.PersonMobilePhone= '021000000'; 
        testAccount.PersonEmail= 'nihan@gmail.com';          
        testAccount.Siebel_References__c = 'tester' ;           
        testAccount.Siebel_Customer_Row_ID__c = '32pkj-32nm';           
        testAccount.RecordTypeid = devRecordTypeId;
        insert testAccount;



 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
  AccExtension4 testAccPlan = new AccExtension4(sc);

  PageReference pageRef = Page.FibreSimplex; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
  Test.setCurrentPage(pageRef);

  PageReference pageRef1 = testAccPlan.save(); call all your function here
 Test.StopTest();
 }
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Thanks
Varaprasad
Salesforce Freelance Consultant/Developer/Administrator
@For Salesforce Project Support: varaprasad4sfdc@gmail.com


Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
This was selected as the best answer
Nick KeehanNick Keehan
Thanks, record type looked fine and didnt increase my coverage. 

Notsure what im doing wrong here