1

I have a trigger which will update checkbox on a child object when Update is made on Parent for mLookup__c field. But the checkbox is always false even after the trigger is executed.

Can anyone please help me with this ?

Trigger:

trigger updatefield on customObj__c (before update)
{
    Set<ID> Ids = new Set<ID>();

    for(customObj__c cb : trigger.new){

        Ids.add(cb.ID);

        }
    List<customObj__c> cbList = [Select ID,Name,mLookup__c from customObj__c where ID IN:Ids];
    List<Product__C> prodlist = [Select Id,checkbox__c,relatedCB__Lookup__c from Product__C where relatedCB__Lookup__r.Id IN: Ids];
    List<Product__C> updateList = new List<Product__C>();

    for(customObj__c op: cbList){

        if(!prodlist.isEmpty()){

        for(Product__C prod :prodlist){

        if(op.mLookup__c != null){
            prod.checkbox__c = true;

           }
           // when checked in debug it is always entering this condition
           if(op.mLookup__c == null){

            prod.checkbox__c = false;

           }
           updateList.add(prod);
        }
    }
    }
    Update updateList;
}
2
  • If you want the checkbox to be ticked when the parent is updated, the trigger should be written on the parent. Also, do you want to update the checkbox of all the child records or do you have any particular logic?
    – User2529
    Commented Feb 24, 2017 at 5:00
  • Debug whats coming in mlookup__c ? Why use two if's.You can use if and else as well
    – Samir
    Commented Feb 24, 2017 at 5:04

2 Answers 2

0

I think this could be more efficient without using extra for loop.

trigger updatefield on customObj__c (before update)
{
    List<Product__C> prodlist = [Select Id,checkbox__c,relatedCB__Lookup__c, relatedCB__Lookup__r.mLookup__c 
    FROM Product__C where relatedCB__Lookup__r.Id IN: trigger.newMap.keySet()];
    if(!prodlist.isEmpty())
    {
        for(Product__C prod :prodlist)
        {
            if(prod.relatedCB__Lookup__r.mLookup__c != null)
            {
                prod.checkbox__c = true;
            }           
            else
            {
                prod.checkbox__c = false;
            }
        }
    }
    update prodlist;
}
1
  • Your answer look incorrect to me. In before update if you query parent value thenyou will get old value. This will not work. Commented Feb 25, 2017 at 7:18
1

You have made thing complicated. Here is a simplified code for you

trigger updatefield on customObj__c (after update)
{

    List<Product__C> prodlist = [Select Id,checkbox__c,relatedCB__Lookup__c,elatedCB__Lookup__r.mLookup__c  from Product__C where relatedCB__Lookup__c IN: trigger.newMap().Keyset()];

    for(Product__C prod : prodlist){
        if(prod.elatedCB__Lookup__r.mLookup__c != null)
            prod.checkbox__c = true;
        else
            prod.checkbox__c = false;

    }
    Update prodlist;
}

This will work for you.

Note: You can also use process builder to achieve the same functionality or a formula field to display checked or unchecked based on lookup.

0

You must log in to answer this question.

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