I am having a problem creating a trigger that sets a lookup field on the child object to blank when a picklist value changes on the child object. Background:
There are two Objects related by a lookup relationship Building__c
(Child) and Warehouse__c
(Parent)
The Child Object Building__c
has three fields, Name
, Warehouse_Name__c
(Lookup to Warehouse), and Status
(picklist). I need a trigger that when Status
is changed to Complete the field Warehouse_Name__c
should be blank without deleting the record.
trigger Diss on Building__c (after insert, after update) {
Map<ID, Warehouse__c> parentware = new Map<ID, Warehouse__c>();
List<Id> listIds = new List<Id>();
for (Building__c childObj : Trigger.new) {
if(childObj.Status__c != null && childObj.Status__c.equals('Complete')){
//System.debug("ids for query: " + childObj.Id);
listIds.add(childObj.Id);
}
}
parentware = new Map<Id, Warehouse__c>([SELECT id, Name,(SELECT ID, Status__c, Warehouse_Name__c FROM Building__r Where Status__c = 'Complete') FROM Warehouse__c WHERE ID IN :listIds]);
// for (Warehouse__c build: Trigger.new){
// System.debug("2nd loop: " + build.toString());
// Warehouse__c myParentOpp = parentware.get(build.Warehouse_Name__c);
// myParentOpp.Name = build.Warehouse_Name__c;
// myParentOpp.Name = null;
// }
List<Building__c> buildUpdate = new List<Building__c>();
for (Building__c bui : Trigger.new) {
Warehouse__c wl = parentware.get(bui.Warehouse_Name__c);
if (bui.Warehouse_Name__c.equals(wl.Name)) {
bui.Warehouse_Name__c = null;
buildUpdate.add(bui);
}
}
update buildUpdate;
}