+ Start a Discussion
Érick BSÉrick BS 

SetupClassTest - TriggerClass Validation

Hi! I am new with SF Apex programming, then I am stuck with a simple problem I think so.
After I added a Validation Rule to unable the user saves a new account without entering data in the fields (ShippingCity, ShippingPostalCode, ShippingStreet) I am facing some problems with a trigger test class.

I must be able to put fake values in my trigger test for these new fields that now are required field.

Could you help me, pls?
 
@isTest
    static void UpdateAccountProspect1() {        
        Boolean result = true;
        try{
            User testUser = SetupClassTest.createUserSalesRep('2801','testadmin@testorg.com', 'testadmin@testorg.com.prod');
            System.runAs(testUser){
                RecordTypeInfo rt = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Prospect_2801');
                List<Account> accToUpdate = SetupClassTest.generateNAccountsWithRT(1, rt.getRecordTypeId(), '2801');
                accToUpdate[0].RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Customer_2801').getRecordTypeId();
                update accToUpdate[0];
            }
        }catch(Exception e){
            System.debug('Línea: ' + e.getStackTraceString());
            result = false;
        }
        System.assert(result);
    }



 
Best Answer chosen by Érick BS
Maharajan CMaharajan C
Hi Erick,

You can find one more apex class named SetupClassTest in your org. In that class find the method generateNAccountsWithRT  there you can add the new values for Account.

If you have any confusion please post the code here.

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Erick,

You can find one more apex class named SetupClassTest in your org. In that class find the method generateNAccountsWithRT  there you can add the new values for Account.

If you have any confusion please post the code here.

Thanks,
Maharajan.C
This was selected as the best answer
Érick BSÉrick BS
Hi Maharajan! 

First of all, thank you for you help!

I will share with you my code... I can´t use this method... I tried the follow code, but I receive an error "Class.AccountTriggerHandlerTest.UpdateAccountAsSystemAdmin: line 243, column 1"
 
@isTest
    public static void UpdateAccountAsSystemAdmin() {
              test.startTest();
        Boolean result = true;
        try{
            User testUser = SetupClassTest.createUserSystemAdmin('2801', 'testSysAdm@testorg.com', 'testSysAdm@testorg.com.prod');
            if(testUser != null){
                System.runAs(testUser){
                    list<Account> accToUpdate = SetupClassTest.generateNAccountsWithRT(1,Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get(Constants.ACCOUNT_CUSTOMER_RECORDTYPE_NAME_PREFIX + '_2801').getRecordTypeId(),'2801');
                    
                    system.debug('Approval_status: ' + accToUpdate[0].approval_status__c + ', Account_status: ' + accToUpdate[0].account_status__c);
                    
                    accToUpdate[0].name = 'AccountTestSysAdmin';
                    accToUpdate[0].ShippingCity = 'Málaga';
                    accToUpdate[0].ShippingPostalCode = '1234-123';
                    accToUpdate[0].ShippingStreet = 'Rua Lisboa do Porto 123';
                    update accToUpdate[0];
                    
                    system.debug('Approval_status: ' + accToUpdate[0].approval_status__c + ', Account_status: ' + accToUpdate[0].account_status__c);
                    
                    List<account> acc = [select id, approval_status__c, account_status__c from account where id=:accToUpdate[0].id];
                    if(acc[0].account_status__c != constants.ACCOUNT_ACOUNT_STATUS_IN_APPROVAL){
                        result = false;       
                    }
                    
                }
            }else{
                result = false;
            }
        }catch(Exception e){
            result = false;
        }

        System.assert(result);
        test.stopTest();
    }

 
Érick BSÉrick BS
Hi Maharajan! 
This was so obvious that I did not see this when I look at for the first time! 
But now I can add new values always as I want to.
Thanks very much!