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
Sunad RasaneSunad Rasane 

hi i have written my first vf standard extension controller.. i tried to write the test class for it.. but i have got most of the part wrong... if anyone can help me with it..

My Extension Controller-

public class OppCreateInstalltionButtonPageCont 
{
    public OppCreateInstalltionButtonPageCont(ApexPages.StandardController stdController)
    {
    }

public String currentRecordId {get;set;}
public Opportunity opp{get;set;}
public Contact con{get;set;}
public Pricebook2 pb{get;set;}
public WorkType wt{get;set;}
public ServiceTerritory st{get;set;}


    public PageReference CreateInstallationWorkOrder()
    {
    currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
    opp = [Select id,name,Job_Number__c, AccountId, OwnerId from Opportunity where id =: currentRecordId ];
    con = [ Select id, name from contact where accountid =: opp.Accountid];
    pb = [Select id, name from Pricebook2 where Name = 'Standard Price Book'];
    wt = [Select id, name from WorkType where Name = 'Installation'];
    st = [Select id, name from ServiceTerritory where Name = 'All' ];

    WorkOrder wo = new WorkOrder();
    wo.Opportunity__c = opp.id;
    wo.AccountId = opp.AccountId;
    wo.OwnerId = opp.OwnerId;
    wo.contactId = con.id;
    wo.Duration = 8;
    wo.DurationType = 'Hours';
    wo.Priority = 'High';
    wo.Subject= opp.Job_Number__c +' ' + opp.name;
    wo.ServiceTerritoryId = st.Id;
    wo.WorkTypeId = wt.Id;
    wo.Pricebook2Id = pb.Id ;
    insert wo;

    Id workorderId = wo.id;

    List<OpportunityLineItem> oppLineItem =[Select id, name,Quantity,UnitPrice, Product2Id,PricebookEntryId from OpportunityLineItem where OpportunityId =: opp.id];
    List<ProductConsumed> prodconlist = new  List<ProductConsumed> ();
    List<ProductRequired> prodreqlist = new  List<ProductRequired> ();

    for(OpportunityLineItem op : oppLineItem)
    {
        ProductConsumed pc = new ProductConsumed();
        pc.QuantityConsumed = op.Quantity;
        pc.UnitPrice = op.UnitPrice;
        pc.WorkOrderId = workorderId;
        //pc.ProductItemId = op.Product2Id;
        pc.PricebookEntryid = op.PricebookEntryId;
        prodconlist.add(pc);

        ProductRequired pr = new ProductRequired();
        pr.ParentRecordId = workorderId;
        pr.Product2Id = op.Product2Id;
        pr.QuantityRequired = op.Quantity;
        prodreqlist.add(pr);
    }
    insert prodconlist;
    insert prodreqlist;
        
        PageReference page = new PageReference('/'+currentRecordId);
        return page;
    } 
    
    public PageReference CreateServiceWorkOrder()
    {
    currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
    opp = [Select id,name,Job_Number__c, AccountId, OwnerId from Opportunity where id =: currentRecordId ];
    con = [ Select id, name from contact where accountid =: opp.Accountid];
    pb = [Select id, name from Pricebook2 where Name = 'Standard Price Book'];
    wt = [Select id, name from WorkType where Name = 'Service'];
    st = [Select id, name from ServiceTerritory where Name = 'All' ];

    WorkOrder wo = new WorkOrder();
    wo.Opportunity__c = opp.id;
    wo.AccountId = opp.AccountId;
    wo.OwnerId = opp.OwnerId;
    wo.contactId = con.id;
    wo.Duration = 90;
    wo.DurationType = 'Minutes';
    wo.Priority = 'High';
    wo.Subject= opp.Job_Number__c +' ' + opp.name;
    wo.ServiceTerritoryId = st.Id;
    wo.WorkTypeId = wt.Id;
    wo.Pricebook2Id = pb.Id ;
    insert wo;

    Id workorderId = wo.id;

    List<OpportunityLineItem> oppLineItem =[Select id, name,Quantity,UnitPrice, Product2Id,PricebookEntryId from OpportunityLineItem where OpportunityId =: opp.id];
    List<ProductConsumed> prodconlist = new  List<ProductConsumed> ();
    List<ProductRequired> prodreqlist = new  List<ProductRequired> ();

    for(OpportunityLineItem op : oppLineItem)
    {
        ProductConsumed pc = new ProductConsumed();
        pc.QuantityConsumed = op.Quantity;
        pc.UnitPrice = op.UnitPrice;
        pc.WorkOrderId = workorderId;
        //pc.ProductItemId = op.Product2Id;
        pc.PricebookEntryid = op.PricebookEntryId;
        prodconlist.add(pc);

        ProductRequired pr = new ProductRequired();
        pr.ParentRecordId = workorderId;
        pr.Product2Id = op.Product2Id;
        pr.QuantityRequired = op.Quantity;
        prodreqlist.add(pr);
    }
    insert prodconlist;
    insert prodreqlist;
        
        PageReference page = new PageReference('/'+currentRecordId);
        return page;
    }  
}