You need to sign in to do that
Don't have an account?
Manish 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
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
add try, catch on insert and update.
and test.StartTest and Test.stopTest.
It will work.
All Answers
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
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.
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??
add try, catch on insert and update.
and test.StartTest and Test.stopTest.
It will work.
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 ;
}
Try this and Add Mandatory fields for Account.
acc.Top_Account__c = True;
istead
acc.Top_Account__c == True;
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.
Image two was missing
it will cover both 100%.
Running Test class in production is not mandatory, but you can run to test the error in production.
Regards,
Manish Tiwary