0

I am trying to create a functionality to clone existing user, wherein i want to assign Public groups, queue and permission sets too. All the features along with profile and role id i would be assigning to new user, just by clicking a button named clone. Here i am not able to assign Queue and permission set of existing user to new one.

Please find below my method which is throwing an error, apart from this i have just created a VF page wherein i am taking a mirror id to clone and user is created first before assigning all details from existing user:

public PageReference cloneuser()
{
    User newus = [Select id from user where Bid__C =: bid];
    mirrorId = newus.Id;
    User newUser = createUser();
    User mirrorUser = getMirrorUserDetails();
    newUser.ProfileId =  mirrorUser.ProfileId;
    newUser.UserRoleId =  mirrorUser.UserRoleId;
    List <GroupMember> g = new List<GroupMember>();  
    g = [SELECT GroupId,UserOrGroupId FROM GroupMember Where UserOrGroupId = :mirrorId ];
    for(GroupMember g1: g){
        g1.UserOrGroupId = newUser.Id;
    }

    PermissionSet ps = new PermissionSet();
    ps = [SELECT Id,Label,Name FROM PermissionSet Where ProfileId = :mirrorUser.ProfileId];
    List <PermissionSetAssignment> psa = new List<PermissionSetAssignment>();
    psa = [SELECT PermissionSetId,AssigneeId FROM PermissionSetAssignment Where AssigneeId = :mirrorId ];
    for(PermissionSetAssignment psa1: psa){
        psa1.AssigneeId  = newUser.Id;
        psa1.PermissionSetId = ps.Id;
    } 

    update newUser;
    return null;     
}

For UserOrGroupId and AssigneeId, i am getting below error:

System.SObjectException: Field is not writeable: PermissionSetAssignment.AssigneeId

  • 2
    What's the question? – Martin Lezer Mar 29 '17 at 8:25
  • Hi Martin, I am trying to create a functionality to clone existing user, wherein i want to assign Public groups, queue and permission sets too. All the features along with profile and role id i would be assigning to new user, just by clicking a button named clone. Here i am not able to assign Queue and permission set of existing user to new one. – N.jadhav Mar 29 '17 at 10:01
  • 1
    All you've done is repeat what was in your original question which doesn't tell us where your code failing or where you need assistance? Have you written a test class for you code? What are you trying to accomplish? You can't clone a user. Sounds like you want to "deep clone" the user's permissions. Please read How do I ask a good question. – crmprogdev Mar 29 '17 at 13:05
  • 1
    @crmprogdev Added method and error too – N.jadhav Mar 29 '17 at 13:54
2

You are trying to update the existing GroupMember and PermissionSetAssignment records, rather than adding new ones. If you want to remove the old records, you will have to delete and replace, as they do not allow reparenting (based on your error message). You will also have to act on these related objects, not the User record itself. Something more like:

List<GroupMember> newGroupMembers = new List<GroupMember>();
for (GroupMember member : [
    SELECT GroupId FROM GroupMember WHERE UserOrGroupId = :mirrorUser
]){
    newGroupMembers.add(new GroupMember(
        GroupId=member.GroupId, UserOrGroupId=newUserId
    ));
}
insert newGroupMembers;

Same idea for PermissionSetAssignment:

List<PermissionSetAssignment> newAssignments = new List<PermissionSetAssignment>();
for (PermissionSetAssignment assignment : [
    SELECT PermissionSetId FROM PermissionSetAssignment WHERE AssigneeId = :mirrorUser
]){
    newAssignments.add(new PermissionSetAssignment(
        PermissionSetId=assignment.PermissionSetId, AssigneeId=newUserId
    ));
}
insert newAssignments;
  • Perfect @Adrian Just 1 more clarification, inserting solved the re-parenting issue? What i was doing is direct assignment? correct. And please a bit detail on re-parenting. Thanks – N.jadhav Mar 29 '17 at 14:27
  • For queues and public group it worked, but for permission set getting below error : System.DmlException: Insert failed. First exception on row 1; first error: INVALID_CROSS_REFERENCE_KEY, You can't assign or unassign this permission set because it's associated with a profile – N.jadhav Mar 31 '17 at 9:50
0

There are two types of permission set, please refer below link: https://help.salesforce.com/articleView?id=000233111&type=1 Directly assigning profile owned permissions were giving us the errorr: System.DmlException: Insert failed. First exception on row 1; first error: INVALID_CROSS_REFERENCE_KEY, You can't assign or unassign this permission set because it's associated with a profile.....

So added a where clause: PermissionSet.IsOwnedByProfile=false in below query:

List newAssignments = new List

  • List<PermissionSetAssignment> newAssignments = new List<PermissionSetAssignment(); for (PermissionSetAssignment assignment : [SELECT PermissionSetId,AssigneeId FROM PermissionSetAssignment WHERE AssigneeId = :mirrorID AND PermissionSet.IsOwnedByProfile=false]){ newAssignments.add(new PermissionSetAssignment(PermissionSetId=assignment.PermissionSetId, AssigneeId=newUserId)); } insert newAssignments; – N.jadhav Mar 31 '17 at 11:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.