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
Arav SundarArav Sundar 

deleting parent should delete the child with self lookup relationship getting an error pls suggest

Hi All , 

i am new to the Coding and i really want to learn the coding and intrested too and i am finding very difficult to build the logic and to write can anyone please suggest ..

i am trying to build the code for the requirement " i am havng an self lookup relationship with opportunity and when i delete the parent child should also get deleted " Below is the code can anyone pls suggest and the error i am getting is " Error: Compile Error: sObject type 'childofopp' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 7 column 37 " Not sure what is the mistake  i have done 

Standard object = Opportunity 
Custom object = Child_of_Oppty__c (having an self lookup realtionship) 

trigger DeletionofParentchildgetsdeleted on Opportunity (before delete) {

List<id> OpptyID = new list<id>();
for( Opportunity Oppty : trigger.old){
opptyid.add(oppty.id);
}
list<Child_of_Oppty__c> childofopp =[select id from childofopp where opportunityid in : opptyid];
delete childofopp;

}

 
rajat Maheshwari 6rajat Maheshwari 6

Arav,

Could you please let me know - : 

Child_of_Oppty__c is lookupField on opportunity ?

I will help you to assist you in coding part, Don't be worry :)

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

Akshay_DhimanAkshay_Dhiman
Hi Arav,

In your code you are trying to delete the sobject "childofopp " but you are getting error in  at line 7 column 37 " because you are not using the API Name in your code so I suggest you to try this code .
  • In your query you are not using the appropriate API name that’s why it is generating error.and please also check the API name of opportunity lookup in subject or custom object “Child_of_Oppty__c ”.
  • In your code always check Null Pointer Exception and try to use best practices.

Try this code:
trigger DeletionofParentchildgetsdeleted on Opportunity (before delete) 
{
  List<id> OpptyID = new list<id>();
   for(Opportunity Oppty : trigger.old){
     OpptyID.add(oppty.id);
}
  List<Child_of_Oppty__c> childofopp =[select id from Child_of_Oppty__c where Opportunityes__c in : OpptyID];
    if(childofopp !=null){
       delete childofopp;
}
}
I hope it will help you.

Regards,
Akshay
Please mark my answer as a solution.
rajat Maheshwari 6rajat Maheshwari 6

Arav,

I am assuming Opportunity have self lookup as you have said, so "Child_of_Oppty__c" is field API name associated with opportunity.

Please let me correct, If I am wrong - 

 

trigger DeletionofParentchildgetsdeleted on Opportunity (after delete) {

Set<id> OpptyID = new Set<id>();
for(Opportunity Oppty : trigger.old){
  if(Oppty.Child_of_Oppty__c!=null)
         opptyid.add(oppty.id);

}
list<Opportunity> childofopp = new list<Opportunity>([select id from Opportunity where Child_of_Oppty__c in : opptyid]);


if(childofopp!=null && !childofopp.isEmpty())
     delete childofopp;

}
 

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

jane1234jane1234
Hi Arav,
could you please let me know whats is the error?
I think can you change your triiger  to after delete ....i think it may work