You need to sign in to do that
Don't have an account?
bathy
Apex trigger to Update the Parent record and delete the record
Hi,
I am trying to develop a trigger to Update the Aprent Record and Delete the curretn record. We have a contract custom object and Offer custom object where Offer is the parent and COntract id the child. My requirement is once the COntract is submitted for cancellation(I customised approval process to cancellation process) and then its cancellation is approved then the Offer(parent Object) Status(custom Field in Offer) must be updated to Cancelled and the child contract record must be deleted.
so far I have developed the following trigger:
trigger Updateoffer on pba__Closing__c (after update) {
Set<String> offerids= new Set<String>();
for (pba__Closing__c cl : Trigger.new)
{
if(cl.Cancellation_Approved__c == true){
offerids.add(cl.pba__Offer__c);
}}
List<pba__Offer__c> offers= [SELECT Id,pba__SystemIsCancelled__c FROM pba__Offer__c WHERE Id =: offerids];
Map<String, pba__Closing__c> cprmap = new Map<String, pba__Closing__c>();
for (pba__Closing__c c : Trigger.new){
cprmap.put(c.pba__Offer__c, c);
}
for (pba__Offer__c o : offers) {
if (cprmap.containsKey(o.Id))
{
{
o.pba__SystemIsCancelled__c = true;
}
}
}
update offers;
delete [select id from pba__Closing__c where id in :Trigger.new];
}
This trigger is deleting the newly created Contracts, i belive it is because there is no if condition for the select statement at the bottom. Can anyone suggest corrections for this.
Thanks in advance.
I am trying to develop a trigger to Update the Aprent Record and Delete the curretn record. We have a contract custom object and Offer custom object where Offer is the parent and COntract id the child. My requirement is once the COntract is submitted for cancellation(I customised approval process to cancellation process) and then its cancellation is approved then the Offer(parent Object) Status(custom Field in Offer) must be updated to Cancelled and the child contract record must be deleted.
so far I have developed the following trigger:
trigger Updateoffer on pba__Closing__c (after update) {
Set<String> offerids= new Set<String>();
for (pba__Closing__c cl : Trigger.new)
{
if(cl.Cancellation_Approved__c == true){
offerids.add(cl.pba__Offer__c);
}}
List<pba__Offer__c> offers= [SELECT Id,pba__SystemIsCancelled__c FROM pba__Offer__c WHERE Id =: offerids];
Map<String, pba__Closing__c> cprmap = new Map<String, pba__Closing__c>();
for (pba__Closing__c c : Trigger.new){
cprmap.put(c.pba__Offer__c, c);
}
for (pba__Offer__c o : offers) {
if (cprmap.containsKey(o.Id))
{
{
o.pba__SystemIsCancelled__c = true;
}
}
}
update offers;
delete [select id from pba__Closing__c where id in :Trigger.new];
}
This trigger is deleting the newly created Contracts, i belive it is because there is no if condition for the select statement at the bottom. Can anyone suggest corrections for this.
Thanks in advance.
and use the set in the delete SOQL instead of Trigger.new
If that last line doesn't work (might not be able to delete the same records of an update); move the delete logic into the OfferTrigger after update:
thanks for your help. Appreciate that. I was able to accomplish this by creating two separate Apex triggers.
Regards,