function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Piyush Sharma 6Piyush Sharma 6 

Null pointer exception error in trigger on Case object

I have written below trigger to update case status when case is assigned from Queue to a user, but it is giving a null pointer exception @line 10.
The requirement is to update case status when case is assigned from a queue to a user.
 
trigger updateCase on Case (before update) {
    List<Case> newCaseList = new List<Case>() ;
    System.debug('@@newCase' + newCaseList);
    for(Case c : Trigger.new) {
        System.debug('@@@' + c);
		//just add few more null checks- 
		If(c.status!=null && c.OwnerID.getsobjecttype()!=null)
		{
			if(c.OwnerID.getsobjecttype() == User.sobjecttype && c.Status == 'New') {
					if(Trigger.oldMap.get(c.Id).Owner.getsobjecttype() != User.sobjecttype) {
							c.Status = 'Working';
							newCaseList.add(c);
						}
				System.debug('@@' + newCaseList);
			}
		}
    }
    if(newCaseList.size() > 0) {
        insert newCaseList;
    }
}

 
Best Answer chosen by Piyush Sharma 6
ManojjenaManojjena
Hi Piyush,

Try with below code it will work as your event is before update no need to do DML on this .
 
trigger updateCase on Case (before update) {
    List<Case> newCaseList = new List<Case>() ;
    System.debug('@@newCase' + newCaseList);
    for(Case c : Trigger.new) {
        System.debug('@@@' + c);
        //just add few more null checks- 
        If(c.status!=null && c.OwnerID.getsobjecttype()!=null)
        {
            if(c.OwnerID.getsobjecttype() == User.sobjecttype && c.Status == 'New') {
                    if(Trigger.oldMap.get(c.Id).OwnerId.getsobjecttype() != User.sobjecttype) {
                            c.Status = 'Working';
                           
                        }
                System.debug('@@' + newCaseList);
            }
        }
    }
    
}

For some concepts you can check below link it will help !!
Let me know if it helps!!
Thanks 
Manoj

All Answers

ManojjenaManojjena
Hi Piyush,

Try with below code it will work as your event is before update no need to do DML on this .
 
trigger updateCase on Case (before update) {
    List<Case> newCaseList = new List<Case>() ;
    System.debug('@@newCase' + newCaseList);
    for(Case c : Trigger.new) {
        System.debug('@@@' + c);
        //just add few more null checks- 
        If(c.status!=null && c.OwnerID.getsobjecttype()!=null)
        {
            if(c.OwnerID.getsobjecttype() == User.sobjecttype && c.Status == 'New') {
                    if(Trigger.oldMap.get(c.Id).OwnerId.getsobjecttype() != User.sobjecttype) {
                            c.Status = 'Working';
                           
                        }
                System.debug('@@' + newCaseList);
            }
        }
    }
    
}

For some concepts you can check below link it will help !!
Let me know if it helps!!
Thanks 
Manoj
This was selected as the best answer
doravmondoravmon
You need to use ownId in line 10:
if(Trigger.oldMap.get(c.Id).OwnerId.getsobjecttype() != User.sobjecttype)
Piyush Sharma 6Piyush Sharma 6
It worked good.
Thanks much for the help