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
CageMMACageMMA 

After delete getting error???

I am getting the following error:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger Last_CBM_F2F_Meeting_trg caused an unexpected exception, contact your administrator: Last_CBM_F2F_Meeting_trg: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Last_CBM_F2F_Meeting_trg: line 5, column 1".

 

On this line:

    Set<Id> relatedIds = new Set<Id>();
    for (Task indivTask : Trigger.new) {
        if (indivTask.WhatId != null) relatedIds.add(indivTask.WhatId);
        
    }

 Any help would be appreciated... Basically if a user deletes the task the Last date field should become null on account.

Thanks

Suresh RaghuramSuresh Raghuram

hi

 

If you are getting a null pointer exception there will be two reasons.

 

one is there is no values at all  to delete or edit,

 

the path or the approach to meet that value is wrong.

check thorughly with this

 

indivTask.WhatId

CageMMACageMMA

I do not think it should matter no...Here is all of my code: the update to account works but when I tried to delete the task and get the most latest task that completes the requirement it errors out meaning after i placed in After Delete in there..

trigger Last_CBM_F2F_Meeting_trg on Task (after insert, after update, after delete) {

    // Get related identifiers on the task set
    Set<Id> relatedIds = new Set<Id>();
    for (Task indivTask : Trigger.new) {
        if (indivTask.WhatId != null) relatedIds.add(indivTask.WhatId);
        
    }
   
    // Get back from account - if they point to other objects it won't matter here
    Map<Id,Account> accountMap = new Map<Id,Account>
    ([Select Id, Last_CBM_F2F_Meeting__c, Last_CBM_Meeting_Request__c From Account Where Id in :relatedIds]);
   
    // Accounts for updates.  Use a map so if there are more than one task in the triggered batch for the same account
    // then repeat related refs get overwritten so we only keep the latest one
    Map<Id,Account> accountsForUpdate = new Map<Id,Account>();
   
    // Loop through triggered set
    for (Integer i=0;i<Trigger.new.size();i++) {
       
        // Process only if insert or the picklist has changed
        if ((Trigger.isInsert) || (Trigger.old[i].ActivityDate != Trigger.new[i].ActivityDate && Trigger.old[i].RecordTypeId=='01230000000YFxgAAG' && Trigger.new[i].Activity_Type__c=='Meeting - Face to Face' || Trigger.old[i].Activity_Type__c != Trigger.new[i].Activity_Type__c || Trigger.old[i].LastModifiedDate !=Trigger.new[i].LastModifiedDate)) {
           
            // only continue if we are related to an account
            if ((Trigger.new[i].WhatId != null && Trigger.new[i].RecordTypeId=='01230000000YFxgAAG' && Trigger.new[i].Activity_Type__c=='Meeting - Face to Face') && (accountMap.get(Trigger.new[i].WhatId) != null)) {
           
                // Put the changed value in the map for update.
                accountsForUpdate.put(Trigger.new[i].WhatId,new Account(Id = Trigger.new[i].WhatId, Last_CBM_F2F_Meeting__c = Trigger.new[i].ActivityDate));
            }
        }
/********************************Last_CBM_Meeting_Request__c********************************************************************************************************/
        // Process only if insert or the picklist has changed
        if ((Trigger.isInsert) || (Trigger.old[i].ActivityDate != Trigger.new[i].ActivityDate && Trigger.old[i].RecordTypeId=='01230000000YFxgAAG' && Trigger.new[i].Activity_Type__c=='Meeting Request' || Trigger.old[i].Activity_Type__c != Trigger.new[i].Activity_Type__c || Trigger.old[i].LastModifiedDate !=Trigger.new[i].LastModifiedDate)) {
           
            // only continue if we are related to an account
            if ((Trigger.new[i].WhatId != null && Trigger.new[i].RecordTypeId=='01230000000YFxgAAG' && Trigger.new[i].Activity_Type__c=='Meeting Request') && (accountMap.get(Trigger.new[i].WhatId) != null)) {
           
                // Put the changed value in the map for update.
                accountsForUpdate.put(Trigger.new[i].WhatId,new Account(Id = Trigger.new[i].WhatId, Last_CBM_Meeting_Request__c = Trigger.new[i].ActivityDate));
            }
        }
        // Process only if insert or the picklist has changed
        if (Trigger.isDelete){
           Task tk =[Select ActivityDate from Task where id=:Trigger.old[i].id AND RecordTypeId='01230000000YFxgAAG' AND Activity_Type__c='Meeting Request' AND WhatId != null Order by ActivityDate DESC Limit 1];

                accountsForUpdate.put(Trigger.old[i].WhatId,new Account(Id = Trigger.old[i].WhatId, Last_CBM_Meeting_Request__c = Trigger.old[i].ActivityDate));
            }
                
/****************************************************************************************************************************************/
        
    } // end loop through triggered set
   
    // Create a list of accounts for update.  Only include them if the latest value
    // has actually changed
    List<Account> updateAccounts = new List<Account>();
    for (Account updateAccount : accountsForUpdate.values()) {
        if (updateAccount.Last_CBM_F2F_Meeting__c != accountMap.get(updateAccount.Id).Last_CBM_F2F_Meeting__c) {
            
        }
        if (updateAccount.Last_CBM_Meeting_Request__c != accountMap.get(updateAccount.Id).Last_CBM_Meeting_Request__c) {
            
        } 
        updateAccounts.add(updateAccount);       
    }
   
    // If there are any changed accounts then issue the update
    if (updateAccounts.size() != 0) update updateAccounts;
} // end trigger

 

Suresh RaghuramSuresh Raghuram

place a system.debug before delete operation takes place and check in the debug logs are you getting the record id which you are trying to delete

CageMMACageMMA

Actually I wrote another trigger that would update the field to null or the latest date of the task when a Open task is deleted. It worked.

Cool

thanks for everything

Z