1

I'm trying to add users into the workspace as a contentworkspacemember through apex. Suppose a current user is trying to create a content,

1.I need to check weather the current user has the permission to manage Salesforce CRM content. 2.Then,If user is not a member in that workspace,need to add the user.

I tried developing the below code,sorry for the bad coding! Could anyone help me out here.

  Id idContentWorkspace = [SELECT Id FROM ContentWorkspace WHERE Name = 'Test' LIMIT 1].Id;

list<ContentWorkspaceMember> ContentWorkspaceMemberadd = new list<ContentWorkspaceMember>();
Id Userid = UserInfo.getUserId();
for(ContentWorkspaceMember iterator : [SELECT ContentWorkspaceId,ContentWorkspacePermissionId FROM ContentWorkspaceMember 
                                        WHERE ContentWorkspaceid =:idContentWorkspace]) {
    ContentWorkspaceMemberadd.add(new ContentWorkspaceMember(ContentWorkspaceId = iterator.ContentWorkspaceId,
    ContentWorkspacePermissionId=iterator.ContentWorkspacePermissionId,MemberId=Userid));
}

if(!ContentWorkspaceMemberadd.isEmpty() && ContentWorkspaceMemberadd != null) {
    Database.upsert(ContentWorkspaceMemberadd, false);

}
0

Please check below code

Id Userid = UserInfo.getUserId();
List<ContentWorkspace> contentWorkSpaces = [SELECT Id, Name FROM ContentWorkspace];
List<ContentWorkspaceMember> ContentWorkspaceMemberadd = new List<ContentWorkspaceMember>();
for (ContentWorkspaceMember iterator : [
        SELECT
                ContentWorkspaceId,
                ContentWorkspacePermissionId
        FROM ContentWorkspaceMember
        WHERE ContentWorkspaceId IN:contentWorkSpaces
]) {
    ContentWorkspaceMemberadd.add(
            new ContentWorkspaceMember(
                    ContentWorkspaceId = iterator.ContentWorkspaceId,
                    ContentWorkspacePermissionId = iterator.ContentWorkspacePermissionId,
                    MemberId = Userid)
    );
}
if (!ContentWorkspaceMemberadd.isEmpty() && ContentWorkspaceMemberadd != null) {
    Database.upsert(ContentWorkspaceMemberadd, false);
}
1
  • Please do not post Code only Answers. Instead, provide an explanation to help the OP understand what was wrong with their original code and how your solution addresses the problem.
    – glls
    Mar 26 '21 at 12:14

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.