Skip to main content
tracy smith 님이 #Data Management에 질문했습니다
Hi

 

Hey Community,

 

I'm attempting to update the Status of a Case to 'Canceled' when an Event record associated with it ( via the "Related To" lookup) is deleted.

 

I've repurposed some code (See below) I found but keep getting this error:

 

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 updateCaseStatusOnEventDelete caused an unexpected exception, contact your administrator: updateCaseStatusOnEventDelete: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: External entry point".

trigger updateCaseStatusOnEventDelete on Event (before delete) {

Set<Id> caseIds = new Set<Id>();

for(Event e: [SELECT WhatId FROM Event WHERE ID IN:trigger.new]){

String wId = e.WhatId;

if(wId!=null && wId.startsWith('500') && !caseIds.contains(e.WhatId)){

caseIds.add(e.WhatId);

}

}

for(Case c:[SELECT Status FROM Case WHERE ID IN:caseIds]){

c.Status = 'Canceled';

update c;

}

}

 

 
답변 2개
  1. 2017년 8월 14일 오전 4:54

    Hi

    trigger updateCaseStatusOnEventDelete on Event ( before delete )

    {

    Set<Id> caseIds = new Set<Id>();

    for ( Event evnt : [SELECT WhatId FROM Event

    WHERE Id IN :Trigger.old AND What.Type = 'Case'] ) caseIds.add( evnt.WhatId );

    List<Case> casestoUpdate = [SELECT Status FROM Case WHERE Id IN :caseIds];

    for ( Case case : casestoUpdate ) case.Status = 'Cancelled';

    update casestoUpdate; // DML on empty collection does not error or count against limits

    }

     

    If the above suggestion worked, let us know by marking the answer as "Best Answer" right under the comment which will help the rest of the community should they have a similar issue in the future. 

     

    Thanks & Regards 

     

    David Hales(1044)

     

    Kloudrac Software Pvt. Ltd.

     

     
0/9000