You need to sign in to do that
Don't have an account?
Daniel Madhure 7
Batch class failing
Hello Guys,
when i am running my batch i am getting below error.
can anyone please help what when wrong in my code.
Class Name: GenerateUserRoleHierarchy_Batch
Purpose: 1. This class is used to insert all unique role in userRole form User_Role_Hierarchy__c custome object.
global class GenerateUserRoleHierarchy_Batch implements Database.Batchable<sObject>
{
global Map<String, String> mapRoleHierarchySrID = new Map<String, String>();
global Map<String, String> mapRoleHierarchyName = new Map<String, String>();
global Map<String,Id> userRoleMap = new Map<String,Id>();
global String SoqlQuery;
/******************************************************************************
Method Name - GenerateUserRoleHierarchy_Batch
Access Modifier - global
Purpose - This is constructor method queries all the User Role_Hierarchy data set required for populating parent child relationship in userRole object.
Output - List
*******************************************************************************/
global GenerateUserRoleHierarchy_Batch()
{
for(User_Role_Hierarchy__c roleHierarchyObj : [Select SR_ID__c, Manager_SR_ID__c, SR_Name__c From User_Role_Hierarchy__c])
{
mapRoleHierarchySrID.put(roleHierarchyObj.SR_ID__c, roleHierarchyObj.Manager_SR_ID__c);
mapRoleHierarchyName.put(roleHierarchyObj.SR_ID__c, roleHierarchyObj.SR_Name__c);
}
List<UserRole> userRoleLst = [Select Name,Id From UserRole order By Name];
for(UserRole usrObj : userRoleLst)
{
userRoleMap.put(usrObj.Name, usrObj.Id);
}
SoqlQuery = 'Select Id, Name, parentRoleId From UserRole order By Name ';
if (Test.isRunningTest()) {
SoqlQuery = 'Select Id, Name, parentRoleId From UserRole order By Name Limit 200';
}
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(SoqlQuery);
}
global void execute(Database.BatchableContext BC, List<UserRole> scope)
{
//Map<String,Id> userRoleMap = new Map<String,Id>();
List<UserRole> userRoleLstToUpdate = new List<UserRole>();
for(UserRole userRoleObj : scope)
{
if(userRoleObj.Name != Constants.exceptionRoleName)
{
List<String> roleSplit = new List<String>();
String managerSrId = '';
String managerSrName = '';
String parentRole = '';
//System.debug('###userRoleObj===>'+userRoleObj);
if(userRoleObj.Name.contains('_Child'))
{
parentRole = userRoleObj.Name.replace('_Child','');
}
else
{
if(userRoleObj.Name.contains(Constants.strUnderScore))
{
roleSplit = userRoleObj.Name.split(Constants.strUnderScore);
if(roleSplit.size() > 1)
{
managerSrId = mapRoleHierarchySrID.get(roleSplit[1]);
managerSrName = mapRoleHierarchyName.get(managerSrId);
}
if(managerSrId != null && !String.isBlank(managerSrId))
parentRole = 'UPS_'+managerSrId+Constants.strUnderScore+managerSrName;
}
}
if(!String.isBlank(parentRole))
{
if(userRoleMap.get(parentRole) == null && managerSrId != Constants.topMostRoleName )
userRoleObj.parentRoleId = userRoleMap.get(Constants.exceptionRoleName);
else
userRoleObj.parentRoleId = userRoleMap.get(parentRole);
}
userRoleLstToUpdate.add(userRoleObj);
}
}
try
{
if(userRoleLstToUpdate.size() > 0)
{
update userRoleLstToUpdate;
}
}
catch(Exception e)
{
Batch_Util exceptionClass = new Batch_Util();
insert exceptionClass.createException('GenerateUserRoleHierarchy_Batch','Batch Execute',e);
system.debug('Error Message'+e.getMessage());
}
}
global void finish(Database.BatchableContext BC)
{
AssignUserRole_Batch batchObj = new AssignUserRole_Batch();
if(!Test.isRunningTest())
{
Database.executeBatch(batchObj);
}
}
}
when i am running my batch i am getting below error.
First error: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Exception__c, original object: UserRole: []
can anyone please help what when wrong in my code.
Class Name: GenerateUserRoleHierarchy_Batch
Purpose: 1. This class is used to insert all unique role in userRole form User_Role_Hierarchy__c custome object.
global class GenerateUserRoleHierarchy_Batch implements Database.Batchable<sObject>
{
global Map<String, String> mapRoleHierarchySrID = new Map<String, String>();
global Map<String, String> mapRoleHierarchyName = new Map<String, String>();
global Map<String,Id> userRoleMap = new Map<String,Id>();
global String SoqlQuery;
/******************************************************************************
Method Name - GenerateUserRoleHierarchy_Batch
Access Modifier - global
Purpose - This is constructor method queries all the User Role_Hierarchy data set required for populating parent child relationship in userRole object.
Output - List
*******************************************************************************/
global GenerateUserRoleHierarchy_Batch()
{
for(User_Role_Hierarchy__c roleHierarchyObj : [Select SR_ID__c, Manager_SR_ID__c, SR_Name__c From User_Role_Hierarchy__c])
{
mapRoleHierarchySrID.put(roleHierarchyObj.SR_ID__c, roleHierarchyObj.Manager_SR_ID__c);
mapRoleHierarchyName.put(roleHierarchyObj.SR_ID__c, roleHierarchyObj.SR_Name__c);
}
List<UserRole> userRoleLst = [Select Name,Id From UserRole order By Name];
for(UserRole usrObj : userRoleLst)
{
userRoleMap.put(usrObj.Name, usrObj.Id);
}
SoqlQuery = 'Select Id, Name, parentRoleId From UserRole order By Name ';
if (Test.isRunningTest()) {
SoqlQuery = 'Select Id, Name, parentRoleId From UserRole order By Name Limit 200';
}
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(SoqlQuery);
}
global void execute(Database.BatchableContext BC, List<UserRole> scope)
{
//Map<String,Id> userRoleMap = new Map<String,Id>();
List<UserRole> userRoleLstToUpdate = new List<UserRole>();
for(UserRole userRoleObj : scope)
{
if(userRoleObj.Name != Constants.exceptionRoleName)
{
List<String> roleSplit = new List<String>();
String managerSrId = '';
String managerSrName = '';
String parentRole = '';
//System.debug('###userRoleObj===>'+userRoleObj);
if(userRoleObj.Name.contains('_Child'))
{
parentRole = userRoleObj.Name.replace('_Child','');
}
else
{
if(userRoleObj.Name.contains(Constants.strUnderScore))
{
roleSplit = userRoleObj.Name.split(Constants.strUnderScore);
if(roleSplit.size() > 1)
{
managerSrId = mapRoleHierarchySrID.get(roleSplit[1]);
managerSrName = mapRoleHierarchyName.get(managerSrId);
}
if(managerSrId != null && !String.isBlank(managerSrId))
parentRole = 'UPS_'+managerSrId+Constants.strUnderScore+managerSrName;
}
}
if(!String.isBlank(parentRole))
{
if(userRoleMap.get(parentRole) == null && managerSrId != Constants.topMostRoleName )
userRoleObj.parentRoleId = userRoleMap.get(Constants.exceptionRoleName);
else
userRoleObj.parentRoleId = userRoleMap.get(parentRole);
}
userRoleLstToUpdate.add(userRoleObj);
}
}
try
{
if(userRoleLstToUpdate.size() > 0)
{
update userRoleLstToUpdate;
}
}
catch(Exception e)
{
Batch_Util exceptionClass = new Batch_Util();
insert exceptionClass.createException('GenerateUserRoleHierarchy_Batch','Batch Execute',e);
system.debug('Error Message'+e.getMessage());
}
}
global void finish(Database.BatchableContext BC)
{
AssignUserRole_Batch batchObj = new AssignUserRole_Batch();
if(!Test.isRunningTest())
{
Database.executeBatch(batchObj);
}
}
}
it is not possible to perform DML on setup objects (since you are using UserRole object) and non-setup object in the same context, you may have to use @future to update and you may opt for Queueing jobs , use the Queuable interface
Please refer https://developer.salesforce.com/forums/?id=906F00000008wAZIAY
please mark as solved, if this helps you
Thanks !