0

I have to write a trigger to prevent deleting of Apartment records if there are flat records related to it

Here is my solution

.-------------

trigger DoNotDeleteAccountHavingRelatedContact on Apartment__c(before delete)  
{  
    for(Apartment__c acc : [SELECT Id FROM Apartment__c WHERE Id IN (SELECT Apartment__c.id FROM Flat__c) AND Id IN :Trigger.oldMap.KeySet()]){
        Trigger.oldMap.get(acc.Id).adderror('Account cannot be deleted');
    }
}

Please help..Thank u in advance

1 Answer 1

0

Assuming Apartment__c is the parent object and Flat__c is the child object, then instead of:

SELECT Apartment__c.id FROM Flat__c

you just need:

SELECT Apartment__c FROM Flat__c

because the Apartment__c field holds the Id. (Apartment__r.Id will also provide the apartment Id.)

2
  • There is Master_Detail Relationship between Flat__c and Apartment__c where Flat__c is a Parent and Apartment__c is a Child
    – user38514
    Commented Oct 23, 2016 at 14:24
  • But ur Logic Works....Thank You ...
    – user38514
    Commented Oct 23, 2016 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .