function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sunil Kandel 10Sunil Kandel 10 

Trigger not working for True condition

Could you please suggest what is wrong with this. My True condition is not triggering,There are true and False condition, it is always resulting to False.
I have custom object Obj_Direct_Bill_Customer__c with fields Direct_Bill_Customer__c (lookup field) and Disposal_Location__c(lookup field)
Customer object: FX5__Ticket_Item__c with Fields Customerforlogiconly__c (checkbox), Disposal_Location_on_Ticket_item__c (lookup Field) and Test_Direct_Bill_Customer__c (checkbox). If value of Customerforlogiconly__c=Direct_Bill_Customer__c  && Disposal_Location_on_Ticket_item__c=Disposal_Location__c, then update Test_Direct_Bill_Customer__c to True, if not False.


trigger Triggerupdatedirectbill on FX5__Ticket_Item__c (before insert, before update ) {

    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate) ) {
        List<FX5__Ticket_Item__c> items = Trigger.new;
        Set<String> accountsIds = new Set<String>();
        Set<String> wellsIds = new Set<String>();
        for(FX5__Ticket_Item__c item:items) {
            if(item.Customerforlogiconly__c!= null ) {
               accountsIds.add(item.Customerforlogiconly__c);
            }

            if(item.Disposal_Location_on_Ticket_item__c!= null ) {
                wellsIds.add(item.Disposal_Location_on_Ticket_item__c);
            }
        }

        Map<String, Obj_Direct_Bill_Customer__c> directInvoicesMap = new Map<String, Obj_Direct_Bill_Customer__c>();
        for(Obj_Direct_Bill_Customer__c di :[Select Direct_Bill_Customer__c, Disposal_Location__c from Obj_Direct_Bill_Customer__c where Direct_Bill_Customer__c in :accountsIds and Disposal_Location__c in :wellsIds] ) {
            if(!directInvoicesMap.containsKey(di.Direct_Bill_Customer__c+'-'+di.Disposal_Location__c) ) {
                directInvoicesMap.put(di.Direct_Bill_Customer__c+'-'+di.Disposal_Location__c,di);
            }
        }

        for(FX5__Ticket_Item__c item:items ) {
            if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'-'+item.Disposal_Location_on_Ticket_item__c) ) {
                item.Test_Direct_Bill_Customer__c = True;
                
            }
            item.Test_Direct_Bill_Customer__c = False;
            
        }
    }
}

Thank you
Best Answer chosen by Sunil Kandel 10
Maharajan CMaharajan C
Hi Sunil,

You trigger always making the field as false due the Line 29 it sholud be in else logic or remove that line

trigger Triggerupdatedirectbill on FX5__Ticket_Item__c (before insert, before update ) {

    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate) ) {
        List<FX5__Ticket_Item__c> items = Trigger.new;
        Set<String> accountsIds = new Set<String>();
        Set<String> wellsIds = new Set<String>();
        for(FX5__Ticket_Item__c item:items) {
            if(item.Customerforlogiconly__c!= null ) {
               accountsIds.add(item.Customerforlogiconly__c);
            }

            if(item.Disposal_Location_on_Ticket_item__c!= null ) {
                wellsIds.add(item.Disposal_Location_on_Ticket_item__c);
            }
        }

        Map<String, Obj_Direct_Bill_Customer__c> directInvoicesMap = new Map<String, Obj_Direct_Bill_Customer__c>();
        for(Obj_Direct_Bill_Customer__c di :[Select Direct_Bill_Customer__c, Disposal_Location__c from Obj_Direct_Bill_Customer__c where Direct_Bill_Customer__c in :accountsIds and Disposal_Location__c in :wellsIds] ) {
            if(!directInvoicesMap.containsKey(di.Direct_Bill_Customer__c+'-'+di.Disposal_Location__c) ) {
                directInvoicesMap.put(di.Direct_Bill_Customer__c+'-'+di.Disposal_Location__c,di);
            }
        }

        for(FX5__Ticket_Item__c item:items ) {
            if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'-'+item.Disposal_Location_on_Ticket_item__c) ) {
                item.Test_Direct_Bill_Customer__c = True;
                
            }
            
            else {
            item.Test_Direct_Bill_Customer__c = False;
            }

        }
    }
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Sunil,

You trigger always making the field as false due the Line 29 it sholud be in else logic or remove that line

trigger Triggerupdatedirectbill on FX5__Ticket_Item__c (before insert, before update ) {

    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate) ) {
        List<FX5__Ticket_Item__c> items = Trigger.new;
        Set<String> accountsIds = new Set<String>();
        Set<String> wellsIds = new Set<String>();
        for(FX5__Ticket_Item__c item:items) {
            if(item.Customerforlogiconly__c!= null ) {
               accountsIds.add(item.Customerforlogiconly__c);
            }

            if(item.Disposal_Location_on_Ticket_item__c!= null ) {
                wellsIds.add(item.Disposal_Location_on_Ticket_item__c);
            }
        }

        Map<String, Obj_Direct_Bill_Customer__c> directInvoicesMap = new Map<String, Obj_Direct_Bill_Customer__c>();
        for(Obj_Direct_Bill_Customer__c di :[Select Direct_Bill_Customer__c, Disposal_Location__c from Obj_Direct_Bill_Customer__c where Direct_Bill_Customer__c in :accountsIds and Disposal_Location__c in :wellsIds] ) {
            if(!directInvoicesMap.containsKey(di.Direct_Bill_Customer__c+'-'+di.Disposal_Location__c) ) {
                directInvoicesMap.put(di.Direct_Bill_Customer__c+'-'+di.Disposal_Location__c,di);
            }
        }

        for(FX5__Ticket_Item__c item:items ) {
            if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'-'+item.Disposal_Location_on_Ticket_item__c) ) {
                item.Test_Direct_Bill_Customer__c = True;
                
            }
            
            else {
            item.Test_Direct_Bill_Customer__c = False;
            }

        }
    }
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer
Sunil Kandel 10Sunil Kandel 10
Hi Maharajan, thank you so much. Once I removed the code, it worked perfectly fine for True statement. However, I wanted to add, if the combination does not exist then update Test_Direct_Bill_Customer__c to False. Can we achieve the same as well. Please suggest.
Maharajan CMaharajan C
Hi Sunil,

You have removed that line right and now only you need to add the else condition like below:

           if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'-'+item.Disposal_Location_on_Ticket_item__c) ) {
                item.Test_Direct_Bill_Customer__c = True;    
            }
            else {
            item.Test_Direct_Bill_Customer__c = False;
            }

In the above post also i have mentioned this.

Happy New year!!!!!!!!!!!!!

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
Sunil Kandel 10Sunil Kandel 10
Thank you Maharjan.