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
Ammar P 9Ammar P 9 

Pls help me for code coverage.........

Apex class code:
public with sharing class TrackOrderController {
    public string ordernum {get; set;}
    public date orderDate {get; set;}
    public List<Order> Order {get; set;}
    public List<OrderItem> status {get; set;}
    public List<string> vrs {public get; private set;}
    public List<string> pro {public get; private set;} 
    public List<integer> index {public get; private set;}
    
    public PageReference findOrder()
    {
        try{
      
        Order = [SELECT id FROM Order where PoNumber=:ordernum and PoDate =: orderdate ];
        status = [SELECT Product_name__c,Vehicle_Request_Status1__c FROM OrderItem WHERE OrderId =: Order[0].Id];
        system.debug('size-->'+status.size());
        vrs = new List<String>();
        index = new List<integer>();   
        for(integer i=0; i<status.size(); i++){
            index.add(i);
            if(status[i].Vehicle_Request_Status1__c != null){
            vrs.add(status[i].Vehicle_Request_Status1__c);
            }else{
                vrs.add('Null');
            }
        }
           
        system.debug(status);
        system.debug('vrs list--->'+vrs);
        system.debug('index list--->'+index);
        pro = new List<string>();
        for(integer i=0; i<status.size(); i++){
             pro.add(status[i].Product_name__c);
        }

        system.debug('pro list--->'+pro);
        return null;
        }
        catch(Exception ex){ ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please check purchase number and date')); return null; }
    }
    public PageReference reset() {
        PageReference newpage = new PageReference(System.currentPageReference().getURL());
    newpage.setRedirect(true);
    return newpage;
    }
}


Test class:
@isTest(SeeAllData=true)
public class TrackOrderControllerTest {
    
static testMethod void findOrder(){
  
      Account acc = New Account();
        acc.name = 'test';
        acc.CR_Number__c = '1234567890';
        acc.CustomerName__c = 'test acc';
        acc.CR_Date_English__c = Date.newInstance(2016, 12, 9);
        acc.CR_Capital__c = 12426;
        acc.Current_Revenue__c = 1233;
        acc.Special_Admin_Rate__c = 123456;
        acc.Security_Type__c = 'ghayth';
        acc.ID_Issuer__c = 'asdasd';
        acc.CR_City__c = 'asdasd';
        acc.Workforce__c = 'sdadad';
        acc.In_Business__c = 'sdasdasdas';
        acc.Nationality_of_Business__c = 'India';
        acc.Arabic_Date__c = Date.newInstance(2016, 10, 7);
        acc.Company_Establishment__c = 'sdads';
        acc.Bank2__c = 'sdfdsf';
        acc.City_Region__c = 'sadf';
        insert acc;  
        
        Product2 newProduct = new Product2();
        newProduct.name = 'abc';
        newProduct.Varient__c = 'asd';
        newProduct.Make__c = 'AMI';
        newProduct.Model__c = 'SEMI TRAILER';
        newProduct.ModelYear__c = 2018;
        newProduct.Fuel_Type__c = 'Petrol';
        newProduct.Transmission__c = 'Powershift';
        newProduct.Internal_Purchase_Price__c = 25;
        newProduct.Registration_Fee__c = 25;
        Insert newProduct;
        
         Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id =newProduct.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
    Order ord = new Order ();
    ord.AccountId =acc.Id;
        ord.PoNumber='PO67543';
        ord.PoDate = Date.newInstance(2016, 10, 5);
        ord.Status ='Draft';
        ord.EffectiveDate= system.today();
        ord.Pricebook2Id = customPB.Id;
    insert ord;
    
    OrderItem orditem = new OrderItem();
    orditem.OrderId = ord.Id;
        Orditem.Vehicle_Request_Status1__c ='PO Approved';
        orditem.PricebookEntryId =customPrice.Id;
        orditem.UnitPrice= 678;
        orditem.Quantity= 7;
    insert orditem;
        
      
    /*  list<OrderItem>status=new List<OrderItem>();
        list<integer>index=new List<integer>();
        status = [SELECT Product_name__c,Vehicle_Request_Status1__c FROM OrderItem WHERE OrderId =: ord.Id];
    for(integer i=0; i<status.size(); i++){
            index.add(i);
        } */
         PageReference testPage = Page.TrackOrder;
         Test.setCurrentPage(testPage);
         testPage.getParameters().put('Id', String.valueOf(ord.Id));
    Test.StartTest(); 
      ApexPages.StandardController sc = new ApexPages.StandardController(ord);
            TrackOrderController  testord = new TrackOrderController();
      testord.findOrder();
      testord.reset ();

    Test.StopTest();
        
  }
}
Suraj Tripathi 47Suraj Tripathi 47

Hi Please find the solution.

http://salesforcecodes.com/inserting-records-into-custom-object-using-controller-in-visualforce-page/

https://developer.salesforce.com/forums?id=9062I000000Bn1qQAC


public class TrackOrderControllerTest {

static testMethod void findOrder(){

Account acc = New Account();
	acc.name = 'test';
	acc.CR_Number__c = '1234567890';
	acc.CustomerName__c = 'test acc';
	acc.CR_Date_English__c = Date.newInstance(2016, 12, 9);
	acc.CR_Capital__c = 12426;
	acc.Current_Revenue__c = 1233;
	acc.Special_Admin_Rate__c = 123456;
	acc.Security_Type__c = 'ghayth';
	acc.ID_Issuer__c = 'asdasd';
	acc.CR_City__c = 'asdasd';
	acc.Workforce__c = 'sdadad';
	acc.In_Business__c = 'sdasdasdas';
	acc.Nationality_of_Business__c = 'India';
	acc.Arabic_Date__c = Date.newInstance(2016, 10, 7);
	acc.Company_Establishment__c = 'sdads';
	acc.Bank2__c = 'sdfdsf';
	acc.City_Region__c = 'sadf';
	insert acc;  
	
	
	
	   
	   
	
			Product2 newProduct = new Product2();
	newProduct.name = 'abc';
	newProduct.Varient__c = 'asd';
	newProduct.Make__c = 'AMI';
	newProduct.Model__c = 'SEMI TRAILER';
	newProduct.ModelYear__c = 2018;
	newProduct.Fuel_Type__c = 'Petrol';
	newProduct.Transmission__c = 'Powershift';
	newProduct.Internal_Purchase_Price__c = 25;
	newProduct.Registration_Fee__c = 25;
	Insert newProduct;
	
	   Id pricebookId = Test.getStandardPricebookId();
	   
	    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = newProduct.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
	
	 Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
	insert customPB;
	
	PricebookEntry customPrice = new PricebookEntry(
		Pricebook2Id = customPB.Id, Product2Id =newProduct.Id,
		UnitPrice = 12000, IsActive = true);
	insert customPrice;
	
	
	  Order ord = new Order ();
	ord.AccountId =acc.Id;
	ord.PoNumber='PO67543';
	ord.PoDate = Date.newInstance(2016, 10, 5);
	ord.Status ='Draft';
	ord.EffectiveDate= system.today();
	ord.Pricebook2Id = pricebookId;
	insert ord;
	
	
	

OrderItem orditem = new OrderItem();
orditem.OrderId = ord.Id;
orditem.Product_name__c='test';
orditem.Vehicle_Request_Status1__c =   // fill this field
	Orditem.Vehicle_Request_Status1__c ='PO Approved';
	orditem.PricebookEntryId =standardPrice.Id;
	orditem.UnitPrice= 678;
	orditem.Quantity= 7;
insert orditem;
 

  PageReference testPage = Page.TrackOrder;
	 Test.setCurrentPage(testPage);
	 testPage.getParameters().put('Id',ord.Id);

test.starttest();
TrackOrderController.ordernum='PO67543';
TrackOrderController.orderDate=Date.newInstance(2016, 10, 5);



test.stoptest();

  PageReference testPage = Page.TrackOrder;
	 Test.setCurrentPage(testPage);
	 testPage.getParameters().put('Id', String.valueOf(ord.Id));
Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(ord);
		TrackOrderController  testord = new TrackOrderController();
		testord.ordernum='PO67543';
		testord.orderDate=Date.newInstance(2016, 10, 5);
		
  testord.findOrder();
  
  testord.reset ();

Test.StopTest();

}
}


Please do somme changes according to your code.

Please let me know it is working or not?

Please mark it as the best answer so that other people would take references from it.

Maharajan CMaharajan C
Hi Ammar,

Please try the below test class:
 
@isTest(SeeAllData=true)
public class TrackOrderControllerTest {
    
    static testMethod void findOrder(){
        
        Account acc = New Account();
        acc.name = 'test';
        // Add if there is any fields are required
        insert acc;  
        
        Product2 newProduct = new Product2();
        newProduct.name = 'abc';
        newProduct.Description='Test Product Entry 1';
        newProduct.productCode = 'ABC';
        newProduct.isActive = true;
        Insert newProduct;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        // Insert PricebookEntry
        
        PricebookEntry standardPrice = new PricebookEntry();
        standardPrice.Pricebook2Id = pricebookId;
        standardPrice.Product2Id = newProduct.Id;
        standardPrice.UnitPrice = 1;
        standardPrice.IsActive = true;
        standardPrice.UseStandardPrice = false;
        insert standardPrice ;
        
        Order ord = new Order ();
        ord.AccountId =acc.Id;
        ord.PoNumber='PO67543';
        ord.PoDate = Date.newInstance(2016, 10, 5);
        ord.Status ='Draft';
        ord.EffectiveDate= system.today();
        ord.Pricebook2Id = pricebookId;
        insert ord;
        
        OrderItem orditem = new OrderItem();
        orditem.OrderId = ord.Id;
        Orditem.Vehicle_Request_Status1__c ='PO Approved';
        orditem.PricebookEntryId =standardPrice.Id;
        orditem.UnitPrice= 678;
        orditem.Quantity= 7;
        insert orditem;
        
        
        Test.StartTest(); 
        //ApexPages.StandardController sc = new ApexPages.StandardController(ord);
        PageReference testPage = Page.TrackOrder;
        Test.setCurrentPage(testPage);
        testPage.getParameters().put('Id', String.valueOf(ord.Id));
        TrackOrderController  testord = new TrackOrderController();
        testord.findOrder();
        testord.reset ();
        Test.StopTest();
        
    }
}

Thanks,
Maharajan.C
Ammar P 9Ammar P 9
Hi Your code coverage is 0. thanks for ur try for me.