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?
Error:
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;
}
}
}
}