TaskBeforeDelete Trigger on Task Object:-

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’

=====Below the trigger=====

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".

**Could you please help us to resolve the issue **

Here is the updated code but still throwing an error .

  • 1
    It doesn't seem like this code should compile. You haven't defined task__c anywhere, for starters.
    – Adrian Larson
    Commented Feb 10, 2017 at 15:43

2 Answers 2


Aside for the code posted not being able to be compiled you need to check that WhatId is not null before attempting to get the sObject Type

task.WhatId != null && task.WhatId.getSObjectType()
  • Hmm, interesting. We both caught a different possible culprit. Ha!
    – Adrian Larson
    Commented Feb 10, 2017 at 15:46
  • @AdrianLarson - Can you put it into a general answer about dereferencing null values, then I will delete this one
    – Eric
    Commented Feb 10, 2017 at 15:47
  • I don't think you need to delete! I can generalize though.
    – Adrian Larson
    Commented Feb 10, 2017 at 15:48
  • I tried with above changes (task.WhatId != null && task.WhatId.getSObjectType() == Deletetask__c.sObjectType and task.Type__c == 'Test' ) but still it is throwing an error when deleting Contact related Task . Here task.Type__c is a PickList Value Commented Feb 10, 2017 at 16:49

In general, if you do not know beforehand if a variable is null, you should check, or avoid using it in a way that will force the code to de-reference the variable.

For example:

String nullVariable;
system.debug(nullVariable == 'Some value');

The above code will not throw a null pointer, because this form of comparison does not de-reference nullVariable. When you have to de-reference in order to do your comparison, check if it's null first. Boolean logic performs short-circuit evaluation, so as soon as it can exit, it will do so:

String nullVariable;
if (nullVariable != null && nullVariable.equals('Some Value'));

With that in mind, you need to make two changes.

  1. You should replace:




    or better yet:

    task.Type__c == 'Test'

    Note that double equals comparison (==) is case-insensitive.

  2. You should replace:

    task.WhatId.getSObjectType() == Deletetask__c.sObjectType


    task.WhatId != null && task.WhatId.getSObjectType() == Deletetask__c.sObjectType
  • I tried with above changes (task.WhatId != null && task.WhatId.getSObjectType() == Deletetask__c.sObjectType and task.Type__c == 'Test' ) but still it is throwing an error . Commented Feb 10, 2017 at 16:45
  • Please update your question with your new code and indicate specifically which line is throwing the error.
    – Adrian Larson
    Commented Feb 10, 2017 at 17:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .