Created
July 18, 2012 14:24
-
-
Save dancinllama/3136488 to your computer and use it in GitHub Desktop.
Creating a portal user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Account createPortalAccount(){ | |
User u = [Select Id From User Where Id=:UserInfo.getUserId()]; | |
Id roleId = [Select Id From UserRole Where PortalType='None' Limit 1].Id; | |
User accountOwner = null; | |
System.runAs(u){ | |
Map<String,Object> acctOwnerInputParams = new Map<String,Object>(); | |
acctOwnerInputParams = new Map<String,Object>(); | |
acctOwnerInputParams.put('UserRoleId',roleId); | |
acctOwnerInputParams.put('Username','[email protected]'); | |
accountOwner = createUser(acctOwnerInputParams); | |
} | |
Map<String,Object> acctParams = new Map<String,Object>(); | |
acctParams.put('OwnerId',accountOwner.Id); | |
Account a = createCompany(acctParams); | |
insert a; | |
return a; | |
} | |
/** | |
* @description create a mock portal user | |
* @param Map<String,String> map of field / value pairs to set on the user initially | |
* @return a mock portal user record (Batman!) | |
*/ | |
public static User createPortalUser(Map<String,Object> inputParams){ | |
if(inputParams == null){ | |
inputParams = new Map<String,Object>(); | |
} | |
String portalUserName = '[email protected]'; | |
if(inputParams.containsKey('Suffix')){ | |
String suffix = (String)inputParams.get('Suffix'); | |
portalUserName = 'batmanportal'+suffix+'@wayneenterprises.com'; | |
inputParams.remove('Suffix'); | |
} | |
Map<String,Object> cInputParams = new Map<String,Object>(); | |
if(!inputParams.containsKey('AccountId')){ | |
Account a = createPortalAccount(); | |
cInputParams.put('AccountId',a.Id); | |
}else{ | |
System.debug('JWL: 2nd portal user accountid: ' + inputParams.get('AccountId')); | |
cInputParams.put('AccountId',inputParams.get('AccountId')); | |
inputParams.remove('AccountId'); | |
} | |
Contact c = createSingleContact(cInputParams); | |
Id profileId = [Select Id From Profile Where Name='PSC/Partnerlink Portal Custom' Limit 1].Id; | |
inputParams.put('ContactId',c.Id); | |
inputParams.put('ProfileId',profileId); | |
if(!inputParams.containsKey('Username')){ | |
inputParams.put('Username',portalUsername); | |
} | |
return createUser(inputParams); | |
} | |
/** | |
* @description create a user | |
* @param Map<String,String> map of field / value pairs to set on the user initially | |
* @param boolean whether to insert the record or not | |
* @return a mock user record (Batman!) | |
*/ | |
public static User createUser(Map<String,Object> inputParams, boolean insertUser){ | |
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; | |
User u = new User( | |
Alias = 'batman', | |
Email='[email protected]', | |
EmailEncodingKey='UTF-8', | |
Firstname='Bruce', | |
Lastname='Wayne', | |
LanguageLocaleKey='en_US', | |
LocaleSidKey='en_US', ProfileId = p.Id, | |
TimeZoneSidKey='America/Chicago', | |
Username='[email protected]'); | |
if(inputParams != null){ | |
for(String key : inputParams.keySet()){ | |
u.put(key,inputParams.get(key)); | |
} | |
} | |
System.debug('JWL: user: ' + u); | |
if(insertUser){ | |
insert u; | |
} | |
return u; | |
} | |
/** | |
* @description create a user | |
* @param Map<String,String> map of field / value pairs to set on the user initially | |
* @return a mock user record (Batman!) | |
*/ | |
public static User createUser(Map<String,Object> inputParams){ | |
return createUser(inputParams,true); | |
} | |
/** | |
* @description create a contact | |
* @return a mock contact record | |
*/ | |
public static Contact createSingleContact(Map<String, Object> inputParams){ | |
Contact c = createContact(inputParams); | |
insert c; | |
return c; | |
} | |
public static Contact createContact(Map<String,Object> inputParams){ | |
Contact c = new Contact(Email='[email protected]',Lastname='Tester',FirstName='Tester'); | |
if(inputParams != null){ | |
for(String key : inputParams.keySet()){ | |
c.put(key, inputParams.get(key)); | |
} | |
} | |
return c; | |
} | |
/** | |
* @description creates (does not insert) a company/account | |
*/ | |
public static Account createCompany(Map<String, Object> inputParams){ | |
Account company = new Account(); | |
company.Name = 'Test Company'; | |
//Set incoming values | |
if(inputParams != null){ | |
for(String key : inputParams.keySet()){ | |
company.put(key, inputParams.get(key)); | |
} | |
} | |
return company; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment