Aish
Aish

Reputation: 67

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 :


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?

Upvotes: 1

Views: 1816

Answers (3)

EricSSH
EricSSH

Reputation: 447

I eyedballed this, but this should get you going

public static void newRecord(List<Account> listAccounts) 
{
    List<Account> accountsToUpdate = new List<Account>();

    for(Account a : listAccounts) 
    {
        String message;
        String hashDigest;

        Account account = new Account();

        if(a.Customer_Email__pc != null)
        {
            message = String.valueof(a.get('Customer_Email__pc'));
            hashDigest = CryptoUtil.generateHashDigest(message);
            account.Hashed_email__pc = HashDigest;
            account.ID = a.ID;
            accountsToUpdate.add(account);
        }          
    }

    if(!accountsToUpdate.isEmpty())
    {
       update accountsToUpdate;
    }
}



@isTest
private class Test_Account
{   
    // -------- Variables --------
    private static List<Account> testAccount;

    // -------- Shared Methods --------
    // Initialization of test data example
    private static void init(Integer testType)
    {
        testAccount = new List<Account>();

        if(testType == 1)
        {
            for(Integer i = 0; i < 10; i++)
            {
                Account a = new Account();
                //build your accounts..
                testAccount.add(a);
            }

            insert testAccount;
        }
    }

    // -------- Test Methods --------
    private static testMethod void testAccountHash()
    {
        init(1);

        Test.startTest();
        //Because this is an actual public method, I would just test the method

        newRecord(testAccount);

        Test.stopTest();

        List<Account> accountResult = [SELECT .... FROM Account];


        //Assert
        System.assertNotEquals(...)

    }
}

Upvotes: 1

Arun Kumar
Arun Kumar

Reputation: 407

Are you wontedly pushing the PB as inactive? Or is it active in your package? Are you using Eclipce/Migration tool or change set to push the code? If PB is active in your package, then it could be a org version issue as Scott mentioned. A workaround will be to directly test your class in a test class without relying on PB. You can do CRUD on the object record and simulate your PB logic in test class to fully test your code.

Upvotes: 0

Scott_in_London
Scott_in_London

Reputation: 131

We have seen issues with deploying inactive PBs. Do make sure the deployment failure of PB is not because the sandbox was on a newer version than production - we run into that during the new-release preview windows, when our sandboxes are often on the upcoming release but prod is not.

We have started to write test classes to cover our process builders. You should be able to write tests to test the expected system behaviour handled through PB. Example: on a record update, your class updates various things and your PB updates various other things and sends an email alert. Your test class can be extended to cover the updates which PB makes and to check that it sends an email when expected.

Hope this helps.

Upvotes: 0

Related Questions