2
    Public class OrderController{

    Public Opportunity opp {get;set;}    
    Public List<Opportunity> oppData {get;set;}
    Public List<Account> accountList{get;set;}
    Public List<Order__c> orderList{get;set;}
    Public List<System__c> systemList{get;set;}
    Public List<Vendor_Order__c> venOrderList {get;set;}
    Public List<ID> sysId {get;set;}
    Public List<OpportunityLineItem> oppLine{get;set;}

    Public String accountId{get;set;}
    Public String orderId{get;set;}
    Public String oppId{get;set;}
    Public String systemId{get;set;} 
    Public String accountName{get;set;}
    Public String orderName{get;set;} 
    Public String systemName{get;set;}
    Public String serialNumber{get;set;}
    Public String assetTag {get;set;}
    Public Boolean isShow {get;set;}

    Public String contactName{get;set;}
    Public String userName{get;set;}
    Public String welcomeText{get;set;}
    Public String logoutLink{get;set;}
    Public String pipeSign{get;set;}
    Public WrapperClass singleConditionWrapper{get;set;}
    Public WrapperClass vendorOrderWrapper{get;set;}
    Public List<WrapperClass> WrapperList{get;set;}
    public Map<Id,Vendor_Order__c> vendorOrderMap{get;set;}
    public Map<String,OpportunityLineItem> systemOpportunityLineItemMap{get;set;}
    public Map<String,List<OpportunityLineItem>> systemOpportunityLineItemListMap{get;set;}
    public Map<String,Vendor_Order_Line__c> oppLIVendorOrderLIMap{get;set;}
    public List<String> oppLIIdSet{get;set;}
    public set<Id> vendorOrderIdSet{get;set;}

    Public OrderController(){

        opp = new Opportunity();
        vendorOrderMap = new Map<Id,Vendor_Order__c>();
        oppLIVendorOrderLIMap = new Map<String,Vendor_Order_Line__c>();
        systemOpportunityLineItemMap = new Map<String,OpportunityLineItem>();
        oppLIIdSet = new List<String>();
        vendorOrderIdSet = new set<Id>();
        systemOpportunityLineItemListMap = new map<String,List<OpportunityLineItem>>();
        contactName = Apexpages.currentPage().getParameters().get('Name');
        userName = Apexpages.currentPage().getParameters().get('UserId');
        welcomeText='Welcome,    ';
        logoutLink='Logout'; 
        pipeSign='|';
        WrapperList= new List<WrapperClass>();

        orderId = ApexPages.currentPage().getParameters().get('orderId');
        accountId = Apexpages.currentPage().getParameters().get('accountId');
        oppId = ApexPages.CurrentPage().getParameters().get('oppId');
        systemId = ApexPages.CurrentPage().getParameters().get('systemId');

        String queryFields = null;

        for(Schema.FieldSetMember field : SObjectType.Opportunity.FieldSets.Opportunity_Summary_Fields.getFields()){
            queryFields = (queryFields == null ? field.getFieldPath() : queryFields+', '+field.getFieldPath());
        }

        String query = 'Select '+queryFields+',Show_On_Sites_Page__c from Opportunity where Id=\''+oppId+'\'';   
        opp = Database.query(query);  
        if(opp.Show_On_Sites_Page__c){
            isShow  = true;
        }else{
            isShow = false;
            apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'This Order has not been made visible through this system. Please speak with your AllianceIT representative to have it made visible here.');
            apexpages.addmessage(msg); 

        }
        accountList= new List<Account>();
        accountList = [select Id,Name,Owner.Name,CreatedDate from Account where Id =: accountId];
        if(accountList.size() > 0){
            accountName = accountList.get(0).Name; 
        }

        oppLine = new List<opportunitylineitem>();
        List<opportunitylineitem> tempOppLine;
        oppLine = [select Id, Product2.Name,SKU__c,Description,Production_Description__c,system__c from opportunityLineItem where Opportunity.Id =: oppId];
        if(oppLine.size() >0)
        for(OpportunityLineItem oli : oppLine){

            if(oli.System__c != null && systemOpportunityLineItemListMap.containsKey(oli.System__c)){
                tempOppLine = systemOpportunityLineItemListMap.get(oli.System__c);
                tempOppLine.add(Oli);
                systemOpportunityLineItemListMap.put(oli.System__c,tempOppLine);
            }else if(oli.System__c != null){
                tempOppLine = new List<OpportunityLineItem>();
                 tempOppLine.add(oli);
                systemOpportunityLineItemListMap.put(oli.System__c,tempOppLine);

            }
            String oliId = oli.Id;
            oppLIIdSet.add(oliId.substring(0, 15));
        }   
        for(Vendor_Order_Line__c vol : [select Opportunity_Product_ID__c, Vendor_Order__c, Sort__c, SKU__c, Ref_Quote__c, Quantity__c, Product__c, Product_Price__c, Part_No__c, Name, Line_Price__c, Id, Full_Description__c, Additional_Description__c From Vendor_Order_Line__c  where Opportunity_Product_ID__c in : oppLIIdSet]){
            system.debug('-----'+vol);
            if(vol.Opportunity_Product_ID__c != null){
                if(vol.Opportunity_Product_ID__c != null)
                    oppLIVendorOrderLIMap.put(vol.Opportunity_Product_ID__c,vol);
                if(vol.Vendor_Order__c != null)
                    vendorOrderIdSet.add(vol.Vendor_Order__c);
            } 
        } 
        for(Vendor_Order__c vo : [Select Shipping_Street__c,Shipping_City__c,Shipping_State__c,Shipping_Country__c,Shipping_Zip__c,Ship_Date__c,Tracking_No__c from Vendor_Order__c where id in : vendorOrderIdSet]){
            vendorOrderMap.put(vo.Id,vo);
        } 



        systemList = new List<System__c>();
        systemList = [Select Id,Name,Serial_Number__c,Asset_Tag__c from system__c where Opportunity__c =: oppId];   
        List<oppLIVendorOrderWrapper> tempoppLIVendorOrderWrapperList;
        oppLIVendorOrderWrapper singleoppLIVendorOrderWrapper;   
        if(systemList.size() > 0){
            for(system__c  sys : systemList){

                singleConditionWrapper = new WrapperClass(sys.Name,sys.Serial_Number__c,sys.Asset_Tag__c);

                List<OpportunityLineItem> oppList = new List<OpportunityLineItem>();


                if(systemOpportunityLineItemListMap.get(sys.Id) != null && systemOpportunityLineItemListMap.get(sys.Id).size() > 0)
                    oppList = systemOpportunityLineItemListMap.get(sys.Id);

                if(oppList.size() > 0  && vendorOrderMap.size() > 0 && oppList.size() > 0){

                    tempoppLIVendorOrderWrapperList = new List<oppLIVendorOrderWrapper>();
                    for(OpportunityLineItem oli : oppList){

                        String oliId15 = oli.Id;
                        if(oppLIVendorOrderLIMap.get(oliId15.substring(0, 15)) != null && oppLIVendorOrderLIMap.get(oliId15.substring(0, 15) ).Vendor_Order__c != null && vendorOrderMap.get(oppLIVendorOrderLIMap.get(oliId15.substring(0, 15)).Vendor_Order__c) != null){
                            singleConditionWrapper.sys = sys;
                            singleoppLIVendorOrderWrapper = new oppLIVendorOrderWrapper();
                            singleoppLIVendorOrderWrapper.oppLI = oli;      
                            singleoppLIVendorOrderWrapper.vo = vendorOrderMap.get(oppLIVendorOrderLIMap.get(oliId15.substring(0, 15)).Vendor_Order__c);
                            tempoppLIVendorOrderWrapperList.add(singleoppLIVendorOrderWrapper);
                        }
                    }
                    singleCOnditionWrapper.oppLIVOWrapperList = tempoppLIVendorOrderWrapperList;
                }
                WrapperList.add(singleConditionWrapper);                
            }
        }
    }  

    Public class WrapperClass{
        Public String systemName{get;set;}
        Public String serialNumber{get;set;}
        Public String assetTag {get;set;}
        public system__c sys{get;set;}
        Public List<oppLIVendorOrderWrapper> oppLIVOWrapperList{get;set;}
        public WrapperClass(String systemName,String serialNumber,String assetTag){
            sys = new system__c();
            this.systemName=systemName;
            this.serialNumber=serialNumber;
            this.assetTag =assetTag;
            oppLIVOWrapperList = new List<oppLIVendorOrderWrapper>();
        }
    }

    Public class oppLIVendorOrderWrapper{
        public OpportunityLineitem oppLI{get;set;}
        public Vendor_Order__c vo{get;set;}
        public oppLIVendorOrderWrapper(){
            oppLI = new OpportunityLineitem();
            vo = new Vendor_Order__c();
        }
    }    
}

       ---------- Test Class ----------

       @isTest
       Private class OrderControllerTest{
       static testMethod void myUnitTest(){

        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        User u1 = new User(Alias = 'parth', Email='duser@org.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='parth@parth.com');
        insert u1;

        Account ac = new Account();
        ac.Name='Test Account';
        insert ac;

        Contact c1 = new Contact();
        c1.FirstName='Test';
        C1.LastName='TestName';
        c1.accountID=ac.id;
        insert c1;



        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opp';
        opp.AccountId = ac.Id;
        opp.CloseDate = system.Today();
        opp.StageName = 'Invoice Sent';
        insert opp;

        Order__c  od = new Order__c();
        od.Name='Test Oder';
        od.Opportunity__c=opp.id;
        insert od;

        System__c sys = new System__c();
        sys.Opportunity__c=opp.id;
        sys.Account__c=ac.id;
        insert sys;

        Vendor_Order__c Vod = new Vendor_Order__c();
        Vod.Opportunity__c=opp.id;
        Vod.Vendor__c=ac.id;
        insert Vod;

        Pricebook2 pb = new Pricebook2(IsStandard);
        pb.Name = 'Test PB';
        pb.IsActive = true;  
        insert pb;
        //Pricebook2 pb= [select Id,Name ,IsActive  from PriceBook2 where IsStandard=True];

        Product2 product = new Product2();
        product.Name='Test Product';
        product.IsActive=true;
        insert product ;

        // Create a pricebook entry for custom pricebook
        PricebookEntry pbe = new PricebookEntry();
        pbe.UseStandardPrice = false;
        pbe.Pricebook2Id=pb.id;
        pbe.Product2Id=product.id;
        pbe.IsActive=true;
        pbe.UnitPrice=99;
        insert pbe;

        OpportunityLineItem NewRec = new OpportunityLineItem();
        NewRec.TotalPrice=99;
        NewRec.Quantity=1;
        NewRec.OpportunityId=Opp.Id;
        NewRec.PricebookEntryId=pbe.id;
        insert NewRec;

         Apexpages.currentPage().getParameters().put('Name',c1.id);
         Apexpages.currentPage().getParameters().put('UserId',u1.id);
         ApexPages.currentPage().getParameters().put('orderId',od.id);
         Apexpages.currentPage().getParameters().put('accountId',ac.id);
         ApexPages.CurrentPage().getParameters().put('oppId',opp.id);
         ApexPages.CurrentPage().getParameters().put('systemId',sys.id);

         OrderController oc = new OrderController();
    }
}
2

IsStandard field is not writable on Pricebook2. Coz there can be one standard pricebook only in each organisation. check below:

IsStandard Properties : Defaulted on create, Filter, Group, Sort Description : Indicates whether this object is the standard price book for the organization (true) or not(false). Every organization has one standard price book, all other price books are custom price books.

https://resources.docs.salesforce.com/196/latest/en-us/sfdc/pdf/object_reference.pdf

Hope this clarifies your doubt.

Thanks! Ruchika

2

To insert priceBookEntry you can use below code

   Product2 newProd = new Product2(Name = 'test product', family = 'test family');
        insert newProd;
    PriceBookEntry pbEntry = new PriceBookEntry(
            UnitPrice = 300,
            PriceBook2Id  = Test.getStandardPricebookId(),
            Product2Id = newProd.Id,
            IsActive = true);
            insert pbEntry ;
0

If you are inserting values in a picklist your object or in formula field in ypur test class then you will get this error while running the test so just remove these insertions if you are using in test class

  • Nope that is not picklist value this is checkbox field and the field name is (IsStandard) – Ravi Vadher Jul 31 '15 at 6:56
  • This happens when you are trying to set a value to a formula field. – Shalini SF Aug 17 '15 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.