+ Start a Discussion
StaciStaci 

Contains in Trigger is erroring

trigger CWUserValidation on Case (before update)
{
       
        for (Case objCase : Trigger.new)
        {
           //If the current user is a Tier 2 user and they assign the case to a Tier 3 user, throw error.
           if(objCase.CW_Current_User_s_Profile__c == 'CW MineStar Support - Tier 2' && objCase.CW_Owner_Role__c == '00E300000019awI')            
           {
                trigger.new[0].addError('You cannot assign a case directly to a Tier 3 user. Please choose a Tier 3 queue.');
           }
              //If the current user is a Tier 3 user and they assign the case to a Tier 2 user, Dealer user or Dealer Queue, throw error.
              if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && (objCase.CW_Owner_Role__c == '00E300000019awS'||objCase.CW_Owner_Role__c == '00E300000019awc'||objCase.CW_Owner_Role__c == '00E300000019awX'||objCase.CW_Owner_Role__c == '00E300000019awN'))              
              {
                trigger.new[0].addError('You cannot assign a case directly to a Tier 2 user, Please assign to the CW Support Advocate (L2) Queue.');
              }else{
                    //If the current user is a Tier 3 user and they assign the case to a Dealer queue, throw error
                    if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && objCase.CW_Queue_Case_Owner__c.contains('Dealer'))            
                    {
                        trigger.new[0].addError('You cannot assign a case to a Dealer queue, Please assign to the CW Support Advocate (L2) Queue.');
                    }else{
                         //If the current user is a Tier 3 user and they assign the case to a Dealer user 
                         if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && objCase.CW_Owner_Profile__c == '00e30000001aw4D')
                        {
                           trigger.new[0].addError('You cannot assign a case to a Dealer user, Please assign to the CW Support Advocate (L2) Queue.');
                        }
                         }
                   }    
         }       
}
My trigger if it hits this line, it errors with the error below:
if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && objCase.CW_Queue_Case_Owner__c.contains('Dealer'))


CWUserValidation: execution of BeforeUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.CWUserValidation: line 17, column 1

Not sure what I'm doing wrong here, can anyone help?
Best Answer chosen by Staci
mjohnson-TICmjohnson-TIC
Your code looks like this? 
 
trigger CWUserValidation on Case (before update){
	for (Case objCase : Trigger.new){
		//If the current user is a Tier 2 user and they assign the case to a Tier 3 user, throw error.
		if(objCase.CW_Current_User_s_Profile__c == 'CW MineStar Support - Tier 2' && objCase.CW_Owner_Role__c == '00E300000019awI')            
		{
			trigger.new[0].addError('You cannot assign a case directly to a Tier 3 user. Please choose a Tier 3 queue.');
		}
		//If the current user is a Tier 3 user and they assign the case to a Tier 2 user, Dealer user or Dealer Queue, throw error.
		if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && (objCase.CW_Owner_Role__c == '00E300000019awS'||objCase.CW_Owner_Role__c == '00E300000019awc'||objCase.CW_Owner_Role__c == '00E300000019awX'||objCase.CW_Owner_Role__c == '00E300000019awN'))              
		{
			trigger.new[0].addError('You cannot assign a case directly to a Tier 2 user, Please assign to the CW Support Advocate (L2) Queue.');
		}else{
			//If the current user is a Tier 3 user and they assign the case to a Dealer queue, throw error
			if(objCase.CW_Queue_Case_Owner__c != null)
			{
				if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && objCase.CW_Queue_Case_Owner__c.contains('Dealer'))            
				{
					trigger.new[0].addError('You cannot assign a case to a Dealer queue, Please assign to the CW Support Advocate (L2) Queue.');
				}
			}else{
			//If the current user is a Tier 3 user and they assign the case to a Dealer user 
				if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && objCase.CW_Owner_Profile__c == '00e30000001aw4D')
				{
					trigger.new[0].addError('You cannot assign a case to a Dealer user, Please assign to the CW Support Advocate (L2) Queue.');
				}
			}
		}    
	}       
}

 

All Answers

Balaji BondarBalaji Bondar
Hi Staci,

Try below code.I  have updated code at Line#17.
trigger CWUserValidation on Case (before update)
{
       
        for (Case objCase : Trigger.new)
        {
           //If the current user is a Tier 2 user and they assign the case to a Tier 3 user, throw error.
           if(objCase.CW_Current_User_s_Profile__c == 'CW MineStar Support - Tier 2' && objCase.CW_Owner_Role__c == '00E300000019awI')            
           {
                trigger.new[0].addError('You cannot assign a case directly to a Tier 3 user. Please choose a Tier 3 queue.');
           }
              //If the current user is a Tier 3 user and they assign the case to a Tier 2 user, Dealer user or Dealer Queue, throw error.
              if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && (objCase.CW_Owner_Role__c == '00E300000019awS'||objCase.CW_Owner_Role__c == '00E300000019awc'||objCase.CW_Owner_Role__c == '00E300000019awX'||objCase.CW_Owner_Role__c == '00E300000019awN'))              
              {
                trigger.new[0].addError('You cannot assign a case directly to a Tier 2 user, Please assign to the CW Support Advocate (L2) Queue.');
              }else{
                    String CWQueueCaseOwnerString = objCase.CW_Queue_Case_Owner__c;
					//If the current user is a Tier 3 user and they assign the case to a Dealer queue, throw error
                    if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && CWQueueCaseOwnerString.contains('Dealer'))            
                    {
                        trigger.new[0].addError('You cannot assign a case to a Dealer queue, Please assign to the CW Support Advocate (L2) Queue.');
                    }else{
                         //If the current user is a Tier 3 user and they assign the case to a Dealer user 
                         if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && objCase.CW_Owner_Profile__c == '00e30000001aw4D')
                        {
                           trigger.new[0].addError('You cannot assign a case to a Dealer user, Please assign to the CW Support Advocate (L2) Queue.');
                        }
                         }
                   }    
         }       
}


Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
StaciStaci
@Balaji Bondar its still giving the same error
mjohnson-TICmjohnson-TIC
The contains() method errors when assessing a null value. Try prefacing the contains statement with IF(Fieldname != null){IF(FieldName.contains('text'){dothis();}}
StaciStaci
@mjohnson-TIC still the same error, still on the contains line
mjohnson-TICmjohnson-TIC
Your code looks like this? 
 
trigger CWUserValidation on Case (before update){
	for (Case objCase : Trigger.new){
		//If the current user is a Tier 2 user and they assign the case to a Tier 3 user, throw error.
		if(objCase.CW_Current_User_s_Profile__c == 'CW MineStar Support - Tier 2' && objCase.CW_Owner_Role__c == '00E300000019awI')            
		{
			trigger.new[0].addError('You cannot assign a case directly to a Tier 3 user. Please choose a Tier 3 queue.');
		}
		//If the current user is a Tier 3 user and they assign the case to a Tier 2 user, Dealer user or Dealer Queue, throw error.
		if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && (objCase.CW_Owner_Role__c == '00E300000019awS'||objCase.CW_Owner_Role__c == '00E300000019awc'||objCase.CW_Owner_Role__c == '00E300000019awX'||objCase.CW_Owner_Role__c == '00E300000019awN'))              
		{
			trigger.new[0].addError('You cannot assign a case directly to a Tier 2 user, Please assign to the CW Support Advocate (L2) Queue.');
		}else{
			//If the current user is a Tier 3 user and they assign the case to a Dealer queue, throw error
			if(objCase.CW_Queue_Case_Owner__c != null)
			{
				if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && objCase.CW_Queue_Case_Owner__c.contains('Dealer'))            
				{
					trigger.new[0].addError('You cannot assign a case to a Dealer queue, Please assign to the CW Support Advocate (L2) Queue.');
				}
			}else{
			//If the current user is a Tier 3 user and they assign the case to a Dealer user 
				if(objCase.CW_Current_User_s_Role__c == '00E300000019awI' && objCase.CW_Owner_Profile__c == '00e30000001aw4D')
				{
					trigger.new[0].addError('You cannot assign a case to a Dealer user, Please assign to the CW Support Advocate (L2) Queue.');
				}
			}
		}    
	}       
}

 
This was selected as the best answer
StaciStaci
ah thank you!  had a } in the wrong place.  Now its working!