How To Create Partner User In Salesforce

Hello #Trailblazers,
In this blog post, we will talk about very important concept for test class. We all know that we need to insert either normal user or community users inside test class. So, we will see how we can create the users for both the types.
Create User in Test Class
Create a Test Utility Class and then use the below method inside Utility class.
public static User createUser(String roleId, String profId, String firstName, String lastName){ User tempUser = prepareUser(roleId, profId, firstName, lastName); return tempUser; } private static User prepareUser(String roleId, String profId, String firstName, String lastName) { String orgId = UserInfo.getOrganizationId(); String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-',''); Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000)); String uniqueName = orgId + dateString + randomInt; User tempUser = new User( FirstName = firstName, LastName = lastName, email = uniqueName + '@sfdc' + orgId + '.org', Username = uniqueName + '@sfdc' + orgId + '.org', EmailEncodingKey = 'ISO-8859-1', Alias = uniqueName.substring(18, 23), TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', LanguageLocaleKey = 'en_US', ProfileId = profId ); if( String.isBlank(roleId) == false ){ tempUser.UserRoleId = roleId; } return tempUser; }
There are two methods inside the class one is createUser which we will call inside our TestClass and second one is being used by our method and the method takes 4 parameters
- roleId – The Id of the Role if Applicable otherwise pass blank string
- profileId – The Id of the Profile and it is Mandatory to pass
- firstName – FirstName of the User
- lastName – LastName of the User
Create Community User in Test Class
As we know that to create the Community User we need to have an Account Record and a Contact Record so first, we will create Contact & Account record using the below code. Use the below code inside your test utility class and then call from your test class.
public static Account createAccount(String Name){ Account portalAccount = new Account(name = Name ); return portalAccount; } public static Contact createContact(String firstName, String lastName, String email, String accountId){ Contact portalContact = new contact( FirstName = firstName, LastName = lastName, Email = email, AccountId = accountI ); return portalContact; }
There are two methods in the utility class that takes the parameters and then return the Contact & Account Record. Call the Methods and then perform the insert operation.
Note:- There might be more required fields so please make sure you are providing all required fields for Account & Contact.
Now, Use below method inside your test Utility class to create the Community User
public static User createCommunityUser(String contactId, String profId, String firstName, String lastName) { User tempUser = prepareUser('', profId, firstName, lastName); tempUser.ContactId = contactId; tempUser.AccountId = accountId; return tempUser; }
As you can see that the above method is taking one new parameters for Contact Id.
Here is the Complete code for the apex Unit test class.
@isTest public class AcountHelperClassTest { @isTest public static void createUser(){ Id profileId = [Select Id From Profile Where Name ='System Administrator'].Id; User u = TestUtility.createUser('',profileId,'Amit','Singh'); System.runAs( u ){ Account acc = TestUtility.createAccount('Test Account'); insert acc; } } @isTest public static void createCommunityUser(){ /* Query Community User Profile */ Id profileId = [Select Id From Profile Where Name ='Customer Community Plus Login User'].Id; Account acc = TestUtility.createAccount('Test Account'); insert acc; Contact con = TestUtility.createContact('test','Contact','[email protected]',acc.Id); insert con; User u = TestUtility.createCommunityUser(con.Id,profileId,'Amit','Singh'); } }

You can find the Complete code for both the Classes here
Hope this helps you.
Thanks for reading and Happy Learning 🙂
#SharingIsCaring #Salesforce #DeveloperGeeks
Related
SFDC Panther
Hey, my name is Amit Singh and I am Salesforce MVP having 14X Salesforce certified professionals working as freelancers. I love to solve the queries in the Salesforce Success Community and Developer forum. I am in love with Salesforce because of its continuous improvement. Inject trailhead into your blood and you will be a salesforce champion.
How To Create Partner User In Salesforce
Source: https://www.sfdcpanther.com/how-to-create-community-user-in-test-class/
Posted by: kingparrived.blogspot.com
0 Response to "How To Create Partner User In Salesforce"
Post a Comment