+ Start a Discussion
KyoKyo 

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 : trigger.new) {
        SODIDs.add(deliveryNew.Sales_Order__c); //A set of SaleOder 
    }        
     Map<Id, Sales_Order__c> pSale = new Map<Id, Sales_Order__c>([SELECT a.id, a.OpportunitiesID__c, a.Name,a.Quotes__c FROM Sales_Order__c a WHERE a.id 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 ;}
    }
    

}

 

@isTest
private class TestPopulateOpportunities {

   static testMethod void myUnitTest() {
    Account Acc = new Account(Name = 'test',Type = 'Domestic');
    insert Acc;
    Opportunities__c Opp = new Opportunities__c(Account_Name__c=Acc.id,Stage__c='Closed Won',Probability__c='100%',Unit__c='Kg',Currency__c='Bath');
    insert Opp; 
    Quotes__c Quo = new Quotes__c(OpportunitiesID__c = Opp.id);
    insert Quo;
     
      
    Sales_Order__c pSale1 = new Sales_Order__c(OpportunitiesId__c=Opp.Id,Quotes__c = Quo.id);
    insert pSale1;
    Sales_Order__c fSale1 = new Sales_Order__c(OpportunitiesId__c=Opp.Id,Quotes__c = Quo.id);
    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) 
KyoKyo

Thank Kiran it's work!

 

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

All Answers

kiranmutturukiranmutturu

 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){

KyoKyo

Thank Kiran , but have a new problem

 

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

kiranmutturukiranmutturu

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.

KyoKyo

Thank Kiran it's work!

 

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

This was selected as the best answer