Skip to main content

trigger CaseDeleted on Case (before delete) 

{

   //Whenever a case is getting deleted check if it has the contact is related account

   // If it is related to the account and contact then throw an error saying "can not be deleted"

   for (Case cs: Trigger. old);

   {

       if(case.Accountid !=Null && case.Contactid !=Null) // if case is releted Account and Contact

       

       {

       case.adderror('we can not delete case Releted Account And Contact');

       }

   }

}

Explain The Execution

2 Antworten
  1. 18. Nov. 2024, 14:58

    Hi,

    The trigger prevents the deletion of a Case record if it is associated with both an Account and a Contact. Before the record is deleted, the trigger checks whether the Case has non-null values for both the AccountId and ContactId. If both are present, the trigger calls addError() on the Case record, which stops the deletion and displays an error message to the user, indicating that the Case cannot be deleted because it is linked to both an Account and a Contact.

    The issue in your code is semicolon after for loop,the code should be :

    trigger CaseDeleted on Case (before delete) {

     

    // Loop through each case in the trigger's old context (cases that are being deleted)

    for (Case cs : Trigger.old) {

     

    // Check if both AccountId and ContactId are populated

    if (cs.AccountId != null && cs.ContactId != null) {

    // If the case is related to both an Account and a Contact, throw an error to prevent deletion

    cs.addError('Cannot delete case related to both an Account and a Contact.');

    }

     

    }

     

    }

    Mark this helpful if it solves your query!

0/9000