i have controller for vf page, i have written a test class. test class is having two methods where i am succesfully able to run the test for Autocal but for Save method i am getting following error

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Case Owner History-Create” process failed. Give your Salesforce admin these details. An unhandled fault has occurred in this flow
An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information.: []

To avoid this error i have included every field which is used process builder for Case owner history object. still facing the same issue.

I am new to test class writing. i have tried the following test class.

Controller :

public class AutoPopulateExample
    public string fileName{get;set;} 
    public Blob fileBody{get;set;}
    public Contact con{get;set;}
    public Case caseObject{get;set;}
    public Attachment attachmentObject  {
      get {
          if (attachmentObject  == null)
            attachmentObject  = new Attachment();
          return attachmentObject ;
    public ApexPages.StandardController stdCntrlr {get; set;}

    public AutoPopulateExample(ApexPages.StandardController controller)
        con = new Contact();
        caseObject = new case();
        caseObject = (Case)controller.getRecord();
        //Get the Currently logged in community user contact id
        String cntId = [SELECT Id, ContactId, Name FROM User WHERE Id =: UserInfo.getUserId()].ContactId;
        caseObject.ContactId = cntId;
        if(cntId != null)
        //calling autofilling method
        attachmentObject = new Attachment();


    //function is called from actionsupport event

    public void autoCal()

    Id conid = caseObject.ContactId;     // collecting contact id from visualforce page

    List<Contact> conLst = [select id,AccountId,FirstName,LastName,Email,Phone from contact where id=:conid];

    caseObject.Requestor_First_Name__c = conLst[0].FirstName;      
    caseObject.AccountId = conLst[0].AccountId; 
    caseObject.Requestor_Last_Name__c = conLst[0].LastName;
    caseObject.Requester_Email__c = conLst[0].Email; 
    caseObject.Requester_Contact_Number__c = conLst[0].Phone;   

        public PageReference save() {

            // Save the Case
            insert caseObject;

            // Save the Attachment using the Case id as the parent Id
            attachmentObject = new Attachment();
           if(fileBody != null){
            Integer i=0;
            attachmentObject .clear();
            attachmentObject.Body = fileBody; 
            attachmentObject.Name = 'Logo_'+caseObject.id+'.jpeg' ; 
            attachmentObject.ParentId = caseObject.id;             
            insert attachmentObject;  
            pagereference pr = new pagereference('/'+caseObject.id);                           
            return pr;

Test Class :

private class AutoPopulateExampleTest{

    static testmethod void autocal(){

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

     Contact con = new Contact();
        con.FirstName = 'Test';
        con.LastName = 'Contact';
        con.Email = '[email protected]';
        con.Phone = '1234561234';
        Con.AccountId = acc.Id;
        insert con;

     Case caseobject = new Case();
        caseObject.Requestor_First_Name__c = con.FirstName;      
        caseObject.AccountId = con.AccountId; 
        caseObject.Requestor_Last_Name__c = con.LastName;
        caseObject.Requester_Email__c = con.Email; 
        caseObject.Requester_Contact_Number__c = con.Phone;   

    static testmethod void save(){  
         Account acc = new Account();
        acc.Name = 'Test Account';
        insert acc;    

     Contact con = new Contact();
        con.FirstName = 'Test';
        con.LastName = 'Contact';
        con.Email = '[email protected]';
        con.Phone = '1234561234';
        Con.AccountId = acc.Id;
        insert con;

        User u=new User();
        u.LastName = 'testU';
        u.Username = '[email protected]' ;
        u.Email = '[email protected]';
        u.Alias = 'testt' ;
        u.CommunityNickname = 'testt';
        u.TimeZoneSidKey = 'America/Los_Angeles'; 
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'UTF-8' ;
        u.ProfileId = '00e20000000jqBg' ;
        u.LanguageLocaleKey = 'en_US' ;
       // u.ContactId = con.Id;
        insert u;

        Case caseobject = new Case();
         caseobject.AccountId = acc.Id;
         caseobject.ContactId= con.Id;
        caseobject.Subject = 'test';
        caseobject.OwnerId= u.Id;
        insert caseobject;

        Case_Owner_History__c COH = new Case_Owner_History__c();
        COH.Name = 'test123';
        COH.Case__c= caseobject.Id;
        COH.Active__c = True;
        COH.Entry_Time__c = datetime.newInstance(2019, 01, 29, 12, 30, 0);
        COH.Exit_Time__c = datetime.newInstance(2019, 01, 30, 12, 30, 0);
        COH.Status__c = caseobject.Status;
        COH.OwnerId = caseobject.OwnerId;
        insert COH;

        Attachment att=new Attachment();
        att.ParentId = caseobject.Id;
        att.Name = 'Unit Test Attachment';
        att.Body = Blob.valueOf('Unit Test Attachment Body');
        insert att;

Note : I am able to run test class when i deactivate processes, which i don't want. Also after writing test class i can see code coverage for controller as 0%.

  • 1
    You're not calling your controller class AutoPopulateExample from your test class. You need to set page parameters and invoke controller methods. Refer Apex Test Units trailhead. Commented Jan 31, 2019 at 6:31

1 Answer 1


First as mentioned in comments as well. You are not calling your main class. Unless it is a trigger handler we don't need to explicitly call the class after we create the data.

So in your test class

ApexPages.StandardController controller = new ApexPages.StandardController(caseobject);
AutoPopulateExample exa = new AutoPopulateExample(controller );
//then use the assert method to verify you are getting proper response.

Now coming to the second point, You have already checked all the field reference so I suggest you check the condition and in that any field you are using in those conditions. You need to make sure that it contains data or you have added the null check before referring the field.

  • Hi I have added the above lines in save method of test class still it is showing me code coverage as zero. Also for process builder i have checked the condition as well, i am getting same error.
    – Snehal
    Commented Jan 31, 2019 at 8:29
  • @SnehalGaware as before execution the class line you get the exception so you first need to fix that. Can you disable the process builder and test the class. Once the class have good coverage you can spend time to fix the error. Commented Jan 31, 2019 at 8:48
  • i have disabled the process builder, and can see code coverage as 27% only
    – Snehal
    Commented Jan 31, 2019 at 9:04
  • @SnehalGaware so you need to find the lines which are not covering and need to work on them. Commented Jan 31, 2019 at 9:04
  • 1
    @SnehalGaware For that, you need to find the field which is missing in your process builder. If you are unable to find that I suggest you create a new question, attached the process builder screenshot to get better result. Commented Jan 31, 2019 at 9:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .