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 

trigger after delete error help me

hi

i have a error in this trigger

is for 2 objects that is related by a lookup filed or a master detail

Vacante__c , and Asignados __c here is the lookup field that search a Vacante__c

that i want to do is prevent delete a Vacante__c when there is one or more records of the object Asignados__c related to the record that i want to delete

 

trigger preventDelete on Vacante__c(before delete){

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

For(Asignados__c a : [Select Vacante__c From Asignados__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');

}





}

 but i get this error Method does not exist or incorrect signature: [SET<Id>].containsKey(Id)  

can somebody help me???

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

Here to go 

trigger validateChild on Vacante__c (before delete) 
{
    
	Set<Id> stId = new Set<Id>();
    List<Asignados__c> lstRe =[Select Id, Vacante__c from Asignados__c where Vacante__c in : Trigger.oldMap.KeySet() ];
    for(Asignados__c objp : lstRe)
    {
        stId.add(objp.Vacante__c);
    }
  
    for(Vacante__c objA : Trigger.old)
    {
        if(stId.contains(objA.Id))
        {
        objA.addError('YOU CAN NOT DELETE THIS RECORD ...... HAHAHAHAH  ');
        }
    }
}

 

 

This code is tested , If you need any other help, please let us know .

 

 

 

Thanks ,

bForce

All Answers

b-Forceb-Force

For set Data Type,

Apex having some different methods to check , whether that specific value exist or not , you can use below signature 

 

 

 

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

hi thanks for your help

 

but my trigger not fire, allows to delete the Vacante__c 

i have 1 record that is related to the vacante__c that i want to delete if the trigger run i can't delete for the child

 

the master detail is in the object Asignados__c 

 

can you halpe me??

b-Forceb-Force

could you please post your requirement in brief , 

Also provide all related API names for both object and fields 

 

I will provide script for it . 

 

Thanks,

bForce

txmapptxmapp

namespace txmrecruitment

object Vacant__c

object Asignados__c

onject Aspirante__c

 

Asignados__c 

*Vacante__c  masterdetail to Vacante__c

*Aspirante__c masterdetail to Aspirante__c

 

i want to prevent  delete a record or records in Vacante__c if have one or more child, the child is the records of object Asignados__c

 

i want to create a trigger before delete

if the record has one or more genereate error or alert 'this Vacante can't be delete '

 

trigger preventDelete on Vacante__c(before delete)
{

  
    Set<ID> vacanteid = New Set<ID>();
    For (Vacante__c va:[Select Id From Vacante__c where Id IN : Trigger.oldMap.keySet()])
    vacanteid.add(va.id);
    
    Set<ID> vacant = New Set<ID>();
    For(Asignados__c a : [Select Vacante__c From Asignados__c Where Vacante__c IN :Trigger.oldMap.keySet()])
    vacant.add(a.Vacante__c);

    //i don't know what to do after

    
}

 

b-Forceb-Force

Here to go 

trigger validateChild on Vacante__c (before delete) 
{
    
	Set<Id> stId = new Set<Id>();
    List<Asignados__c> lstRe =[Select Id, Vacante__c from Asignados__c where Vacante__c in : Trigger.oldMap.KeySet() ];
    for(Asignados__c objp : lstRe)
    {
        stId.add(objp.Vacante__c);
    }
  
    for(Vacante__c objA : Trigger.old)
    {
        if(stId.contains(objA.Id))
        {
        objA.addError('YOU CAN NOT DELETE THIS RECORD ...... HAHAHAHAH  ');
        }
    }
}

 

 

This code is tested , If you need any other help, please let us know .

 

 

 

Thanks ,

bForce

This was selected as the best answer
txmapptxmapp

thanks for your help

 

i checked and prove but the record is deleted and have one child

 

i don't know what to do

 

i have 4 records of Vacante_c then i created other 4 records on Asignados__c  that is related with master detail for each record on Vacante__c

i hace 3 days with this problem

any suggestion??

b-Forceb-Force

ohh cmon,

This code is tested properly and It works :)

 

Could you please post your trigger code... and make sure your trigger is active .

 

you can get back to me  at balasahebwani@gmail.com

 

Thanks,

bForce

txmapptxmapp

thanks for your help friend

 

i solved my problem

 

thanks

b-Forceb-Force

Hey ,

I just saw snapshot or your trigger, please change the error message as per your business need ,

 

Happy coding..

 

Cheers,

Bala