0

Encountered this issue run my test class - it definitely worked before last time (back in early 2016) when I deployed the code it was testing so it came as a surprise.

The error is: EXCEPTION_THROWN [36]|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [OwnerId]: [OwnerId]

I modified my code to explicitly define the owner of the record before inserting but to no avail.. Any help would be appreciated! Thanks!

Detail: So this is a simple Test Class and it sets up the records I need for the rest of the method.

@isTest
public class KHOrderTriggerHandlerTest
{
    static testMethod void testCreateLookups() 
    {
        KH_Province__c p = new KH_Province__c (Name = 'DummyProvince',Province_Code__c = '99');
        insert p;
        KH_District__c d = new KH_District__c (District_Code__c  = '9998', Name = 'DummyDist',Province__c=p.Id);
        insert d;


    // Create DSCC, RSCM, RSM, DSL
    system.debug ('Create DSCC, RSCM, RSM, DSL');
    ide_Staff__c RSCM = new ide_staff__c (Name = 'DummyRSCM', Position__c='Regional Supply Chain Coordinator');
    insert RSCM;
    ide_Staff__c DSCC = new ide_staff__c (Name = 'DummyDSCC', Position__c='District Supply Chain Coordinator', Reports_to__c = RSCM.id);
    insert DSCC;
    ide_Staff__c RSM = new ide_staff__c (Name = 'DummyRSM', Position__c='Regional Sales Manager');
    insert RSM;
    ide_Staff__c DSL = new ide_staff__c (Name = 'DummyDSL', Position__c='District Sales Leader', Reports_to__c = RSM.id, Supported_by__c = DSCC.id);
    insert DSL;


    // Create Sanitation Teacher
    system.debug ('Create Sanitation Teacher');
    KH_Sanitation_Teacher__c t = new KH_Sanitation_Teacher__c(Name = 'No Agent', District_Sales_Lead__c = DSL.Id );
    insert t;       // PROBLEM OCCURS HERE, as it triggers an AFTER INSERT (Please See next snippet of code)

// MORE of this going on.... }
}

This is the Handler when a Sanitation Teacher record is created, it creates 5 Child records that points to the new Sanitation Teacher:

public class SanitationTeacherTriggerHandler extends TriggerHandler
{

        private Map<Id, KH_Sanitation_Teacher__c > newMap;

    private Map<Id, KH_Sanitation_Teacher__c > oldMap;
    private List<KH_Sanitation_Teacher__c > triggerNew = new List<KH_Sanitation_Teacher__c >();
    public SanitationTeacherTriggerHandler()
    {
        newMap = (map<Id, KH_Sanitation_Teacher__c >)trigger.newMap;
        oldMap = (map<Id, KH_Sanitation_Teacher__c >)trigger.oldMap;
        triggerNew = (List<KH_Sanitation_Teacher__c >) trigger.new;
    }   
    protected override void afterInsert()
    {            
        CreateOrderStatuses();
    }
    private void CreateOrderStatuses()
    {
        List<kh_order_status__c> ordersToInsert = new List<kh_order_status__c>();
        for (KH_Sanitation_Teacher__c teacher : triggerNew)
        {
            ordersToInsert.add(new kh_order_status__c(Sanitation_Teacher__c = teacher.Id, Name ='UNASSIGNED', OwnerId = userinfo.getUserId() ));
            ordersToInsert.add(new kh_order_status__c(Sanitation_Teacher__c = teacher.Id, Name ='ASSIGNED', OwnerId = userinfo.getUserId() ));
            ordersToInsert.add(new kh_order_status__c(Sanitation_Teacher__c = teacher.Id, Name ='CONFIRMED', OwnerId = userinfo.getUserId() ));
            ordersToInsert.add(new kh_order_status__c(Sanitation_Teacher__c = teacher.Id, Name ='DELIVERED', OwnerId = userinfo.getUserId() ));
            ordersToInsert.add(new kh_order_status__c(Sanitation_Teacher__c = teacher.Id, Name ='CANCELLED', OwnerId = userinfo.getUserId() ));
        }
        for (kh_order_status__c thisOrderStatus : ordersToInsert){
            system.debug ('ST Order Status OwnerId: ' + thisOrderStatus.OwnerId);
        }
        system.debug ('Before Insert'); 
        insert ordersToInsert;
        system.debug ('After Insert');
    }
}

This is the debug log, so it was clear that it was the Insert of these 5 records that is causing the issue (it doesn't happen if I comment it out), but these 5 child records clearly had OwnerIds so I am not sure what Salesforce is complaining about :

  • 16:45:41:076 USER_DEBUG [20]|DEBUG|Create DSCC, RSCM, RSM, DSL
  • 16:45:41:157 USER_DEBUG [32]|DEBUG|Create Sanitation Teacher
  • 16:45:41:204 USER_DEBUG [33]|DEBUG|ST Order Status OwnerId: 005C0000006yfCXIAY
  • 16:45:41:204 USER_DEBUG [33]|DEBUG|ST Order Status OwnerId: 005C0000006yfCXIAY
  • 16:45:41:204 USER_DEBUG [33]|DEBUG|ST Order Status OwnerId: 005C0000006yfCXIAY
  • 16:45:41:204 USER_DEBUG [33]|DEBUG|ST Order Status OwnerId: 005C0000006yfCXIAY
  • 16:45:41:205 USER_DEBUG [33]|DEBUG|ST Order Status OwnerId: 005C0000006yfCXIAY 16:45:41:205 USER_DEBUG [35]|DEBUG|Before Insert

Then it crapped out.

  • Can you check that, any other workflow or, process is associated with this operation – Santanu Boral Jan 26 '17 at 0:40
  • 1
    a side question is why are you setting ownerId in the records at all as they should default to the running user? – cropredy Jan 26 '17 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.