function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aishwarya MAHAJAN 8Aishwarya MAHAJAN 8 

Cannot deploy apex class with process builder in Salesforce

I want to deploy a package that has an apex class, test class and a process builder that calls the apex class. On the sandbox where I developed the class, the code coverage is 100%. When I tried to deploy it to another sandbox/production, it failed because it showed the code coverage to be 65%.

I believe the issue is because the process builder is inactive when it is deployed and the entire code is not covered as a result. How should I proceed with this?

I have already tried to do the following :
  • Deploy the process builder first to activate it before deploying the classes. Deploying the process builder failed.
  • Deploy the classes without the process builder; code coverage was 65%.
  • Change the test class to accommodate more cases. This was not possible as I changed the code to work with process builder and I cannot find a way to test it.
  • I ran the test code when the process builder was activated and deactivated. It showed 65% coverage when it was deactivated, and 100% coverage when it was activated, as the process builder is called when a record is inserted in the test class.
My code takes the customer email and converts it to a hash using CryptoUtil.generateHashDigest method and then saves it in the hashed email field.
 
Public static void newRecord(List<Account> listAccounts) {
    for(Account a : listAccounts) {
        Account updacc=[select id from account where id = :a.id];
        String message = String.valueof(a.get('Customer_Email__pc'));
        String hashDigest = CryptoUtil.generateHashDigest(message);
        updacc.Hashed_email__pc = HashDigest;
        update updacc;
    }
}

I had to create a clone of the account record inserted/updated in order to use process builder. Using this method, the changes are only made in the clone. If process builder is not used, the test class gets a Null value instead of the actual hash value in the Hashed_email__pc field which leads to the test failing. When process builder is used, the changes made in the clone are reflected in the actual record, and the test passes. Even if I do not have a test method calling this section of code, the test passes as the process builder covers it.

I cannot figure out a way of creating a test class where the correct values are returned when the process builder is deactivated. I have to use DML to insert the record, so that it can be cloned.

How should I test the apex class in this case?
 
Best Answer chosen by Aishwarya MAHAJAN 8
Shashikant SharmaShashikant Sharma
Hi,

Change your test class to query the account again from database to so you get the Hashed_email__pc value.
 
@isTest static void testInsertingNewRecordEmailNotNull() {
        List<Account> acct = new List<Account>();
        acct.add(new Account(LastName= 'Smith', Customer_Email__pc= 'abc123@gmail.com'));
        insert acct;                                                           
        CryptoUtil.newRecord(acct);
        

        // query form DB and have a new instance
       Account acc = [ Select Hashed_email__pc From Account Where Id =: acct.get(0).Id ];

        System.assertEquals('ZSev/yqjeUZX0vLKGhRot2XibFC3gE4qw3VtZHZhFHQ=', 
acc.Hashed_email__pc);
    

}
Thanks
Shashikant
 

All Answers

Shashikant SharmaShashikant Sharma
Hi Aishwarya,

You just need a vey simple Test Class where you need to Insert a Account with populating Customer_Email__pc field on account. Then Call this method directly newRecord passwing the account somethign like below. 

 
@isTest

private class AccountMethodTest
{
    static testMethod void AccountMethodTest()
    { 
       
       Account a = new Account();
       a.Name  = 'Test';
       insert a;
       

      List<Account> accs = new List<Account>{a};
      //then call the new Record method passing accs 
       
    }
    
}
Let me know if you face issues.

Thanks
Shashikant
Aishwarya MAHAJAN 8Aishwarya MAHAJAN 8
Hi Shashikant,

I had created a test function which is very similar to the one you have posted, the only addition was that I had inserted the customer email along with the name when creating the new record and I had included a system.assert statement at the end.

Here, 2 things happen
  • The process builder gets called on insert which covers the code
  • The new record method is called manually. Here the new record method returns a null value in the hashed email field instead of the actual value since the changes are only made in the clone of the record in the newRecord method. This leads to the System.assert failing(This compares the actual and expected value in the hashed_email__pc field). 
When the process builder is inactive, only the scenario in the second point happens which leads to the test failing. 

The overall code coverage for the class gets reduced to 65% since this part of the class does not get covered.
Shashikant SharmaShashikant Sharma
Hi,

Could you please share your Test Class code.

Thanks
Aishwarya MAHAJAN 8Aishwarya MAHAJAN 8
Hi! I am sorry for the late reply. Here is my test code. This does not work as the hashed email field has null, instead of the required hash/value.

@isTest static void testInsertingNewRecordEmailNotNull() {
        List<Account> acct = new List<Account>();
        acct.add(new Account(LastName= 'Smith', Customer_Email__pc= 'abc123@gmail.com'));
        insert acct;                                                           
        CryptoUtil.newRecord(acct);
        System.assertEquals('ZSev/yqjeUZX0vLKGhRot2XibFC3gE4qw3VtZHZhFHQ=', acct[0].Hashed_email__pc);
    }
Shashikant SharmaShashikant Sharma
Hi,

Change your test class to query the account again from database to so you get the Hashed_email__pc value.
 
@isTest static void testInsertingNewRecordEmailNotNull() {
        List<Account> acct = new List<Account>();
        acct.add(new Account(LastName= 'Smith', Customer_Email__pc= 'abc123@gmail.com'));
        insert acct;                                                           
        CryptoUtil.newRecord(acct);
        

        // query form DB and have a new instance
       Account acc = [ Select Hashed_email__pc From Account Where Id =: acct.get(0).Id ];

        System.assertEquals('ZSev/yqjeUZX0vLKGhRot2XibFC3gE4qw3VtZHZhFHQ=', 
acc.Hashed_email__pc);
    

}
Thanks
Shashikant
 
This was selected as the best answer
Aishwarya MAHAJAN 8Aishwarya MAHAJAN 8
Hi Shashikant,

It is working now! Thank you so much. This has been a huge help.

Thanks again,
Aishwarya
Pallavi mangal 3Pallavi mangal 3
Hi, 

Can you tell me the resolution? How you resolved this issue. I'm facing same issue.

Thanks,
Pallavi