You need to sign in to do that
Don't have an account?
salesforce explorer
while creating user manager is not getting map and permission sets are also not assigning to user
public class BMCRF_NewUserCreation
{
public static string description = '';
public static string firstName = '';
public static string Alias = '';
public static string Email = '';
public static string UserName = '';
public static string lastName = '';
public static string WorkerType = '';
public static string EmployeeType = '';
public static string Position = '';
public static string Manager = '';
public static string EmployeeID = '';
public static string Company = '';
public static string Division = '';
public static string Location = '';
public static string Department = '';
public static ID userManagerId;
public static list<user> usercreationList = new list<user>();
public static list<id> taskIDList = new list<id>();
public static Profile prof = [SELECT Id FROM Profile WHERE Name = 'ServiceDesk Client'limit 1];
public static ID profileId = prof.id;
//===========================================================================================================================================================
public static ID getManagerId(string mName)
{
user uName = [SELECT Id,name FROM User where name =: mName limit 1];
return uName.id;
}
//===========================================================================================================================================================
@invocableMethod
public static void createUser(list<ID> IDList)
{
taskIDList.addAll(IDList);
BMCRF_NewUserCreation.setUserInfo();
}
//===========================================================================================================================================================
public static void setUserInfo()
{
//list<BMCServiceDesk__Task__c> tskList = [Select BMCServiceDesk__taskDescription__c from BMCServiceDesk__Task__c where id IN: taskIDList];
list<BMCServiceDesk__Task__c> tskList = [Select BMCServiceDesk__taskDescription__c from BMCServiceDesk__Task__c where Name = '00076155'];
for(BMCServiceDesk__Task__c tsk : tskList)
{
description = tsk.BMCServiceDesk__taskDescription__c;
User newUser = new User();
if(description.contains(firstName))
{
firstName = description.substringAfter('First Name:');
firstName = firstName.substringBefore('\n');
firstName.trim();
newUser.FirstName = firstName;
}
if(description.contains(lastName))
{
lastName = description.substringAfter('Last Name:');
lastName = lastName.substringBefore('\n');
lastName.trim();
newUser.LastName = lastName;
}
UserName = firstName+'.';
UserName = UserName+lastName.trim();
Email = UserName +'@starrcompanies.com';
UserName = UserName+'@starrcompanies.com';
Alias = firstName.substring(1,2)+lastName.substring(1,5);
newUser.Alias = Alias;
if(description.contains(WorkerType))
{
WorkerType = description.substringAfter('Worker Type:');
WorkerType = WorkerType.substringBefore('\n');
WorkerType.trim();
newUser.Title = WorkerType;
newUser.Worker_Type__c = WorkerType;
}
if(description.contains(EmployeeType))
{
EmployeeType = description.substringAfter('Employee Type:');
EmployeeType = EmployeeType.substringBefore('\n');
EmployeeType.trim();
newUser.Employee_CW_Type__c = EmployeeType;
}
if(description.contains(Position))
{
Position = description.substringAfter('Position:');
Position = Position.substringBefore('\n');
Position.trim();
}
if(description.contains(Manager))
{
Manager = description.substringAfter('Manager:');
Manager = Manager.substringBefore('\n');
Manager.trim();
userManagerId = getManagerId(Manager);
newUser.ManagerId = userManagerId;
}
if(description.contains(EmployeeID))
{
EmployeeID = description.substringAfter('Employee ID:');
EmployeeID = EmployeeID.substringBefore('\n');
EmployeeID.trim();
newUser.BMCRF_Employee_Id__c = EmployeeID;
}
if(description.contains(Company))
{
Company = description.substringAfter('Company:');
Company = Company.substringBefore('\n');
Company.trim();
newUser.CompanyName = Company;
}
if(description.contains(Division))
{
Division = description.substringAfter('Division:');
Division = Division.substringBefore('\n');
Division.trim();
newUser.Division = Division;
}
if(description.contains(Department))
{
Department = description.substringAfter('Cost Center:');
Department = Department.substringBefore('\n');
Department.trim();
newUser.Department = Department;
}
if(description.contains(Location))
{
Location = description.substringAfter('Location:');
Location = Location.substringBefore('\n');
Location.trim();
newUser.City = Location;
}
newUser.Email = Email;
newUser.EmailEncodingKey = 'UTF-8';
newUser.LanguageLocaleKey = 'en_US';
newUser.LocaleSidKey = 'en_US';
newUser.TimeZoneSidKey='America/New_York';
newUser.ProfileId = profileId;
newUser.Username = UserName;
newUser.CommunityNickname = firstName+'.'+lastName;
newUser.IsActive=true;
newUser.FederationIdentifier = firstName+'.'+lastName;
usercreationList.add(newUser);
}
try
{
//insert usercreationList;
Database.SaveResult[] results = Database.insert(usercreationList, false);
for(Database.SaveResult result : results)
{
if(result.isSuccess())
{
System.debug('Successfully inserted User : ' + result.getId());
}
else
{
for(Database.Error error : result.getErrors())
{
System.debug('Error Status : ' + error.getStatusCode() + ' : ' + 'Error Fields : ' + error.getFields());
}
}
}
system.debug('User is created....');
assignPermissionSet();
}
catch(DmlException e)
{
System.debug('The following exception has occurred: ' + e.getMessage());
}
DisplayInfo();
assignPermissionSet();
}
//============================================================================================================================================================
//@future(callout=true)
public static void assignPermissionSet()
{
//========================================================Permission Set License Assign===================================================================
list<PermissionSetLicenseAssign> PermissionSetLicenseList = new list<PermissionSetLicenseAssign>();
PermissionSetLicense PermissionSetLicense = [SELECT id, MasterLabel FROM PermissionSetLicense where MasterLabel = 'Identity Connect'];
for(user userId : usercreationList)
{
PermissionSetLicenseAssign PermissionSetLicenseAssign = new PermissionSetLicenseAssign();
PermissionSetLicenseAssign.AssigneeId = userID.id;
PermissionSetLicenseAssign.PermissionSetLicenseId = PermissionSetLicense.id;
PermissionSetLicenseList.add(PermissionSetLicenseAssign);
}
//insert PermissionSetLicenseList;
try
{
//insert usercreationList;
Database.SaveResult[] results = Database.insert(PermissionSetLicenseList, false);
for(Database.SaveResult result : results)
{
if(result.isSuccess())
{
System.debug('Successfully inserted User : ' + result.getId());
}
else
{
for(Database.Error error : result.getErrors())
{
System.debug('Error Status : ' + error.getStatusCode() + ' : ' + 'Error Fields : ' + error.getFields());
}
}
}
}
catch(DmlException e)
{
System.debug('The following exception has occurred: ' + e.getMessage());
}
//========================================================Permission set Assignment===================================================================
string IdentityConnect = 'IdentityConnect';
string MobileDeviceFormAccess = 'Mobile_Device_Form_Access';
string ServiceDeskClient = 'ServiceDesk_Client';
string ServiceDeskClientMobile = 'ServiceDesk_Client_Mobile';
list<PermissionSet> permissions = [SELECT Id, Name FROM PermissionSet WHERE Name =: IdentityConnect OR Name =: MobileDeviceFormAccess OR Name =: ServiceDeskClient OR Name =: ServiceDeskClientMobile];
List<PermissionSetAssignment> permissionSetAssignmentsList = new List<PermissionSetAssignment>();
system.debug('permissions : '+ permissions);
for(User user: usercreationList)
{
for(PermissionSet permission: permissions)
{
system.debug('permissions : '+ permissions);
PermissionSetAssignment permissionSetAssignment = new PermissionSetAssignment();
if(permission.name == IdentityConnect)
{
permissionSetAssignment.PermissionSetId = permission.Id;
system.debug('IdentityConnect : '+ permission.Id);
}
else if(permission.name == MobileDeviceFormAccess)
{
permissionSetAssignment.PermissionSetId = permission.Id;
system.debug('MobileDeviceFormAccess : '+ MobileDeviceFormAccess);
}
else if(permission.name == ServiceDeskClient)
{
permissionSetAssignment.PermissionSetId = permission.Id;
system.debug('ServiceDeskClient : '+ permission.Id);
}
else if(permission.name == ServiceDeskClientMobile)
{
permissionSetAssignment.PermissionSetId = permission.Id;
system.debug('ServiceDeskClientMobile : '+ permission.Id);
}
permissionSetAssignment.AssigneeId = user.ID;
permissionSetAssignmentsList.add(permissionSetAssignment);
}
}
//insert permissionSetAssignmentsList;
try
{
Database.SaveResult[] results = Database.insert(permissionSetAssignmentsList, false);
for(Database.SaveResult result : results)
{
if(result.isSuccess())
{
System.debug('Successfully inserted User : ' + result.getId());
}
else
{
for(Database.Error error : result.getErrors())
{
System.debug('Error Status : ' + error.getStatusCode() + ' : ' + 'Error Fields : ' + error.getFields());
}
}
}
}
catch(DmlException e)
{
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
//============================================================================================================================================================
public static void DisplayInfo()
{
System.debug('firstName : '+ firstName);
System.debug('lastName : '+ lastName);
System.debug('WorkerType : '+ WorkerType);
System.debug('EmployeeType : '+ EmployeeType);
System.debug('Position : '+ Position);
System.debug('Manager : '+ Manager);
System.debug('ManagerID : '+ userManagerId);
System.debug('EmployeeID : '+ EmployeeID);
System.debug('Company : '+ Company);
System.debug('Division : '+ Division);
System.debug('Location : '+ Location);
System.debug('Department : '+ Department);
System.debug('Email : '+ Email);
System.debug('Alias : '+ Alias);
System.debug('UserName : '+ UserName);
}
}
{
public static string description = '';
public static string firstName = '';
public static string Alias = '';
public static string Email = '';
public static string UserName = '';
public static string lastName = '';
public static string WorkerType = '';
public static string EmployeeType = '';
public static string Position = '';
public static string Manager = '';
public static string EmployeeID = '';
public static string Company = '';
public static string Division = '';
public static string Location = '';
public static string Department = '';
public static ID userManagerId;
public static list<user> usercreationList = new list<user>();
public static list<id> taskIDList = new list<id>();
public static Profile prof = [SELECT Id FROM Profile WHERE Name = 'ServiceDesk Client'limit 1];
public static ID profileId = prof.id;
//===========================================================================================================================================================
public static ID getManagerId(string mName)
{
user uName = [SELECT Id,name FROM User where name =: mName limit 1];
return uName.id;
}
//===========================================================================================================================================================
@invocableMethod
public static void createUser(list<ID> IDList)
{
taskIDList.addAll(IDList);
BMCRF_NewUserCreation.setUserInfo();
}
//===========================================================================================================================================================
public static void setUserInfo()
{
//list<BMCServiceDesk__Task__c> tskList = [Select BMCServiceDesk__taskDescription__c from BMCServiceDesk__Task__c where id IN: taskIDList];
list<BMCServiceDesk__Task__c> tskList = [Select BMCServiceDesk__taskDescription__c from BMCServiceDesk__Task__c where Name = '00076155'];
for(BMCServiceDesk__Task__c tsk : tskList)
{
description = tsk.BMCServiceDesk__taskDescription__c;
User newUser = new User();
if(description.contains(firstName))
{
firstName = description.substringAfter('First Name:');
firstName = firstName.substringBefore('\n');
firstName.trim();
newUser.FirstName = firstName;
}
if(description.contains(lastName))
{
lastName = description.substringAfter('Last Name:');
lastName = lastName.substringBefore('\n');
lastName.trim();
newUser.LastName = lastName;
}
UserName = firstName+'.';
UserName = UserName+lastName.trim();
Email = UserName +'@starrcompanies.com';
UserName = UserName+'@starrcompanies.com';
Alias = firstName.substring(1,2)+lastName.substring(1,5);
newUser.Alias = Alias;
if(description.contains(WorkerType))
{
WorkerType = description.substringAfter('Worker Type:');
WorkerType = WorkerType.substringBefore('\n');
WorkerType.trim();
newUser.Title = WorkerType;
newUser.Worker_Type__c = WorkerType;
}
if(description.contains(EmployeeType))
{
EmployeeType = description.substringAfter('Employee Type:');
EmployeeType = EmployeeType.substringBefore('\n');
EmployeeType.trim();
newUser.Employee_CW_Type__c = EmployeeType;
}
if(description.contains(Position))
{
Position = description.substringAfter('Position:');
Position = Position.substringBefore('\n');
Position.trim();
}
if(description.contains(Manager))
{
Manager = description.substringAfter('Manager:');
Manager = Manager.substringBefore('\n');
Manager.trim();
userManagerId = getManagerId(Manager);
newUser.ManagerId = userManagerId;
}
if(description.contains(EmployeeID))
{
EmployeeID = description.substringAfter('Employee ID:');
EmployeeID = EmployeeID.substringBefore('\n');
EmployeeID.trim();
newUser.BMCRF_Employee_Id__c = EmployeeID;
}
if(description.contains(Company))
{
Company = description.substringAfter('Company:');
Company = Company.substringBefore('\n');
Company.trim();
newUser.CompanyName = Company;
}
if(description.contains(Division))
{
Division = description.substringAfter('Division:');
Division = Division.substringBefore('\n');
Division.trim();
newUser.Division = Division;
}
if(description.contains(Department))
{
Department = description.substringAfter('Cost Center:');
Department = Department.substringBefore('\n');
Department.trim();
newUser.Department = Department;
}
if(description.contains(Location))
{
Location = description.substringAfter('Location:');
Location = Location.substringBefore('\n');
Location.trim();
newUser.City = Location;
}
newUser.Email = Email;
newUser.EmailEncodingKey = 'UTF-8';
newUser.LanguageLocaleKey = 'en_US';
newUser.LocaleSidKey = 'en_US';
newUser.TimeZoneSidKey='America/New_York';
newUser.ProfileId = profileId;
newUser.Username = UserName;
newUser.CommunityNickname = firstName+'.'+lastName;
newUser.IsActive=true;
newUser.FederationIdentifier = firstName+'.'+lastName;
usercreationList.add(newUser);
}
try
{
//insert usercreationList;
Database.SaveResult[] results = Database.insert(usercreationList, false);
for(Database.SaveResult result : results)
{
if(result.isSuccess())
{
System.debug('Successfully inserted User : ' + result.getId());
}
else
{
for(Database.Error error : result.getErrors())
{
System.debug('Error Status : ' + error.getStatusCode() + ' : ' + 'Error Fields : ' + error.getFields());
}
}
}
system.debug('User is created....');
assignPermissionSet();
}
catch(DmlException e)
{
System.debug('The following exception has occurred: ' + e.getMessage());
}
DisplayInfo();
assignPermissionSet();
}
//============================================================================================================================================================
//@future(callout=true)
public static void assignPermissionSet()
{
//========================================================Permission Set License Assign===================================================================
list<PermissionSetLicenseAssign> PermissionSetLicenseList = new list<PermissionSetLicenseAssign>();
PermissionSetLicense PermissionSetLicense = [SELECT id, MasterLabel FROM PermissionSetLicense where MasterLabel = 'Identity Connect'];
for(user userId : usercreationList)
{
PermissionSetLicenseAssign PermissionSetLicenseAssign = new PermissionSetLicenseAssign();
PermissionSetLicenseAssign.AssigneeId = userID.id;
PermissionSetLicenseAssign.PermissionSetLicenseId = PermissionSetLicense.id;
PermissionSetLicenseList.add(PermissionSetLicenseAssign);
}
//insert PermissionSetLicenseList;
try
{
//insert usercreationList;
Database.SaveResult[] results = Database.insert(PermissionSetLicenseList, false);
for(Database.SaveResult result : results)
{
if(result.isSuccess())
{
System.debug('Successfully inserted User : ' + result.getId());
}
else
{
for(Database.Error error : result.getErrors())
{
System.debug('Error Status : ' + error.getStatusCode() + ' : ' + 'Error Fields : ' + error.getFields());
}
}
}
}
catch(DmlException e)
{
System.debug('The following exception has occurred: ' + e.getMessage());
}
//========================================================Permission set Assignment===================================================================
string IdentityConnect = 'IdentityConnect';
string MobileDeviceFormAccess = 'Mobile_Device_Form_Access';
string ServiceDeskClient = 'ServiceDesk_Client';
string ServiceDeskClientMobile = 'ServiceDesk_Client_Mobile';
list<PermissionSet> permissions = [SELECT Id, Name FROM PermissionSet WHERE Name =: IdentityConnect OR Name =: MobileDeviceFormAccess OR Name =: ServiceDeskClient OR Name =: ServiceDeskClientMobile];
List<PermissionSetAssignment> permissionSetAssignmentsList = new List<PermissionSetAssignment>();
system.debug('permissions : '+ permissions);
for(User user: usercreationList)
{
for(PermissionSet permission: permissions)
{
system.debug('permissions : '+ permissions);
PermissionSetAssignment permissionSetAssignment = new PermissionSetAssignment();
if(permission.name == IdentityConnect)
{
permissionSetAssignment.PermissionSetId = permission.Id;
system.debug('IdentityConnect : '+ permission.Id);
}
else if(permission.name == MobileDeviceFormAccess)
{
permissionSetAssignment.PermissionSetId = permission.Id;
system.debug('MobileDeviceFormAccess : '+ MobileDeviceFormAccess);
}
else if(permission.name == ServiceDeskClient)
{
permissionSetAssignment.PermissionSetId = permission.Id;
system.debug('ServiceDeskClient : '+ permission.Id);
}
else if(permission.name == ServiceDeskClientMobile)
{
permissionSetAssignment.PermissionSetId = permission.Id;
system.debug('ServiceDeskClientMobile : '+ permission.Id);
}
permissionSetAssignment.AssigneeId = user.ID;
permissionSetAssignmentsList.add(permissionSetAssignment);
}
}
//insert permissionSetAssignmentsList;
try
{
Database.SaveResult[] results = Database.insert(permissionSetAssignmentsList, false);
for(Database.SaveResult result : results)
{
if(result.isSuccess())
{
System.debug('Successfully inserted User : ' + result.getId());
}
else
{
for(Database.Error error : result.getErrors())
{
System.debug('Error Status : ' + error.getStatusCode() + ' : ' + 'Error Fields : ' + error.getFields());
}
}
}
}
catch(DmlException e)
{
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
//============================================================================================================================================================
public static void DisplayInfo()
{
System.debug('firstName : '+ firstName);
System.debug('lastName : '+ lastName);
System.debug('WorkerType : '+ WorkerType);
System.debug('EmployeeType : '+ EmployeeType);
System.debug('Position : '+ Position);
System.debug('Manager : '+ Manager);
System.debug('ManagerID : '+ userManagerId);
System.debug('EmployeeID : '+ EmployeeID);
System.debug('Company : '+ Company);
System.debug('Division : '+ Division);
System.debug('Location : '+ Location);
System.debug('Department : '+ Department);
System.debug('Email : '+ Email);
System.debug('Alias : '+ Alias);
System.debug('UserName : '+ UserName);
}
}
Please try the below code and let me know if this help-
Please let me know if this helps!
Thanks
Shivdeep Kumar