Programatically create Partner Community Accounts using Apex / Salesforce
Step 1: GoTo Account Tab and Create a account name "User Account"
Step 2: On Account Details page click on button "Manage External Account" and the click on "Enable As Partner". Make sure login user profile must have assign role
Then
user users=new users();
users.email='test@gmail.com';
users.firstname='ajay';
users.username='test@gmail.com';
users.lastname='Kumar';
Id p = [select id from profile where name='Partner Community Profile'].id;
account ac= [select id from account where name='User Account' limit 1 ];
Contact objectContact = new Contact(LastName =users.lastname,AccountId = ac.Id);
insert objectContact;
User u = new User(contactId=objectContact.Id,
username=users.username,
firstname=users.firstname,
lastname=users.lastname,
email=users.email ,
communityNickname = users.lastname.substring(0,1) + '_'+Math.random(),
alias = string.valueof(users.lastname.substring(0,1) +
users.lastname.substring(0,1) + Math.random() ).substring(0,5),
profileid =p , emailencodingkey='UTF-8',
languagelocalekey='en_US',
localesidkey='en_US',
IsPrmSuperUser = true,
timezonesidkey='America/Los_Angeles');
insert u;

Comments
Post a Comment