3

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was:

Apex trigger updatequotestatus caused an unexpected exception, contact your administrator: updatequotestatus: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updatequotestatus line 5, column 1.

Error Occured at Line:

if((t.what.getsobjecttype() == quote__c.sobjecttype) && (t.subject.substring(0,5) == 'Email')

trigger updatequotestatus on task (after insert, after update){
    Set<Id> quoteIds = new Set<Id>();
    List<quote__c> quotes = new List<quote__c>();

    for(task t:trigger.new){
        if(t.whatId!=null){
            if((t.what.getsobjecttype() == quote__c.sobjecttype) && (t.subject.substring(0,5) == 'Email')){
                quoteIds.add(t.whatId);
            }
        }
    }

    for(quote__c q:[select StageName__c from quote__c where Id IN : quoteIds]){
        quote__c qt = new quote__c();
        qt.Id = q.Id;
        qt.StageName__c = 'Proposal Sent';
        quotes.add(qt);
    }

    if(quotes.size()>0){
        update quotes;
    }
}
  • 4
    NullPointerExceptions are pretty straightforward. You are referencing a variable that is not initialised or has been set to null. There are not many variables in if((t.what.getsobjecttype()==quote__c.sobjecttype) && (t.subject.substring(0,5)=='Email') so I suggest you do some debugging (e.g. printout the value of t.what) to see what is null in that line – BarCotter Jun 9 '14 at 12:59
7

T.what is null, because you haven't queried for it, and the relationships passed into a trigger are always null. Instead, you can call getSobjectType on WhatId directly:

if(t.WhatId != NULL && t.WhatId.getSobjectType() == Quote__c.SobjectType ...

Bonus: Also, since you aren't checking for any of the data from the query, there's strictly no reason to waste a query here:

for(Id quoteId: quoteIds) {
    Quote__c q = new Quote__c(Id=quoteId);
    ...
  • where i have to change my trigger any suggestions plz.. – Sathya Jun 9 '14 at 13:33
  • @Sathya As the answer says, change t.what.get... to t.whatid.get... – sfdcfox Jun 9 '14 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.