I have written a test class to delete the related list records and update master detail Object fields value when the Master detail two fields(Model&Version) changes.But getting error
Trigger:
trigger ModelVersionChange on Opportunity_car_set__c (before update) {
List<Service__c> newService = [select id from Service__c where Opportunity_car_set__c in:trigger.newmap.keyset()];
List<Accompaniment__c> newAccompaniment = [select id from Accompaniment__c where Opportunity_car_set__c in:trigger.newmap.keyset()];
Map<Id,Opportunity_car_set__c> mOldValue = new Map<Id,Opportunity_car_set__c>();
mOldValue = trigger.oldMap;
for(Opportunity_car_set__c newcont: trigger.new) {
if((newcont.Model__c != mOldValue.get(newcont.Id).Model__c)||(newcont.Version__c != mOldValue.get(newcont.Id).Version__c)) {
newcont.Price_HT__c = null;
newcont.TVN_Amount__c = null;
delete newService;
delete newAccompaniment;
}
}
}
Error:Apex trigger ModelVersionChange caused an unexpected exception, contact your administrator: Rfleet_ModelVersionChange: execution of BeforeUpdate caused by: System.DmlException: Delete failed. First exception on row 0 with id a0F4E000000CsrHUAS; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a0C4E000000mIrk) is currently in trigger Rfleet_ModelVersionChange, therefore it cannot recursively update itself: []: Trigger.Rfleet_ModelVersionChange: line 11, column 1