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);

    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;

    update buildUpdate;
  • If Warehouse_Name__c is a master-detail relationship, and "allow reparenting" is not set, any attempt to change or null out the field will be silently ignored. Make sure your field type is actually set to lookup, because master-details can't have a null value anyways. – sfdcfox May 7 '14 at 0:36
  • It is a lookup field, and settings for "What to do if the lookup record is deleted? "Clear the value of this field". (will this impact anything)? I am currently getting an error of Apex trigger Diss caused an unexpected exception, contact your administrator: Diss: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Diss: line 33, column 1. any idea? – Slice32 May 7 '14 at 3:31
  • You'll need to add this "if(bui.Status__c != null && bui.Status__c.equals('Complete')){" to your 2nd for-loop as this is the filter used inside your first for-loop – Richard Durrant May 7 '14 at 9:19

You've got way too much code. Consider taking some of the basic APEX webinars on Triggers..

To do what you described, you could do just this (note that I made it a before trigger instead of an after trigger):

trigger Diss on Building__c (before insert, before update) { 
    for (Building__c childObj : Trigger.new) {
        if(childObj.Status__c != null && childObj.Status__c.equals('Complete')){
            childObj.Warehouse_Name__c = null;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.