+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

Test class error for simple apex class

HI ,

I am facing the following error , can anyone please help me to resolve this error.
 
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Sales Order” process failed. Give your Salesforce admin these details. <b>An unhandled fault has occurred in this flow</b><br>An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information.: []

My test cass is following here 
 
@isTEst
public class GD_SalesOrderPrintableViewTest {

    
    public static testmethod void testData(){
        
        Account acc = GD_TestDataFactory.createCustomer('Acc1','Customer');
        insert acc;
        
        GD_Address__c address = new GD_Address__c(GD_account__c = acc.id, GD_Address_1__c = 'address 1', GD_Address_2__c = 'address 2', GD_Address_3__c = 'address 3');
        insert address;
        
        GD_Order__c ord = new GD_Order__c(GD_Account__c = acc.id);
        insert ord;
        
        GD_Inventory_Organization__c inOrg = new GD_Inventory_Organization__c(Name = 'Test'); 
        insert inOrg;
        
        GD_Item_Master__c imas = new GD_Item_Master__c(GD_Item_Code__c= '001122', GD_Unit_Price__c = 1, GD_Inventory__c = inOrg.id);
        insert imas;
        
        GD_Order_Line_Item__c oli = new GD_Order_Line_Item__c(name = 'test',GD_Unit_Price__c = 10, GD_Quantity__c = 2,GD_Item__c = imas.id, GD_Vat__c = 2);
        oli.GD_Sales_Order__c = ord.id;
        insert oli;
        
        
        
        ApexPages.StandardController sc = new ApexPages.standardController(oli);
        GD_SalesOrderPrintableView controller = new GD_SalesOrderPrintableView(sc); 
        
    }
    
}

my class is following here 
 
/*
Description : Generate PDF in Sales order Details Page.
*/


public with sharing class GD_SalesOrderPrintableView {
    
    public string so_Id = apexpages.currentpage().getparameters().get('id');
    public List<GD_Order_Line_Item__c> accList{get;set;}
    public List<GD_Address__c> addList{get;set;}
    set<id> accId = New set<id>();
    public GD_SalesOrderPrintableView (ApexPages.StandardController controller){
        accList = [select id,name ,GD_Unit_Price__c,GD_Total_Amount_Formula__c, GD_Quantity__c,GD_Item__r.GD_Item_Code__c, GD_Vat__c, GD_Discount__c from GD_Order_Line_Item__c where GD_Sales_Order__c =: so_Id];
        GD_Order__c so = [select id,name,GD_Account__c from GD_Order__c where id =:so_Id]; 
        string acc = so.GD_Account__c;
        system.debug('Account Name ++ ' + acc);
        addList = [select id,name,GD_Type__c,GD_Account__c,GD_Address_1__c,GD_Address_2__c,GD_Address_3__c from GD_Address__c where GD_Account__c =:acc and  GD_Type__c = 'BILL_TO' Limit 1];
    }
}

Thanks in advance,
Regards,
Soundar.​​​​​​​
Best Answer chosen by Soundar Raj Ponpandi
Nikhil_KhetanNikhil_Khetan
Soundar,

It seems there is a error on the line 14, data is not getting queried. You need to fix the code and test class.
Also, i am not sure what the process builder was doing, so you should fix it instead of deactivating it.

Use list instance instead of object instance.

Thanks,
Nikhil
Please mark this as best answer if you find my answer's helpful.

All Answers

Nikhil_KhetanNikhil_Khetan
@soundar

It seems that you have a process builder named as “Sales Order” which is failing while inserting the test records.
What is this process about? Check if there is any specific field you are referring in this process builder, if so, make sure to fill that data in your test class order instance.

Thanks,
Nikhil
Please mark this as best answer if it was helpful!
Soundar Raj PonpandiSoundar Raj Ponpandi
Dear Nikhil,

Yes that's right , Thanks for your help.

Now it's showing following error 
 
Error Message	: System.QueryException: List has no rows for assignment to SObject
Stack Trace	    : Class.GD_SalesOrderPrintableView.<init>: line 14, column 1
                  Class.GD_SalesOrderPrintableViewTest.testData: line 30, column 1


Can you help me to resolve this issue ?

Regards,
Soundar.
 
Nikhil_KhetanNikhil_Khetan
you need to add this code in your test class"

PageReference pageRef = Page.YOURVFNAME;
Test.setCurrentPage(pageRef);
pageRef.getParameters().put('id',ord.id);
Soundar Raj PonpandiSoundar Raj Ponpandi
Hi Nikhil,

code coverage is 62% after deactivating the process builder, Still i have same issue I have Page reference in end of the test class.

Can you help me why this query is not covered ?

User-added image

Regards,
Soundar.
Nikhil_KhetanNikhil_Khetan
Soundar,

It seems there is a error on the line 14, data is not getting queried. You need to fix the code and test class.
Also, i am not sure what the process builder was doing, so you should fix it instead of deactivating it.

Use list instance instead of object instance.

Thanks,
Nikhil
Please mark this as best answer if you find my answer's helpful.
This was selected as the best answer