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

Error Poppulate

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PopulateOpportunities: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.PopulateOpportunities: line 12, column 43: []


trigger PopulateOpportunities on Delivery__c (before insert, before update) {
    //Set of SaleOrder Ids
    Set<Id> SODIDs = new Set<Id>();
    for (Delivery__c deliveryNew : {
        SODIDs.add(deliveryNew.Sales_Order__c); //A set of SaleOder 
     Map<Id, Sales_Order__c> pSale = new Map<Id, Sales_Order__c>([SELECT, a.OpportunitiesID__c, a.Name,a.Quotes__c FROM Sales_Order__c a WHERE IN :SODIDs]);
    for(Delivery__c fcon : Trigger.New){
        if(pSale.get(fcon.Sales_Order__c).OpportunitiesID__c != null){
        fcon.OpportunitiesID__c = pSale.get(fcon.Sales_Order__c).OpportunitiesID__c;
        fcon.Quotes__c = pSale.get(fcon.Sales_Order__c).Quotes__c ;}



private class TestPopulateOpportunities {

   static testMethod void myUnitTest() {
    Account Acc = new Account(Name = 'test',Type = 'Domestic');
    insert Acc;
    Opportunities__c Opp = new Opportunities__c(,Stage__c='Closed Won',Probability__c='100%',Unit__c='Kg',Currency__c='Bath');
    insert Opp; 
    Quotes__c Quo = new Quotes__c(OpportunitiesID__c =;
    insert Quo;
    Sales_Order__c pSale1 = new Sales_Order__c(OpportunitiesId__c=Opp.Id,Quotes__c =;
    insert pSale1;
    Sales_Order__c fSale1 = new Sales_Order__c(OpportunitiesId__c=Opp.Id,Quotes__c =;
    insert fSale1;
        Delivery__c pDO1 = new Delivery__c(OpportunitiesID__c=pSale1.OpportunitiesId__c,Quotes__c =pSale1.Quotes__c );
        insert pDO1;
        Delivery__c fDO1 = new Delivery__c(OpportunitiesID__c=fSale1.OpportunitiesId__c,Quotes__c =fSale1.Quotes__c );
        insert fDO1;
        // TO DO: implement unit test

 Thank you so much.

Best Answer chosen by Admin (Salesforce Developers) 

Thank Kiran it's work!


Delivery__c pDO1 = new Delivery__c(sales_order__c =,OpportunitiesID__c=pSale1.OpportunitiesId__c,Quotes__c =pSale1.Quotes__c );

All Answers


 if(pSale.get(fcon.Sales_Order__c).OpportunitiesID__c != null){




the above highlited part has no meaning i think so ...

 in think you have to use like this



 if(pSale.get(fcon).OpportunitiesID__c != null){


Thank Kiran , but have a new problem


Error: Compile Error: Incompatible key type SOBJECT:Delivery__c for MAP at line 12 column 12


in your test class while creating the delivery record you are not referencing any sales_order__c object record.. 


try like this...



Delivery__c pDO1 = new Delivery__c(sales_order__c = psale1,OpportunitiesID__c=pSale1.OpportunitiesId__c,Quotes__c =pSale1.Quotes__c );


then insert.


Thank Kiran it's work!


Delivery__c pDO1 = new Delivery__c(sales_order__c =,OpportunitiesID__c=pSale1.OpportunitiesId__c,Quotes__c =pSale1.Quotes__c );

This was selected as the best answer