+ Start a Discussion
Ravi K 47Ravi K 47 

Issues while deleting task— throwing NullPointerException: Attempt to de-reference a null object error

My requirement is Do not permit any users in any profile except 'System Administrator','Standard User' to delete tasks where Left 3 characters of the WhatId = ‘a0P’(DeleteTask__c) and Type(type__c) = ‘Test’
 
trigger TaskBeforeDelete on Task (before delete) {

  Map<Id,Profile> profileMap=new Map<Id,Profile>([SELECT Id,Name FROM Profile WHERE Name Not IN ('System Administrator','Standard User')]);
    profile p=profileMap.get(UserInfo.getprofileID());


    system.debug('Profile ==>'+p);

    for (Task task : Trigger.old)  {    

    if(task.type__c.equalsIgnoreCase('Test') && (p==null)&& task.WhatId != null && task.WhatId.getSObjectType() == == Deletetask__c.sObjectType)           

        {
            task.addError('You are not permitted to delete this task');

        }

    }

 }

Issue is While deleting Task from other objects(like account,contact) throwing NullPointerException: Attempt to de-reference a null object error
Below is the complete Error message
 
Validation Errors While Saving Record(s)There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger TaskBeforeDelete1 caused an unexpected exception, contact your administrator: TaskBeforeDelete1: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.TaskBeforeDelete1: line 11, column 1".
Can any one please help to resolve the issue

 
Best Answer chosen by Ravi K 47
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
trigger TaskBeforeDelete on Task (before delete) 
{

	Map<Id,Profile> profileMap=new Map<Id,Profile>([SELECT Id,Name FROM Profile WHERE Name Not IN ('System Administrator','Standard User')]) ;

	String DeleteTaskkeyPrefix = Deletetask__c.sObjectType.getDescribe().getKeyPrefix();
	
    for (Task task : Trigger.old)  
	{    
		String strWhatId = task.WhatId;
	
		if( ( task.type__c != null && task.type__c.equalsIgnoreCase('Test') ) && ( profileMap.get(UserInfo.getprofileID()) == null ) && 
				task.WhatId != null && strWhatId.startsWith(DeleteTaskkeyPrefix)  ) 
        {
            task.addError('You are not permitted to delete this task');
        }

    }

}

Let us know if this will help you
 

All Answers

rajat Maheshwari 6rajat Maheshwari 6

Ravi,

Please follow this code -: 

trigger DeleteTask on Task (before delete) 
{
     
    

      
    for(Task tsk : trigger.old)
    {
        
       

   string s = tsk.WhatId;

    

        if((userinfo.getProfileid() =='00e************' || userinfo.getProfileid() == '00e***************') && s.startsWith('a0P'))
              tsk.addError('You are not permitted to delete this task');
        
    
   
}   
}


Here you will provide the id of system admin and standard user, because it's a best practice.

 

Please mark as best answer if it solve your issue.

 

Thanks

rajat Maheshwari 6rajat Maheshwari 6

Include type value also, in if condition, which I missed

Thanks

Amit Chaudhary 8Amit Chaudhary 8
Please try below code
trigger TaskBeforeDelete on Task (before delete) 
{

	Map<Id,Profile> profileMap=new Map<Id,Profile>([SELECT Id,Name FROM Profile WHERE Name Not IN ('System Administrator','Standard User')]) ;

	String DeleteTaskkeyPrefix = Deletetask__c.sObjectType.getDescribe().getKeyPrefix();
	
    for (Task task : Trigger.old)  
	{    
		String strWhatId = task.WhatId;
	
		if( ( task.type__c != null && task.type__c.equalsIgnoreCase('Test') ) && ( profileMap.get(UserInfo.getprofileID()) == null ) && 
				task.WhatId != null && strWhatId.startsWith(DeleteTaskkeyPrefix)  ) 
        {
            task.addError('You are not permitted to delete this task');
        }

    }

}

Let us know if this will help you
 
This was selected as the best answer
Ravi K 47Ravi K 47
Thanks for the updates..using try ..catch statements inside trigger logic resolves the issue.