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. An impressive share! I have just forwarded this onto a
    colleague who had been doing a little homework on this.

    And he actually bought me breakfast simply because I stumbled upon it for him…
    lol. So allow me to reword this…. Thank YOU for the
    meal!! But yeah, thanks for spending time to talk about this
    matter here on your blog.

  2. I got this web page from my buddy who shared with me
    about this web page and at the moment this time I am browsing this web page and reading very informative
    content here.

  3. After I originally commented I seem to have clicked on the
    -Notify me when new comments are added- checkbox and from now on every time a comment is added I recieve four
    emails with the exact same comment. Is there an easy method
    you are able to remove me from that service? Cheers!

  4. If you wish for to increase your familiarity just keep visiting this website
    and be updated with the latest news posted here.

Comments are closed.