0

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.

1
  • if(listUserInsert.size()>0){ is no longer necessary in modern Apex.
    – sfdcfox
    Oct 5, 2021 at 16:15

1 Answer 1

0

You're assigning the wrong values to the record.

UserFromGroup.UserOrGroupId = strUser;
UserFromGroup.GroupId = groupId;

Do not assign a value to Id when inserting records.

2
  • I've tried your suggestion, but I keep getting the same error. Could it be that this query has a mistake too: ` if(setUserIds.size()>0){delete [SELECT Id FROM GroupMember WHERE GroupId =:groupId AND UserOrGroupId IN:setUserIds]; } ` @sfdcfox
    – j1992b
    Oct 5, 2021 at 19:51
  • @jbuch It looks fine to me. By the way, you really should use Id instead of String if you mean Id values.
    – sfdcfox
    Oct 5, 2021 at 20:02

You must log in to answer this question.

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