Salesforce: Log Into Community As User

Scenario: You’ve been using Salesforce for some time and, being a progressive person, have jumped aboard the Lightning Experience train with both feet. However, there’s one feature you keep having to go back into Classic for, and it kinda drives you nuts: Log Into Community As User. It’s such a simple button, but it makes life so much easier, and you really notice it when it’s gone, don’t ya?

The solution, at least until Salesforce brings the functionality into Lightning (which I imagine they’ll do any day now), is creating a lighting component, an Apex controller, and dragging your component onto the Contact page builder.

It’s not so complicated, I promise. Create your component and apex class, and copy-and-paste the code below into the appropriate files.

LoginToCommunityAsUser.cmp

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId" controller="LoginToCommunityAsUser_Controller">
	<aura:attribute type="String" name="MyAccountId" />
	<aura:attribute type="String" name="UserAccountId" />
	<aura:attribute type="String" name="setupId" />
	<aura:attribute type="String" name="userId" />
	<aura:attribute name="recordId" type="Id" />
	<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

	<button class="slds-button slds-button_brand slds-hide" aura:id="button" onclick="{!c.click}">Login to Community as User</button>
</aura:component>

LoginToCommunityAsUserController.js

({
	doInit : function(component, event, helper) {
		var recordId = component.get('v.recordId');
		helper.getOrgId(component).then(function(r) {
			component.set('v.MyAccountId', r);
		});
		helper.getAccountId(component).then(function(r) {
			component.set('v.UserAccountId', r);
		});
		helper.getSetupId(component).then(function(r) {
			component.set('v.setupId', r);
		});
		helper.getUserId(component).then(function(r) {
			component.set('v.userId', r);
			$A.util.removeClass(component.find('button'),'slds-hide');
		});
	},

	click : function(component, event, helper) {
		helper.loginToCommunityAsUser(component);
	}
})

LoginToCommunityAsUserHelper.js

({
	navigateToUrl : function(a) {
		"undefined" !== typeof sforce && "undefined" !== typeof sforce.one ? sforce.one.navigateToURL(a) : window.location = a
	},

	getOrgId : function(component) {
		return new Promise(function(resolve, reject) {
			var action = component.get('c.getOrgId');
			action.setCallback( this, function( response ) {
				resolve( response.getReturnValue() );
			});
			$A.enqueueAction( action );
		});
	},

	getAccountId : function(component) {
		var action = component.get('c.getAccountId'),
				recordId = component.get('v.recordId');
		return new Promise(function(resolve, reject) {
			console.log(recordId);
			action.setParams({
				"recordId" : recordId
			});
			action.setCallback( this, function( response ) {
				console.log(response);
				resolve( response.getReturnValue() );
			});
			$A.enqueueAction( action );
		});
	},

	getSetupId : function(component) {
		return new Promise(function(resolve, reject) {
			var action = component.get('c.getSetupId'),
					recordId = component.get('v.recordId');
			action.setParams({
				"recordId" : recordId
			});
			action.setCallback( this, function( response ) {
				console.log(response);
				resolve( response.getReturnValue() );
			});
			$A.enqueueAction( action );
		});
	},

	getUserId : function(component) {
		return new Promise(function(resolve, reject) {
			var action = component.get('c.getUserId'),
					recordId = component.get('v.recordId');
			action.setParams({
				"recordId" : recordId
			});
			action.setCallback( this, function( response ) {
				resolve( response.getReturnValue() );
			});
			$A.enqueueAction( action );
		});
	},

	loginToCommunityAsUser : function(component) {
		var self = this,
				MyAccountId = component.get('v.MyAccountId'),
				CustomerAccountId = component.get('v.UserAccountId'),
				setupId = component.get('v.setupId'),
				userId = component.get('v.userId');

		self.navigateToUrl('/servlet/servlet.su?oid=' + MyAccountId + '&retURL=' + UserAccountId + '&sunetworkid=' + setupId + '&sunetworkuserid=' + userId);
	}

})

LoginToCommunityAsUser_Controller.cls

public with sharing class LoginToCommunityAsUser_Controller {
	@AuraEnabled
	public static String getOrgId() {
		return UserInfo.getOrganizationId().mid(0,15);
	}

	@AuraEnabled
	public static String getAccountId(String recordId) {
		String id = [select AccountId from Contact WHERE Id =  :recordId].AccountId;
		return id;
	}

	@AuraEnabled
	public static String getSetupId(String recordId) {
		Id userId = getUserId(recordId);
		return String.valueOf([SELECT NetworkId FROM NetworkMember WHERE MemberId = :userId AND Network.Status = 'Live' LIMIT 1]).mid(26,15);
	}

	@AuraEnabled
	public static String getUserId(String recordId) {
		String accountId = [SELECT AccountId FROM Contact WHERE Id = :recordId LIMIT 1].AccountId;
		return [SELECT Id FROM User WHERE AccountId = :accountId AND ContactId = :recordId LIMIT 1].Id;
	}
}

Now, all you have to do is drag your brand new component onto your Contact page in page page builder, and you should be good to roll. May this be as useful to you as it is to me.

Written on September 25, 2017