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
txmapptxmapp 

how to prevent delete a object with other object related?

 i have 2 custom objects  Vacante__c and Aspirante__c, there are 2 lookup fields from Vacante__c to Aspirante__c and Aspirante__c to Vacante__c

 

i want to prevent delete a record of Vacante__c that is related with a Aspirante__c

this olny can be deleted when nobody Aspirante is related with a Vacante that i want to delete

 

i want to create a trigger but im new and i dont know how to do this, can yuo help me?

 

Aspirante__c is like a Applicant

Vacante __is is like a Job

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

 

 

Since you have a lookup to Asiprante__c on the parent this is really easy. Not sure why you have a lookup to the child on the parent and the parent on the child but......

 

 

trigger preventDelete on Vacante__c(before delete){


For(Vacante__c v : trigger.old){

   If(v.aspirante__c != Null)
      v.addError('You cannot delete this');

}





}

 

Now if there can be many aspirante__c for a given Vacant__c and only one lookup on Vacant__c do this:

 

trigger preventDelete on Vacant__c(before delete){

Set<ID> vacant = New Set<ID>();

For(Aspirante__c a : [Select Vacante__c From Aspirante__c Where Vacante__c IN :trigger.old])
     vacant.add(a.Vacante__c);

For(Vacante__c v : trigger.old){

   If(vacant.containsKey(v.ID))
      v.addError('You cannot delete this');

}





}

 

All Answers

Starz26Starz26

Here is an example....

 

This is on Opportunity:

 

Implementation_Hand_Off is a lookup to Opportunity

FSA__c is a Master-Detail to Implementation_Hand_Off

 

I am checking to see if an opportunity has an existing FSA__c (via the related Implementation Hand Off)

 

//Delete Trigger WILL
    //  1. Check to see if there is an FSA associated with the records in the trigger
    //      a. For each record - if there is an FSA, do not allow delete
    //      b. For each record - If there is no FSA - Allow Delete
    
    
    //List of IHO's and FSA's for the Opportunity's  
    List<Implementation_Hand_Off__c> lIHO = [SELECT ID, Opportunity_Name__c, (Select ID from FSA2IHO__r) FROM Implementation_Hand_Off__c WHERE Opportunity_Name__c IN :idOpp];
     
    //List and Map of Opp ID to IHO's associated with the Opportunity's   
    Map<ID, Implementation_Hand_OfF__C> mIHO = New Map<ID,Implementation_Hand_Off__c>();        
        
    for(Implementation_Hand_Off__c oIHO :lIHO){
        
        mIHO.put(oIHO.Opportunity_Name__c,oIHO);
        
    }

    //For each Opportunity in the trigger
    for(Opportunity oOpp : trigger.old){
        
        //If this Opportunity has FSA's with the IHO, do not allow delete
        for(FSA__c f : mIHO.get(oOpp.id).FSA2IHO__r){
            oOpp.addError('An FSA Exists for this Opportunity and it cannot be deleted');
            break;
        }//End FSA Check Loop
        
    }//end for oOpp Loop   
}//End isDelete trigger 

 

txmapptxmapp

i want something more simple

 

1.- get the id,name of the job that i want to delete
2.- select count(id or name)that count all the applicant that have the same job in the field job of the applicant
3.- if the result of this select, like a count is >0 generate error "this job can't be delete because
there are applicants that is related with this job"
4.- if the result of the select , like a count is ==0 do nothing allow to delete

 

 

please help me

Starz26Starz26

 

You have everything you need in front of you, please take the time to review it, read documentation, and take a stab at it. Then if you still have issues I would be more than happy to help you further.

 

 

 And the example I provided is not much more complex than what you need to do if writing the code properly..It does look that way since you are new to this.

 

If you have specific questions, let us know and we can point you in the right direction.

 

Also, keep in mind, it is not as simple as doing a query and getting a count. You have to take into considertaion that there could be multiple (up to 200) different records in the transaction

 

 

I.S.KI.S.K

I agree with the reply from Startz26

I.S.KI.S.K

The following Code Will completely suitable for your requirement.

 

//This Trigger Should Be executed at before delete contest
   map<Id,set<Id>> vacanteIdWithAspiranteId=new map<Id,set<Id>>();
   list<Aspirante__c > listRec=new list<Aspirante__c >([Select Id,Vacante__c From Aspirante__c  where Vacante__c IN :Trigger.oldMap.keyset()]); 
   //skip when there is no Asprirante attached
   if(listRec.size()>0){
  (Aspirante__c  rec:listRec){
   set<Id> sceIdSet=vacanteIdWithAspiranteId.get(rec.Vacante__c);
   if(sceIdSet==null){
   sceIdSet=new set<Id>();
   }
   if(rec.Id !=null){
   sceIdSet.add(rec.Id);
   }
   vacanteIdWithAspiranteId.put(rec.Vacante__c, sceIdSet);
  }    //Get Asprirante  Ends Here    
   
   //Prevent parent deletion End Here
   
    if(vacanteIdWithAspiranteId!=null){
   for(Vacante__c job:Trigger.old){
   if(vacanteIdWithAspiranteId.get(job.Id).size()>0){
   job.addError('vacante Cannot be deleted until all the associated Aspirente are deleted');
   }
   }     //Error Message Thrown Ends    
   }
  

Starz26Starz26

 

 

Since you have a lookup to Asiprante__c on the parent this is really easy. Not sure why you have a lookup to the child on the parent and the parent on the child but......

 

 

trigger preventDelete on Vacante__c(before delete){


For(Vacante__c v : trigger.old){

   If(v.aspirante__c != Null)
      v.addError('You cannot delete this');

}





}

 

Now if there can be many aspirante__c for a given Vacant__c and only one lookup on Vacant__c do this:

 

trigger preventDelete on Vacant__c(before delete){

Set<ID> vacant = New Set<ID>();

For(Aspirante__c a : [Select Vacante__c From Aspirante__c Where Vacante__c IN :trigger.old])
     vacant.add(a.Vacante__c);

For(Vacante__c v : trigger.old){

   If(vacant.containsKey(v.ID))
      v.addError('You cannot delete this');

}





}

 

This was selected as the best answer