• Daniel Madhure 7
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hello Team,

I have user (lookup field )on opportunty i want to make that field manadatory through trigger..

i want show message user is manadatory field please select user.

trigger opportunityTeamMemberTrigger  on OpportunityTeamMember (Before Insert, Before Update) {
     for (OpportunityTeamMember oppTeam: Trigger.new)
       { 
           
           if(oppTeam.User == Null){
           
           
           oppTeam.addError('please select the user');
                     
           }
                  
                } 
      
}


the trigger is active and it not working can any one tell what went wrong 
Hello Guys,

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);
        }

    }

}
Hello Team,

I have user (lookup field )on opportunty i want to make that field manadatory through trigger..

i want show message user is manadatory field please select user.

trigger opportunityTeamMemberTrigger  on OpportunityTeamMember (Before Insert, Before Update) {
     for (OpportunityTeamMember oppTeam: Trigger.new)
       { 
           
           if(oppTeam.User == Null){
           
           
           oppTeam.addError('please select the user');
                     
           }
                  
                } 
      
}


the trigger is active and it not working can any one tell what went wrong