You need to sign in to do that
Don't have an account?
Robbert Bos1
Delete before insert when not finding a lookup relationship
Hello all,
first, I have to see this is my first crack at APEX. I use an external database to fill some custom objects with data we need for the proces but not all data is needed. What I'm trying is to find a parent record based on the external id value. If the parent record is found: Fill the lookup relationship. So far, so good.
But when there is no parent found I want to delete the record or not have it saved. This is where the errors start. Somehow I cannot delete a record which entererd my trigger.
Does anybody have an idea?
first, I have to see this is my first crack at APEX. I use an external database to fill some custom objects with data we need for the proces but not all data is needed. What I'm trying is to find a parent record based on the external id value. If the parent record is found: Fill the lookup relationship. So far, so good.
But when there is no parent found I want to delete the record or not have it saved. This is where the errors start. Somehow I cannot delete a record which entererd my trigger.
Does anybody have an idea?
trigger KoppelPersoonsfase on Persoonsfase__c (before insert, before update) { for (Persoonsfase__c p : Trigger.new) { if(p.PersoonID__c != null){ string FlexId = p.PersoonID__c; List<Parent__c> Kandidaat = [SELECT Id FROM Parent__c WHERE FlexService_ID__c = :FlexId]; p.Werknemer__c = Kandidaat[0].Id; } else { delete p; } } }
delete p;
with:
p.AddError('No lookup found.');
or some other error message you want. That will prevent the records in the Trigger from being saved.