0

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 ;
        }
      set;
      }
    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
         autoCal();
        }
        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];

        if(conLst.isEmpty())
        {
          return;
        }    
    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
            System.debug('@@@@@fileBody'+fileBody);     
            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 :

@isTest
private class AutoPopulateExampleTest{

    static testmethod void autocal(){
       test.startTest(); 

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

     Contact con = new Contact();
        con.FirstName = 'Test';
        con.LastName = 'Contact';
        con.Email = 'test@test.com';
        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;   
      test.stopTest();   
        }

    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 = 'test@test.com';
        con.Phone = '1234561234';
        Con.AccountId = acc.Id;
        insert con;

        User u=new User();
        u.LastName = 'testU';
        u.Username = 'gsunpeth@test.com' ;
        u.Email = 'test@test.com';
        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.Status='Open';
        caseobject.Priority='Low';
        caseobject.Origin='Email';
        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. – Rohit Mourya Jan 31 at 6:31
0

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 );
exa.autoCal();
exa.save();
//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. – Admin Jan 31 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. – Tushar Sharma Jan 31 at 8:48
  • i have disabled the process builder, and can see code coverage as 27% only – Admin Jan 31 at 9:04
  • @SnehalGaware so you need to find the lines which are not covering and need to work on them. – Tushar Sharma Jan 31 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. – Tushar Sharma Jan 31 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.