1

In my update I have created a product and I am referring this product to a OpportunityLineItem am getting an error

System.NullPointerException: Attempt to de-reference a null object

But when I do debug I am able to view the record id Of product. Do anyone have any idea on this. Do I am missing out anything.

 public static testmethod void autoPopulateProjectFields(){

    Account acc = new Account();
    acc.Name = 'AccName';
    insert acc;

    Id pricebookId = Test.getStandardPricebookId();
    Product2 p2 = new Product2(Name          = 'Product X', 
                               ProductCode   = 'Pro-X', 
                               isActive      = true,
                               List_Price__c = 300);
    insert p2;

    PricebookEntry pbEntry = new PricebookEntry(Pricebook2Id = pricebookId,
                                                Product2Id   = p2.Id,
                                                UnitPrice    = 100.00,
                                                IsActive     = true);
    insert pbEntry;

    Asset ass         = new Asset();
    ass.Name          = 'siteName';
    ass.AccountId     = acc.ID;
    insert ass;           

    Opportunity opp   = new Opportunity();
    opp.Name          = 'OppName';
    opp.StageName     = 'Qualified';
    opp.CloseDate     = Date.Today().addDays(5);
    opp.DomainName__c = 'Test Domain';
    insert opp;

    OpportunityLineItem oppLine = new OpportunityLineItem();
    oppLine.Opportunity         = opp;
    System.debug('Product :::' + p2);
    System.debug('Product details :::' + p2.Id);
    System.debug('Product details :::' + [SELECT Id FROM Product2 WHERE Id = :p2.Id].Id);
    oppLine.Product2.Id         = p2.Id; // I am getting error on this line 
    //oppLine.Product2.Id         = [SELECT Id FROM Product2 WHERE Id = :p2.Id].Id;
    oppLine.Quantity            = 1;
    oppLine.UnitPrice           = 250;
    insert oppLine;

    AssetOpportunity__c assOpp  = new AssetOpportunity__c();
    assOpp.Opportunity__c       = opp.Id;  
    assOpp.Site__c              = ass.Id;
    insert assOpp;

    opp.StageName               = 'Closed Won';
    ProjectTriggerHandler.isUpdate = True;
    update opp;

}
2

I fixed this issue with changing

oppLine.Product2.Id         = p2.Id; 

To

oppLine.PricebookEntryId    = pbEntry.Id;

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.