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
Manish Kumar 23Manish Kumar 23 

Deleting custom object record releated tp parent account based on condition

Hi All,

I am new to the trigger 

I wanted to write a trigger which will delete a related record in custom object
when there is checkbox uncheck in parent object

Regards,
Manish Tiwary
Best Answer chosen by Manish Kumar 23
Anoop yadavAnoop yadav
Try this code and change accordingly.

@isTest (SeeAllData = true)
public class ContactDelete_Test {
    public static testMethod void testContactDelete() {
        
        Account acc = new Account();
        acc.Name = 'Test';
        // add All mandatory Field value;
        //checkbox == true
       
        insert acc;
        
       Contact con = new Contact();
       con.AccountId = acc.Id;
       con.LastName = 'Last Name';
       insert con;
       
       //update Account
       //checkbox == false; 
       update acc;
    }
    
}

add try, catch on insert and update.
and test.StartTest and Test.stopTest.
It will work.

All Answers

Ramu_SFDCRamu_SFDC
Hi Manish,

First of all Triggers work when there is a DML operation taking place on the record on which the trigger is written. In the current scenario, you want the record to be deleted when its parent record's checkbox is checked? if Yes, then you can write an after trigger on the parent object and add an if condition fi the checkbox is checked, if yes then get all the child records associated to that particular parent object and delete the records. The trigger goes something like the below example

Trigger deletechildrecs on Parent(After update){
List<childobject> recstodelete=new List<childobject>();
set<id> idtodelete=new set<id>();
For(Parentobject Prec:trigger.new){
if(Prec.checkboxfield==true){
idtodelete.add(prec.id);
}
}

recstodelete=[select id from childobject where parentrecid in :idtodelete];

delete recstodelete;


}


Anoop yadavAnoop yadav
Hi Manish,

Below is a sample code to delete contact records, When you update the Account Record.

Just put your conditon in if () statement, and change parent and child object Name.
This will work.

trigger DeleteContact on Account (after update) {
    
    Map<Id, Account> accMap = new Map<Id, Account>();
    List<Contact> conList = new List<Contact>();
    
    for(Account acc :Trigger.New){
        if(true){
            accMap.put(acc.Id, acc);
        }
    }
    
    conList = [Select Id From Contact Where AccountId IN :accMap.keySet()];
    
    if(conList.size() > 0){
        delete conList;
    }
    
}




Manish Kumar 23Manish Kumar 23
Hi Anoop,

I have used the same code and is working.Now we have to write test class for the code coverage.
Can you help me in doing that??
Anoop yadavAnoop yadav
Try this code and change accordingly.

@isTest (SeeAllData = true)
public class ContactDelete_Test {
    public static testMethod void testContactDelete() {
        
        Account acc = new Account();
        acc.Name = 'Test';
        // add All mandatory Field value;
        //checkbox == true
       
        insert acc;
        
       Contact con = new Contact();
       con.AccountId = acc.Id;
       con.LastName = 'Last Name';
       insert con;
       
       //update Account
       //checkbox == false; 
       update acc;
    }
    
}

add try, catch on insert and update.
and test.StartTest and Test.stopTest.
It will work.
This was selected as the best answer
Manish Kumar 23Manish Kumar 23
Thax Anoop, 
test class is working fine .
Thax for your help and support.

Simalary  I have written trigger  for inserting the record based on conditon as below
When i am creating test class for below it is showing only 50 % coverage.

trigger Topaccount on Account(After insert, after update) {
  List<Top_Account__c> newTop = new List<Top_Account__c>();
  for (Account acc : Trigger.new) {
  if ((trigger.isInsert || acc.Top_Account__c != trigger.oldMap.get(acc.Id).Top_Account__c) && acc.Top_Account__c == True)
  {
    Top_Account__c tp = new Top_Account__c();
    tp.Name = acc.Name;
    tp.releted_acc__c = acc.Id; // Use the trigger record's ID
    newTop .add(tp);
  }
  }
  insert newTop ;
  }
Anoop yadavAnoop yadav
@isTest (SeeAllData = true)
public class ContactDelete_Test1 {
    public static testMethod void testContactDelete1() {
        
        Account acc = new Account();
        acc.Name = 'Test';
        acc.Top_Account__c == True;
        insert acc;       
    }    
}

Try this and Add Mandatory fields for Account.
Anoop yadavAnoop yadav
use
acc.Top_Account__c = True;

istead
acc.Top_Account__c == True;
Manish Kumar 23Manish Kumar 23
Hii Annop.

Thank you Anoop,

But I have one confusion:

When I am running Topaccountdelete test calss

I am getting 50 % code coverage for Topaccount trigger
And  100% code coverage  for DeleteTopaccount trigger

Simalary

When I am running testTopinsert class
I am geeting 100% code covereage for Topaccount trigger
and 75% code coverage for DeleteTopaccount trigger

PFA for your reference.



Snap-1Snap-2
Manish Kumar 23Manish Kumar 23
User-added image
Image two was missing
Anoop yadavAnoop yadav
Run both test class at the same time
it will cover both 100%.
Manish Kumar 23Manish Kumar 23
Thax when I am running both together I am gettinh 100% code coverage for both User-added image
Manish Kumar 23Manish Kumar 23
SO I have to import both the test calss in production instance with my trigger and there also i have to run test class together??
Anoop yadavAnoop yadav
Yes, you should import both the test class with Trigger.

Running Test class in production is not mandatory, but you can run to test the error in production.
Manish Kumar 23Manish Kumar 23
Thank you very much for you inputs and support.

Regards,
Manish Tiwary