Tag Archives: Community

Assign a Default Community to a User Profile Programmatically

Salesforce stores default community to a User Profile in NetworkAffinity object. Using apex we cannot insert the default community to a User Profile. We can make a REST API call to add default community to a User Profile.

Sample Code:

//Get Endpoint URL
String endpointURL = URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v48.0/sobjects/NetworkAffinity';
HttpRequest req = new HttpRequest();  
req.setEndpoint(endpointURL);  
req.setMethod('POST');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId()); 
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
//Add Community Id as NetworkId and Profile/Permission Id
req.setBody('{"NetworkId":"0DB1I000000TP59WAG","ProfileId":"00e1I000001uXJUQA2"}');
Http http = new Http();
HttpResponse response = http.send(req);
System.debug('response-' + response);

Note: Add your salesforce base URL as remote site settings and then execute above code in developer console anonymous window.

Add Salesforce Community Member Programmatically

Salesforce stores community members in NetworkMemberGroup object. Using apex we cannot insert the community member. So we can make a REST API call to add community member.

Sample Code:

//Get Endpoint URL
String endpointURL = URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v48.0/sobjects/NetworkMemberGroup';
HttpRequest req = new HttpRequest();  
req.setEndpoint(endpointURL);  
req.setMethod('POST');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId()); 
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
//Add Community Id as NetworkId and Profile Id as ParentId
req.setBody('{"NetworkId":"0DB1I000000TP59WAG","ParentId":"00e1I000001uXJUQA2"}');
Http http = new Http();
HttpResponse response = http.send(req);
System.debug('response-' + response);

Note: Add your salesforce base URL as remote site settings and then execute above code in developer console anonymous window.

Get Salesforce Community Page URL Parameters in Lightning Component

Community URL:
https://testcommunity.my.salesforce.com/communityname/ProfilePage?id=00Q0I00000xUjMH&name=biswajeet

Lightning JS Controller:

({
    getParamValue: function(component, event, helper) {

        //Get Id Parameter Value From Community URL
        var idParamValue = helper.getURLParameterValue().id;
        console.log('Id-' + idParamValue);

        //Get Name Parameter Value From Community URL
        var nameParamValue = helper.getURLParameterValue().name;
        console.log('Name-' + nameParamValue);
    }
})

Lightning JS Helper:

({
    getURLParameterValue: function() {

        var querystring = location.search.substr(1);
        var paramValue = {};
        querystring.split("&").forEach(function(part) {
            var param = part.split("=");
            paramValue[param[0]] = decodeURIComponent(param[1]);
        });

        console.log('paramValue-' + paramValue);
        return paramValue;
    }
})