0

I've been trying for a few days to get this to work and but I've run out of ideas. I'm stuck. I can't get this test class to work. I get the following error.

Trigger

trigger efs_Sales_OrderTrigger on SCMC__Sales_Order__c (after update, after insert) 
{
    if(trigger.isAfter && trigger.isInsert)
        efs__.EgnyteSyncQueueTrigger.onAfterInsert();
    else if(trigger.isAfter && trigger.isUpdate)
        efs__.EgnyteSyncQueueTrigger.onAfterUpdate();
}

Test Class

@isTest
public class efs_Sales_OrderTest {
    static testMethod void mytestmethod(){
        Test.startTest();
        String uniqueUserName = 'systemadmin' + DateTime.now().getTime() + '@testorg.com';
        Profile p = [SELECT Id FROM Profile WHERE Name='ACI System Admin'];
        User u = new User(Alias = 'standt', Email='[email protected]',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
        UserName=uniqueUserName);
        
        SCMC__PO_Payment_Terms__c popt = new SCMC__PO_Payment_Terms__c();
        popt.SCMC__Terms_Name__c = 'N180';
        insert popt;
        SCMC__PO_Payment_Terms__c pt = [SELECT Id FROM SCMC__PO_Payment_Terms__c WHERE SCMC__Terms_Name__c = 'N180' LIMIT 1];
        
        Account  acc = new Account();
        acc.Name = 'ACME';
        acc.SCMC__Active__c = true;
        acc.SCMC__Customer__c = true;
        acc.SCMC__Currency__c = c.Id;
        acc.SCMC__Payment_Terms__c = pt.Id;
        acc.SCMC__Corp_Line1__c = '374 Hamilton Road';
        acc.SCMC__Corp_City__c = 'New Hamburg';
        acc.SCMC__Corp_PostalCode__c = 'N3A 2K2';
        insert acc;
        Account a = [SELECT Id FROM Account WHERE Name = 'ACME' LIMIT 1];
        
        RecordType rt = [SELECT Id, Name from RecordType where Name = 'Inventory' AND SObjectType = 'SCMC__Sales_Order__c'];

        System.runAs(u) {
            SCMC__Sales_Order__c app = new SCMC__Sales_Order__c();
            app.SCMC__Customer_Account__c = a.Id;
            app.SCMC__Customer_Purchase_Order__c = '12345678';
            app.RecordTypeId = rt.Id;
            insert app;
            update app;
        }
        Test.stopTest();
    }
}

Error

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SCMC.SalesOrders: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.SCMC.SalesOrders.validations: line 251, column 1
Class.SCMC.SalesOrders.onBeforeInsert: line 44, column 1
Class.SCMC.fflib_SObjectDomain.handleBeforeInsert: line 144, column 1
Class.SCMC.fflib_SObjectDomain.triggerHandler: line 314, column 1
Class.SCMC.fflib_SObjectDomain.triggerHandler: line 270, column 1
Trigger.SCMC.SalesOrders: line 9, column 1: []

I'm at a loss at this point.

1
  • so - the problem is in SCMC.SalesOrders.validations class on line 251- most likely because the mock Sales Order you are creating is missing a field value.
    – cropredy
    Commented Jun 18, 2020 at 13:55

2 Answers 2

0

This is because somewhere in your trigger execution you are trying yo update the same record twice, you can add bypass in your triggers when you do not need to "re-update" a record, for example:

public class AccountTriggerClass {
    public static Boolean bypass = false;
    public static void isBeforeInsert(List<Account> accounts) {
     if(!bypass){
      AccountTriggerClass.updateSomething(accounts);
     }
    }
    public static void updateSomething(List<Account> accounts) {
    ...
    }

}

public class AnotherClass {
   public static void anotherMethod(List<Account> accounts) {
      AccountTriggerClass.bypass = true;
     insert accounts;
      AccountTriggerClass.bypass = false;
    }
}

This is really useful when you have a lot of triggers and you only need to execute at certain times.

0

Got it to work.

I should have waited for before posting the question. Our instance was already in the queue to have our FinancialForce software updated. Once that was completed, there was more information available in the error message that made me understand that I needed to create a Currence Master record in order to create a test Sales Order.

Adding the additional line with regards to the SCMC__Currency_Master__c object finally got it to work.

    @isTest
    public class efs_Sales_OrderTest {
      static testMethod void mytestmethod(){
        Test.startTest();
        String uniqueUserName = 'systemadmin' + DateTime.now().getTime() + '@testorg.com';
        Profile p = [SELECT Id FROM Profile WHERE Name='ACI System Admin'];
        User u = new User(Alias = 'standt', Email='[email protected]',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
        UserName=uniqueUserName);
        
        SCMC__Currency_Master__c cm = new SCMC__Currency_Master__c();
        cm.Name = 'USD';
        cm.SCMC__Active__c = true;
        cm.SCMC__Corporate_Currency__c = true;
        insert cm;
        SCMC__Currency_Master__c c = [SELECT Id FROM SCMC__Currency_Master__c WHERE Name = 'USD' LIMIT 1];
        
        Account  acc = new Account();
        acc.Name = 'ACME';
        acc.SCMC__Active__c = true;
        acc.SCMC__Customer__c = true;
        acc.SCMC__Currency__c = c.Id;
        acc.SCMC__Corp_Line1__c = 'One Infinity Loop';
        acc.SCMC__Corp_City__c = 'Cupertino';
        acc.SCMC__Corp_PostalCode__c = '12345';
        insert acc;
        Account a = [SELECT Id FROM Account WHERE Name = 'ACME' LIMIT 1];
        
        RecordType rt = [SELECT Id, Name from RecordType where Name = 'Inventory' AND SObjectType = 'SCMC__Sales_Order__c'];

        System.runAs(u) {
            SCMC__Sales_Order__c app = new SCMC__Sales_Order__c();
            app.SCMC__Customer_Account__c = a.Id;
            app.SCMC__Customer_Purchase_Order__c = '12345678';
            app.RecordTypeId = rt.Id;
            insert app;
            update app;
        }
        Test.stopTest();
      }
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .