1

I am reusing some old code here - i've made slight changes but the code itslef is so old it contained the test class within the main class. I have seperated them out but I am getting a pile of errors - any idea on how to get rid of the above erorr message would be appreciated

@isTest
public class tester{
 static testMethod void Product_Search_Controller2() {

        Account acc = new Account();
        acc.Name = 'Silver Lined Solutions';
        insert(acc);

        Product2 p = new Product2();
        p.Name = 'TestProd';
        p.IsActive = true;
        p.Business_Unit__c = 'MAG';
        insert(p);

        List<PriceBook2> pbs = [SELECT Id FROM PriceBook2 WHERE Name = 'MA&G' LIMIT 1];
        if(0==pbs.size()) {
            PriceBook2 pbMAG = new PriceBook2();
            pbMAG.Name = 'MA&G';
            pbMAG.IsActive = true;
            insert(pbMAG);
        }

       // PriceBook2 standardPB = [SELECT Id FROM PriceBook2 WHERE IsStandard = True LIMIT 1];

       Pricebook2 standardPB = null;
       List <Pricebook2> pbe = [SELECT Id FROM PriceBook2 WHERE IsStandard = True LIMIT 1];
       if (pbe.size()>0)
       {
        standardPB = pbe.get(0);

       }

       PriceBookEntry pbe1 = new PriceBookEntry();
     //  if (new PriceBookEntry.Id != null)
       pbe1.PriceBook2Id = standardPB.Id;
       pbe1.Product2Id = p.Id;
       pbe1.IsActive = true;
       pbe1.UnitPrice=10;

      insert(pbe1);








       pbe1 = new PriceBookEntry();
        pbe1.PriceBook2Id = [SELECT Id FROM PriceBook2 WHERE Name = 'MA&G' LIMIT 1].Id;
        pbe1.Product2Id = p.Id;
        pbe1.IsActive = true;
        pbe1.UnitPrice=100;
        insert(pbe1);

        RecordType rt = null;
        List<RecordType> rts = [SELECT Id FROM RecordType WHERE SobjectType='Opportunity' AND Name='MA&G Corporate' LIMIT 1];
        if(0==rts.size()) {
            rts = [SELECT Id FROM RecordType WHERE SobjectType='Opportunity' AND IsActive=True LIMIT 1];
        }
        rt = rts.get(0);

        Opportunity opp = new Opportunity();
        opp.Name = 'TestOpp';
        opp.CloseDate = date.today();
        opp.StageName = 'Anything';
        opp.AccountId = acc.Id;
        opp.RecordTypeId = rt.Id;
        opp.PriceBook2Id = pbe1.PriceBook2Id;
        insert(opp);

        Test.setCurrentPage(Page.Product_Search);
        ApexPages.currentPage().getParameters().put('OppId',opp.Id);
        ApexPages.currentPage().getParameters().put('OppPB',opp.PriceBook2Id);

        Product_Search_Controller psc  = new Product_Search_Controller();

        system.assertequals(opp.Id, psc.opportunityId);
        system.assertequals(opp.PriceBook2Id, psc.oppPB);

        system.assert(psc.getBUList().size()>0);

        psc.BU = 'MAG';
        psc.searchQuery = 'TestProd';
        psc.Search();
        system.assert(psc.getHasResults());
        system.assert(psc.pbes.size()>0);

        psc.selectedPBEId = psc.pbes.get(0).Id;

        psc.ShowAll();
        system.assert(psc.getHasResults());

        PageReference oppProdMgr = psc.selectPBE();
        system.assertequals(opp.Id, oppProdMgr.getParameters().get('Id'));
        system.assertequals(psc.selectedPBEId, oppProdMgr.getParameters().get('addProdPBE'));

        psc.searchQuery = 'A';
        psc.Search();

        psc.cancel();

    }
}
3
  • 1
    What line is the error occurring at? Jun 30 '14 at 14:37
  • line 35 column 1
    – user6866
    Jun 30 '14 at 14:41
  • @user6866 fyi, line numbers don't show up in sfse posts, it's more useful if you post the specific error message, than flag the line with a comment e.g. // error occurs here. Makes it easier for people to answer your question Jun 30 '14 at 22:49
2

You create a new PricebookEntry at line 25:

 Pricebook2 standardPB = null;

You then query for & assign to standardPB another PBE:

List <Pricebook2> pbe = [SELECT Id FROM PriceBook2 WHERE IsStandard = True LIMIT 1];
   if (pbe.size()>0)
   {
    standardPB = pbe.get(0);

   }

However, if no PBE is returned from your query, standardPB remains null, hence your error at line 35 when it is referenced:

pbe1.PriceBook2Id = standardPB.Id;

You will need to set SeeAllData to true to access Pricebooks: SeeAllData=false and Pricebooks although the newly added Test.getStandardPricebookId() method may work in your case: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm#apex_System_Test_getStandardPricebookId

0
0

I am betting that you are in or testing in an org that has not made the Standard Pricebook active.

You code

Pricebook2 standardPB = null;
       List <Pricebook2> pbe = [SELECT Id FROM PriceBook2 WHERE IsStandard = True LIMIT 1];
       if (pbe.size()>0)
       {
        standardPB = pbe.get(0);

       }

       PriceBookEntry pbe1 = new PriceBookEntry();
     //  if (new PriceBookEntry.Id != null)
       **pbe1.PriceBook2Id = standardPB.Id;**
       pbe1.Product2Id = p.Id;
       pbe1.IsActive = true;
       pbe1.UnitPrice=10;

Does not test if it actually found a standard price book where I indicated. So if it does not exist you will get a dereference

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.