+ Start a Discussion
maiyakumaiyaku 

Test Class this error

one error is null id after insert

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PopAccounttoPayment: execution of AfterInsert caused by: System.StringException: Invalid id: Trigger.PopAccounttoPayment: line 16, column 42: 

 

trigger PopAccounttoPayment on Payment_Line__c (after insert) {
set<ID> setInvID = new set<ID>();

        for(Payment_Line__c Pml : trigger.new){
                if(Pml.Invoice_BC__c != null || Pml.Invoice_BC__c != ''){
                        setInvID.Add(Pml.Invoice_BC__c);
                }
        }   
        
        Invoice_BC__c InvBC = new Invoice_BC__c();
        List<Invoice_BC__c> lstInvBC = [Select id,AccountID__c from Invoice_BC__c Where id in : setInvID];
            if(lstInvBC.size() != 0){
                InvBC = lstInvBC[0];
            }   
        if(InvBC.AccountID__c != null && InvBC.AccountID__c != ''){  
            Payment_Line__c pml = Trigger.new[0];   
            List<Payments__c> lstPm = new List<Payments__c>();
            Payments__c Pm = new Payments__c(id = pml.DocNo__c);
            Pm.AccountID__c = InvBC.AccountID__c;
            lstPm.Add(Pm);
        
            Update lstPm;
        }
}

 Test class

@isTest 
private class TestPopAccounttoPayment{
    static testMethod void myTest() {
    
      Account objAcc = new Account();
      objAcc.Name = 'test';
      insert objAcc;
        
      Opportunities__c objOpp = new Opportunities__c();
      objOpp.Account_Name__c = objAcc.Id;
      insert objOpp;
           
      Quotes__c objQuo = new Quotes__c();
      objQuo.Account_Name__c = objAcc.Id;
      objQuo.OpportunitiesID__c = objOpp.Id;
      insert objQuo;
                
      Sales_Order__c  objSOD = new Sales_Order__c();
      objSOD.Name = 'test4';
      objSOD.OpportunitiesID__c = objOpp.Id;
      objSOD.Quotes__c = objQuo.Id;
      insert objSOD;
      
      Invoice_BC__c  objInvBc = new Invoice_BC__c();
      objInvBc.Name = 'Test5';
      objInvBc.Sales_OrderID__c = objSOD.id;
      objInvBc.AccountID__c = objAcc.id;
      insert objInvBc;
      
      Payments__c objPay = new Payments__c();
      objPay.Name = 'Test2';
      objPay.Due_Date__c = System.Today();
      objPay.AccountID__c = objInvBc.AccountID__c;
      insert objPay;
      
      Test.StartTest();
      Payment_Line__c objPayLine = new Payment_Line__c();
      ApexPages.currentPage().getParameters().put('id', objPay.id);
      objPayLine.Name = 'Test3';
      objPayLine.DocNo__c = objPay.id;
      objPayLine.Invoice_BC__c = objInvBc.id;
      insert objPayLine;
           
      Payments__c objPay2 = new Payments__c();
      ApexPages.currentPage().getParameters().put('id', objPayLine.DocNo__c);
      objPay2.Name = 'Test2';
      objPay2.Due_Date__c = System.Today();
      objPay2.AccountID__c = objInvBc.AccountID__c;
      insert objPay2;
      update objPay2;
      Test.StopTest();
    
    }
}

 thank you so much

sfdcbynitesfdcbynite

is pml.Doc__c a lookup to Payment__c? If not, you are assigning the wrong recordtype to the id for Payment__c.

KyoKyo

Yes pml.Doc__c is lookup Payment__c object. i want update Account after save payment line .

sfdcbynitesfdcbynite

I don't see anything in your code off-hand, except it doesn't look like you are bulkifying so well (Trigger.new[0]).

 

Try putting in some debug statements to see what values you are getting for the ids.