0
votes

Suppose in Account object, it have 3 custom fields, there are invoice_delivery_method, invoice_delivery_email and invoice_delivery_print. For invoice_delivery_method, the type is picklist, and the possible values are Email, Email and Print, Print and Other. The rest of two custom fields are checkbox, and default to unchecked, which is false.

Now when user update the invoice_delivery_method field to Email, (either by Salesforce account page or by SOQL), the invoice_delivery_email is set to true, and invoice_delivery_print is set to false.

The way I did is create a trigger class like following:

trigger InvoiceDeliveryMethodTrigger on Account (before update) {
    InvoiceDeliveryMethodTriggerHandler.handleBeforeUpdate(Trigger.new);
}

Inside the handler class I did following:

public class InvoiceDeliveryMethodTriggerHandler {
    public static void handleBeforeUpdate(Account[] accounts){
        RecordType recordType = [select Id from RecordType where sobjecttype = 'Account' and Name =: MSSP_Settings__c.getOrgDefaults().Account_Record_Type__c];  
        for (Account account : accounts) {
            if(account.RecordTypeId == recordType.Id) {
                System.debug('Information for Account: ' + account);
                System.debug('Information for Invoice Delivery Method: ' + account.Invoice_Delivery_Method__c);
                account.Invoice_Delivery_Email__c = false;
                account.Invoice_Delivery_Print__c = false;
                String delivery_method = account.Invoice_Delivery_Method__c;
                System.debug('String is not blank ' + String.isNotBlank(delivery_method));
                if (String.isNotBlank(delivery_method)){
                    if (delivery_method.equals('Email')){
                        account.Invoice_Delivery_Email__c = true;
                        account.Invoice_Delivery_Print__c = false;
                    }
                    else if (delivery_method.equals('Email and Mail')){
                        account.Invoice_Delivery_Email__c = true;
                        account.Invoice_Delivery_Print__c = true;
                    }
                    else if (delivery_method.equals('Mail')){
                        account.Invoice_Delivery_Email__c = false;
                        account.Invoice_Delivery_Print__c = true;                   
                    }  
                }
            }
        }
    }
}

I also have one trigger class on Account after update, but I didn't change any value for that 3 custom field.

If I test via application, it seems that two custom field is got updated based on the value of invoice_delivery_method. But I got problems for my unit testing.

Here is the unit test class I wrote

    @isTest
    private class InvoiceDeliveryMethodTest {
        @isTest(SeeAllData=true)
        static void testAccountEmailSelected(){
            Account testAccount = new Account();
            // populating some of the mandatory field for Account
            testAccount.Invoice_Delivery_Method__c = 'Other';
            insert testAccount;        


            Account acct = [Select Id, Invoice_Delivery_Method__c, Invoice_Delivery_Email__c, Invoice_Delivery_Print__c from Account 
                            where Id =: testAccount.Id];
            acct.Invoice_Delivery_Method__c = 'Email';
            update acct;
            acct = [Select Id, Invoice_Delivery_Method__c, Invoice_Delivery_Email__c, Invoice_Delivery_Print__c from Account 
                    where Id =: testAccount.Id];    
            System.assertEquals('Email', acct.Invoice_Delivery_Method__c);
            System.assert(acct.Invoice_Delivery_Email__c);
            System.assert(!acct.Invoice_Delivery_Print__c);
            delete testAccount;

        }
}

When I run the test case, it fail on System.assert(acct.Invoice_Delivery_Email__c);

That fields still false. Why this happen?

2

2 Answers

0
votes

First off, I think the simpler solution might be to use formulas instead of a trigger.

If you still want to go the trigger route I would suggest to not use seealldata.

That said the only thing I can really think of is the record type. When creating the account, can you assert that the record type Id is what you are expecting it to be?

0
votes

OK. I got it. Here is the way I did.

First remove all the seealldata=true in the isTest annotation.

Then according to http://sfdcsrini.blogspot.com/2014/07/how-to-readcreate-record-types-in-apex.html, I create an setup method with @testSetup to create Account data and the recordType.

In the test method, I select the account that was created in setup(), change the value for invoice_delivery_method and update the account. Then re-select again, and do the assert check. Once all the assert check passed, delete that account object, so it keep only 1 account object to use for next test method.