You need to sign in to do that
Don't have an account?
txmapp
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
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......
Now if there can be many aspirante__c for a given Vacant__c and only one lookup on Vacant__c do this:
All Answers
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)
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
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 agree with the reply from Startz26
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
}
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......
Now if there can be many aspirante__c for a given Vacant__c and only one lookup on Vacant__c do this: