After I moved a user from a selected group (Queue Marketing) for example, to the left or right side of the dual list box.
-left side has: List of Users from the Org where Queue MKT is not part of
-right side: List of Users that belong to the Queue MKT.
I am not able to save these changes, I get this error in the console:
Error{"status":500,"body":{"message":"Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Group ID: id value of incorrect type: 00509000009355mAAA: [GroupId]"},"headers":{}}
The Apex method is the following:
@AuraEnabled
public static String addRemoveUsers(String groupId, List <String> selectedUsers, List<String> updatedUsers,
Boolean userUpdatedBool){
List<GroupMember> listUserInsert = new List <GroupMember>();
Set<String> setUserIds =new Set<String>();
try{
Set <String> setSelectedUsers = new Set < String >();
Set <String> setUpdatedUsers = new Set < String >();
setSelectedUsers.addAll( selectedUsers );
system.debug('selectedUsers'+selectedUsers);
setUpdatedUsers.addAll( updatedUsers );
system.debug('updatedUsers'+updatedUsers);
if(userUpdatedBool){
for(String strUser:setUpdatedUsers){
if(!setSelectedUsers.contains(strUser)){
GroupMember UserFromGroup = new GroupMember();
UserFromGroup.UserOrGroupId = groupId;
UserFromGroup.Id = strUser;
listUserInsert.add(UserFromGroup);
}
}
for(String strUser:setSelectedUsers){
if(!setUpdatedUsers.contains(strUser)){
setUserIds.add(strUser);
system.debug('SetGroupId'+strUser);
}
}
}
if(setUserIds.size()>0){
delete [SELECT Id
FROM GroupMember
WHERE GroupId =:groupId
AND UserOrGroupId
IN:setUserIds];
}
if(listUserInsert.size()>0){
insert listUserInsert;
}
return 'Successful';
}catch (Exception e){
throw new AuraHandledException( e.getMessage() );
}
}
I think my mistake is related with the data Base I'm calling, however I struggle to understand if on this case I need to use User or GroupMember, I'll appreciate if someone can share their experience with me.
if(listUserInsert.size()>0){
is no longer necessary in modern Apex.