Manage Community Member Programmatically

Add Community Member


Salesforce stores community membership in object called NetworkMemberGroup. Although Salesforce says ‘create’ allowed on Network Member but seems that not allowed using apex code. No worries, we can make a post REST call for the same:

First add your salesforce base url as remote site setting then add this code as part of your implementation:

String endpoint = URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v47.0/sobjects/NetworkMemberGroup';
HttpRequest httpRequest = new HttpRequest();  
httpRequest.setEndpoint(endpoint);  
httpRequest.setMethod('POST'); 
httpRequest.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); 
httpRequest.setHeader('Content-Type', 'application/json;charset=UTF-8'); 

//Change network id and profile/permission set Id
httpRequest.setBody('{"NetworkId":"0DB900000008Qga","ParentId":"00e90000001uNre"}');
Http http = new Http();
HttpResponse httpResponse = http.send(httpRequest);
  • Add community/internal profile id or a permission set as ParentId
  • Provide Community id as NetworkId

Set Default Community for a Profile

Salesforce stores community membership in object called Network Affinity. Although Salesforce says ‘create’ allowed on this object but seems that not allowed using apex code. But we can make a post REST call for the same as previously:

String endpoint = URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v47.0/sobjects/NetworkAffinity';
HttpRequest httpRequest = new HttpRequest();  
httpRequest.setEndpoint(endpoint);  
httpRequest.setMethod('POST'); 
httpRequest.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); 
httpRequest.setHeader('Content-Type', 'application/json;charset=UTF-8'); 

//Change network id and profile/permission set Id
httpRequest.setBody('{"NetworkId":"0DB900000008Qga","ProfileId":"00e90000001uNre"}');
Http http = new Http();
HttpResponse httpResponse = http.send(httpRequest);
  • Add community/internal profile id as ProfileId field
  • Provide Community id as NetworkId

25 thoughts on “Manage Community Member Programmatically”

  1. I know this if off topic but I’m looking into starting my own blog and
    was curious what all is required to get set up?
    I’m assuming having a blog like yours would cost a pretty penny?

    I’m not very internet savvy so I’m not 100% positive.
    Any recommendations or advice would be greatly appreciated.

    Thank you

  2. I have found very interesting ypur article.It’s
    pretty worth enough for me. In my view, if alll website owners and bloggers made good content aas you did, tthe
    web will be a lot more useful than efer
    before.

  3. Hello There. I found your blog using google. This
    is an extremely well written article. I’ll make sure to boolkmark it aand return to read more of your useful
    information. Thanks for thee post. I’ll certainly return.

  4. I havve found vwry interesting your article.It’s pretty
    worth enough for me. In my view, if all website owners and
    bloggers made good content as you did, the web will be a lot more useful than ever before.

  5. Wonderful article! This is the kind of information that are supposed to be shared across the net.

    Shame on Google for no longer positioning this post
    higher! Come on over and seek advice from my site .
    Thanks =)

  6. Fine way of describing, and nice paragraph to obtain facts concerning my presentation focus, which i am going to convey in institution of higher education. games ps4 allenferguson games ps4

  7. whoah this blog is great i like studying your posts.
    Stay up the great work! You already know, many persons are
    hunting round for this information, you could help them greatly.

Comments are closed.