I am new to Salesforce development and needs some help with the below code. Code Logic is to assign Service Writer (user) to account object in Round Robin. I have created a queue and custom setting to assign the users based on index value. Test class is simple (mandatory fields to insert) but while executing the test class, i am getting the below error:

Can you please help?


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

caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Class.AccountTriggerHandler.populateSWclass: line 38, column 1 Class.AccountTriggerHandler.onBeforeInsert: line 3, column 1

public class AccountTriggerHandler {
    public static void onBeforeInsert(List<Account> newAccounts){
        populateSWclass(newAccounts, null); 
    public static void onBeforeUpdate(List<Account> newAccounts, Map<Id, Account> oldAccountsMap){
        populateSWclass(newAccounts, oldAccountsMap);
 public static void populateSWclass (List<Account> newAccounts, Map<Id, Account> oldAccountsMap){
        for(Account acc: newAccounts){
            if (acc.Region__c == 'Boston'){
             // Get the queue user details 
                List<Group> queues = [SELECT Id,
                                     (Select Id, UserOrGroupId FROM GroupMembers Order By ID ASC) 
                                     FROM Group 
                                     Where Type = 'Queue' AND DeveloperName = 'SW_Boston_Queue'];
                //Custom settings
                SW__c SWA = SW__c.getOrgDefaults();
                Integer userIndex = (SWA.get('User_Index_Boston__c') == null || Integer.valueOf(SWA.get('User_Index_Boston__c')) < -1) ? -1 : Integer.valueOf(SWA.get('User_Index_Boston__c'));
                if(queues.size() > 0 && queues.get(0).GroupMembers.size() > 0){
                    Id queueId = queues.get(0).Id;
                    Integer groupMemberSize = queues.get(0).GroupMembers.size();
                    Integer SWAUserIndex =  (userIndex + 1) >= groupMemberSize ? 0 : userIndex + 1;
                  //update user in account 
                    acc.Service_Writer__c = queues.get(0).GroupMembers.get(SWAUserIndex).UserOrGroupId;
                    userIndex = SWAUserIndex;
                // Update the custom settings user index with the last assigned user
                    SWA.User_Index_Boston__c = userIndex; 
                    update SWA;

1 Answer 1


Custom settings are like SObjects in test classes, in that you do not have access to existing records unless you mark the test class with @IsTest(SeeAllData=true). Therefore SW__c.getOrgDefaults() will not return an existing record that can be updated.

In your case you have three options:

  1. Insert default settings in your test method insert new SW__c (SetupOwnerId = Userinfo.getOrganizationId())
  2. Change update SWA to upsert SWA so that a custom setting is inserted if it doesn't already exist
  3. Add @IsTest(SeeAllData=true) to use your real org default settings
  • 3
    It's worthwhile to note that while #3 is an option, using seeAllData=true is very bad practice. It breaks the concept of "test data isolation", and can lead to hard to debug errors (like deployments between sandboxes working, but not from a sandbox to production). It is a necessary evil in a few, very specific cases. This is not one of them.
    – Derek F
    Commented Aug 15, 2020 at 23:41

You must log in to answer this question.

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