无法在Salesforce中使用process builder部署apex类

无法在Salesforce中使用process builder部署apex类,salesforce,apex-code,processbuilder,apex,Salesforce,Apex Code,Processbuilder,Apex,我想部署一个包,该包包含一个apex类、一个测试类和一个调用apex类的process builder。在我开发这个类的沙箱中,代码覆盖率是100%。当我尝试将它部署到另一个沙箱/产品时,它失败了,因为它显示代码覆盖率为65% 我认为问题是因为process builder在部署时处于非活动状态,因此没有涵盖整个代码。我应该如何进行这项工作 我已经尝试做了以下工作: 在部署类之前,首先部署process builder以激活它。部署process builder失败。 在没有process bu

我想部署一个包,该包包含一个apex类、一个测试类和一个调用apex类的process builder。在我开发这个类的沙箱中,代码覆盖率是100%。当我尝试将它部署到另一个沙箱/产品时,它失败了,因为它显示代码覆盖率为65%

我认为问题是因为process builder在部署时处于非活动状态,因此没有涵盖整个代码。我应该如何进行这项工作

我已经尝试做了以下工作:

在部署类之前,首先部署process builder以激活它。部署process builder失败。 在没有process builder的情况下部署类;代码覆盖率为65%。 更改测试类以适应更多情况。这是不可能的,因为我将代码更改为使用process builder,并且找不到测试它的方法。 当process builder被激活和停用时,我运行了测试代码。它在停用时显示65%的覆盖率,在激活时显示100%的覆盖率,正如在测试类中插入记录时调用process builder一样。 我的代码接收客户电子邮件,并使用CryptoUtil.generateHashDigest方法将其转换为哈希,然后将其保存在哈希电子邮件字段中

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;
    }
}
为了使用process builder,我必须创建插入/更新的帐户记录的克隆。使用此方法,仅在克隆中进行更改。如果未使用process builder,则测试类将在散列的\u email\u pc字段中获取一个空值,而不是实际的散列值,这将导致测试失败。使用process builder时,在克隆中所做的更改将反映在实际记录中,并且测试通过。即使我没有调用这段代码的测试方法,当process builder覆盖它时,测试也会通过

我无法找到一种方法来创建一个测试类,当process builder被停用时,在该测试类中返回正确的值。我必须使用DML插入记录,以便可以克隆它


在这种情况下,我应该如何测试apex类?

我们已经看到了部署非活动PBs的问题。请确保PB的部署失败不是因为沙盒的版本比生产版更新——我们在新版本预览窗口中遇到了这种情况,而我们的沙盒通常在即将发布的版本中,而prod则不是

我们已经开始编写测试类来覆盖我们的流程构建器。您应该能够编写测试来测试通过PB处理的预期系统行为。示例:在记录更新时,您的类更新各种内容,您的PB更新各种其他内容并发送电子邮件警报。您的测试类可以扩展,以涵盖PB所做的更新,并检查它是否在需要时发送电子邮件


希望这有帮助。

您是否习惯将PB设为非活动状态?或者它在您的软件包中处于活动状态? 您是否使用ECLICE/迁移工具或变更集来推送代码? 如果PB在您的包中是活动的,那么它可能是Scott提到的组织版本问题。
一种解决方法是直接在测试类中测试类,而不依赖于PB。您可以在对象记录上执行CRUD,并在测试类中模拟PB逻辑,以全面测试代码。

我注意到了这一点,但这应该可以让您继续

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(...)

    }
}

当我按下PB时,它在我的包中处于活动状态。我正在使用变更集。我已经用代码更新了我的问题,这解释了为什么直接测试类是不可能的。您知道解决方法吗?此代码不正确,如果您加载的帐户超过101个,则无法正常工作,需要进行修复。我计划将此代码用于101个以上的帐户。应该如何改进代码以避免出现该问题?process builder仍然可以使用它吗?使用asynchronous apex可以吗?您需要研究bulkifacation