-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
2Questions
-
1Replies
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);
}
}
- salesforce explorer
- February 23, 2023
- Like
- 0
How to resolve INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
How to resolve
Error: Line: 68, Column: 1
System.DmlException: Process failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
Code:
global class BMCRF_CustomApprovalProcess implements Schedulable
{
global void execute(SchedulableContext SC)
{
//----Variable declaration--------
string description = 'Do you want to send this to another staff member to complete this form?: No';
list<BMCServiceDesk__Incident__c> incRecord = new list<BMCServiceDesk__Incident__c>();
list<ProcessInstance> workItemList = new list<ProcessInstance>();
list<ProcessInstanceWorkitem> AppoverName = new list<ProcessInstanceWorkitem>();
map<id,id> finalList = new map<id,id>();
map<id,string> userID = new map<id,string>();
map<id,string> records = new map<id,string>();
map<Id,string> approvalRecords = new map<id,string>();
map<id,string> approverList = new map<id,string>();
map<id,BMCServiceDesk__Incident__c> approvedRecordList = new map<id,BMCServiceDesk__Incident__c>();
//--------------------------------
//-----Fetching Incident records----------------------
incRecord = [Select id,name,BMCServiceDesk__Service_Request_Title__c,BMCServiceDesk__Client_Name__c, BMCServiceDesk__incidentDescription__c from BMCServiceDesk__Incident__c where BMCServiceDesk__Service_Request_Title__c = 'New User Request Form For Workday' OR BMCServiceDesk__Service_Request_Title__c = 'New User Request Form'];
for(BMCServiceDesk__Incident__c inc:incRecord)
{
if(inc.BMCServiceDesk__incidentDescription__c.contains(description))
{
records.put(inc.Id,inc.BMCServiceDesk__Client_Name__c);
}
}
//----Fetching records in pending approval state---------
workItemList = [SELECT Id,targetObjectId FROM ProcessInstance WHERE Status='Pending' AND ProcessInstance.targetObjectId IN: records.keySet()];
AppoverName = [SELECT ProcessInstance.targetObjectId,ProcessInstanceId,OriginalActor.Name,OriginalActorId FROM ProcessInstanceWorkitem WHERE ProcessInstance.targetObjectId IN: records.keySet()];
for(ProcessInstanceWorkitem record: AppoverName)
{
approverList.put(record.ProcessInstance.targetObjectId,record.OriginalActor.Name);
userID.put(record.ProcessInstanceId,record.OriginalActorId);
}
//--------Getting records to be Approved----------
for(ProcessInstance pi: workItemList)
{
id recID = pi.targetObjectId;
if(approverList.get(recID).equals(records.get(recID)))
{
finalList.put(pi.id,recID);
}
}
system.debug('KeySet : '+finalList.keySet());
system.debug(' Values : '+finalList.values() );
for(id pi:finalList.keySet())
{
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setComments('Approving request using CustomApprovalProcess');
req.setAction('Approve');
req.setNextApproverIds(new Id[]{userID.get(pi)});
req.setWorkitemId(pi);
Approval.ProcessResult result = Approval.process(req);
system.debug('Incident Records : '+pi);
}
}
}
Error: Line: 68, Column: 1
System.DmlException: Process failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
Code:
global class BMCRF_CustomApprovalProcess implements Schedulable
{
global void execute(SchedulableContext SC)
{
//----Variable declaration--------
string description = 'Do you want to send this to another staff member to complete this form?: No';
list<BMCServiceDesk__Incident__c> incRecord = new list<BMCServiceDesk__Incident__c>();
list<ProcessInstance> workItemList = new list<ProcessInstance>();
list<ProcessInstanceWorkitem> AppoverName = new list<ProcessInstanceWorkitem>();
map<id,id> finalList = new map<id,id>();
map<id,string> userID = new map<id,string>();
map<id,string> records = new map<id,string>();
map<Id,string> approvalRecords = new map<id,string>();
map<id,string> approverList = new map<id,string>();
map<id,BMCServiceDesk__Incident__c> approvedRecordList = new map<id,BMCServiceDesk__Incident__c>();
//--------------------------------
//-----Fetching Incident records----------------------
incRecord = [Select id,name,BMCServiceDesk__Service_Request_Title__c,BMCServiceDesk__Client_Name__c, BMCServiceDesk__incidentDescription__c from BMCServiceDesk__Incident__c where BMCServiceDesk__Service_Request_Title__c = 'New User Request Form For Workday' OR BMCServiceDesk__Service_Request_Title__c = 'New User Request Form'];
for(BMCServiceDesk__Incident__c inc:incRecord)
{
if(inc.BMCServiceDesk__incidentDescription__c.contains(description))
{
records.put(inc.Id,inc.BMCServiceDesk__Client_Name__c);
}
}
//----Fetching records in pending approval state---------
workItemList = [SELECT Id,targetObjectId FROM ProcessInstance WHERE Status='Pending' AND ProcessInstance.targetObjectId IN: records.keySet()];
AppoverName = [SELECT ProcessInstance.targetObjectId,ProcessInstanceId,OriginalActor.Name,OriginalActorId FROM ProcessInstanceWorkitem WHERE ProcessInstance.targetObjectId IN: records.keySet()];
for(ProcessInstanceWorkitem record: AppoverName)
{
approverList.put(record.ProcessInstance.targetObjectId,record.OriginalActor.Name);
userID.put(record.ProcessInstanceId,record.OriginalActorId);
}
//--------Getting records to be Approved----------
for(ProcessInstance pi: workItemList)
{
id recID = pi.targetObjectId;
if(approverList.get(recID).equals(records.get(recID)))
{
finalList.put(pi.id,recID);
}
}
system.debug('KeySet : '+finalList.keySet());
system.debug(' Values : '+finalList.values() );
for(id pi:finalList.keySet())
{
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setComments('Approving request using CustomApprovalProcess');
req.setAction('Approve');
req.setNextApproverIds(new Id[]{userID.get(pi)});
req.setWorkitemId(pi);
Approval.ProcessResult result = Approval.process(req);
system.debug('Incident Records : '+pi);
}
}
}
- salesforce explorer
- November 10, 2022
- Like
- 0
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);
}
}
- salesforce explorer
- February 23, 2023
- Like
- 0