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
Mamadou Diallo 14Mamadou Diallo 14 

Trigger not firing if not null criteria

Hello,

I have 3 custom objects: Commission, Tax ID and Broker Appointment. Commission and Broker Appointment are not related but Tax ID has 1 to many relationship with each of those objects. Commission and Broker Appointment have TAX ID as a lookup field. 
I want to updated a field in Commission when Broker Appointment Status is Approved. 

Here is the code I built.
trigger ReleaseCommissionWhenBrokerApproved on Broker_Appointment__c (after update) {

     map<Id, Broker_Appointment__c> brokerApptIds = new map<Id, Broker_Appointment__c>();
   
        for (Broker_Appointment__c a : trigger.New){
            if ( a.Tax_ID__c != null && a != null && a.Appointment_Status__c == 'Approved'){
            brokerApptIds.put(a.Tax_ID__c, a);
          }
        }
    
    List<Commission__c> comm = [ SELECT Id, Tax_ID__c, Underwriter__c, Domicile_State1__c, Broker_Writing_Agent__c, On_Hold_Reason__c, Hold_Payment__c FROM Commission__c WHERE Tax_ID__c IN :brokerApptIds.keySet() AND Hold_Payment__c = TRUE];
  
    Broker_Appointment__c appt;
       
    for (Commission__c c: comm){
        
             if(!brokerApptIds.isEmpty() && brokerApptIds.containsKey(c.Tax_ID__c) && appt != null ){
       			 if (appt.Underwriter_Account__c == c.Underwriter__c && appt.Producer_Contact__c == c.Broker_Writing_Agent__c && appt.State__c == c.Domicile_State1__c)
                 {
                    c.Hold_Payment__c = FALSE;      
               }  
          }
       update comm;
    }        
}

When I remove the line "if (appt.Underwriter_Account__c == c.Underwriter__c && appt.Producer_Contact__c == c.Broker_Writing_Agent__c && appt.State__c == c.Domicile_State1__c)", the trigger works but I need to compare thos fields before updating the fields.

Help need please. Maybe I'm missing something.

Thank you.
Best Answer chosen by Mamadou Diallo 14
Apoorv Saxena 4Apoorv Saxena 4
Hi Mamadou,

Please try the following code and let me know how it works out for you:
 
rigger ReleaseCommissionWhenBrokerApproved on Broker_Appointment__c (after update) {

     map<Id, Broker_Appointment__c> brokerApptIds = new map<Id, Broker_Appointment__c>();
   
        for (Broker_Appointment__c a : trigger.New){
            if ( a.Tax_ID__c != null && a != null && a.Appointment_Status__c == 'Approved'){
            brokerApptIds.put(a.Tax_ID__c, a);
          }
        }
    
    List<Commission__c> comm = [ SELECT Id, Tax_ID__c, Underwriter__c, Domicile_State1__c, Broker_Writing_Agent__c, On_Hold_Reason__c, Hold_Payment__c FROM Commission__c WHERE Tax_ID__c IN :brokerApptIds.keySet() AND Hold_Payment__c = TRUE];
     
	for(Broker_Appointment__c appt:trigger.new)
		for (Commission__c c: comm){
			
				 if(!brokerApptIds.isEmpty() && brokerApptIds.containsKey(c.Tax_ID__c) && appt != null ){
					 if (appt.Underwriter_Account__c == c.Underwriter__c && appt.Producer_Contact__c == c.Broker_Writing_Agent__c && appt.State__c == c.Domicile_State1__c)
					 {
						c.Hold_Payment__c = FALSE;
				   }  
			  }
		}       
	}
	update comm;
}

Please mark this question as Solved if this helps you so that others can view it as a proper solution.

Thanks,
Apoorv

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Mamadou,

Please try the following code and let me know how it works out for you:
 
rigger ReleaseCommissionWhenBrokerApproved on Broker_Appointment__c (after update) {

     map<Id, Broker_Appointment__c> brokerApptIds = new map<Id, Broker_Appointment__c>();
   
        for (Broker_Appointment__c a : trigger.New){
            if ( a.Tax_ID__c != null && a != null && a.Appointment_Status__c == 'Approved'){
            brokerApptIds.put(a.Tax_ID__c, a);
          }
        }
    
    List<Commission__c> comm = [ SELECT Id, Tax_ID__c, Underwriter__c, Domicile_State1__c, Broker_Writing_Agent__c, On_Hold_Reason__c, Hold_Payment__c FROM Commission__c WHERE Tax_ID__c IN :brokerApptIds.keySet() AND Hold_Payment__c = TRUE];
     
	for(Broker_Appointment__c appt:trigger.new)
		for (Commission__c c: comm){
			
				 if(!brokerApptIds.isEmpty() && brokerApptIds.containsKey(c.Tax_ID__c) && appt != null ){
					 if (appt.Underwriter_Account__c == c.Underwriter__c && appt.Producer_Contact__c == c.Broker_Writing_Agent__c && appt.State__c == c.Domicile_State1__c)
					 {
						c.Hold_Payment__c = FALSE;
				   }  
			  }
		}       
	}
	update comm;
}

Please mark this question as Solved if this helps you so that others can view it as a proper solution.

Thanks,
Apoorv
This was selected as the best answer
Mamadou Diallo 14Mamadou Diallo 14
Thank you Apoorv.

I tried your code but it did not work.  Here is the code I used.
trigger ReleaseCommissionWhenBrokerApproved on Broker_Appointment__c (after update) {

 // if(Trigger.isupdate || Trigger.isinsert){
        
     map<Id, Broker_Appointment__c> brokerApptIds = new map<Id, Broker_Appointment__c>();
   
        for (Broker_Appointment__c a : trigger.New){
            if ( a.Tax_ID__c != null && a != null && a.Appointment_Status__c == 'Approved'){
            brokerApptIds.put(a.Tax_ID__c, a);
          }
        }
    
    List<Commission__c> comm = [ SELECT Id, Tax_ID__c, Underwriter__c, Domicile_State1__c, Broker_Writing_Agent__c, On_Hold_Reason__c, Hold_Payment__c FROM Commission__c WHERE Tax_ID__c IN :brokerApptIds.keySet() AND Hold_Payment__c = TRUE];
  
    for(Broker_Appointment__c appt: trigger.new){
       
    for (Commission__c c: comm){
        
            if(!brokerApptIds.isEmpty() && brokerApptIds.containsKey(c.Tax_ID__c) && appt != null ){
                          
       			 if (appt.Underwriter_Account__r.Name == c.Underwriter__c && appt.Producer_Contact__c == c.Broker_Writing_Agent__c && appt.State__c == c.Domicile_State1__c)
                 {
                    c.Hold_Payment__c = FALSE;      
               }  
          }
       update comm;
    }        
} 
}

Underwriter_Account__c is a lookup field but Underwriter__c is formula field that display the name of the underwriter. It's why I used Underwriter_Account__r.Name in the criteria.
Producer_Contact__c and Broker_Writing_Agent__c are lookup fields.
State__c is a picklist and Domicile_State1__c is formula field.

Thanks for your support.
Apoorv Saxena 4Apoorv Saxena 4
Hi Mamadou,

Could you elaborate what does not work here?
Please double check the data you are entering in the record when testing trigger, whether it meets the criteria specified in your If condition or not.
It will only update if condition is met.
Mamadou Diallo 14Mamadou Diallo 14
Hi Apoorv,

I have a test Broker Appointment with the following date: 
Tax ID = 81-0908746 ;  Underwriter Account = TheUnderwiters ; Producer Contact = John Doe; Sate = MA, Status = Pending
For Commission, I have:
Tax ID = 81-0908746 ;  Underwriter  = TheUnderwiters ; Broker Writing Agent = John Doe; Domicile Sate = MA, Hold Commission = TRUE

When I changed the Broker Appointment Status to Approved, the Hold Commission remains TRUE and does not change.
Apoorv Saxena 4Apoorv Saxena 4
Hi Mamadou,

Try using this code:
 
trigger ReleaseCommissionWhenBrokerApproved on Broker_Appointment__c (after update) {
        
    Set<Id> brokerApptIds = new Set<Id>();
   
	for (Broker_Appointment__c a : trigger.New){
		if ( a.Tax_ID__c != null && a.Appointment_Status__c == 'Approved'){
			brokerApptIds.add(a.Tax_ID__c);
		}
	}
    
    List<Commission__c> comm = [ SELECT Id, Tax_ID__c, Underwriter__c, Domicile_State1__c, Broker_Writing_Agent__c, On_Hold_Reason__c, Hold_Payment__c FROM Commission__c WHERE Tax_ID__c IN :brokerApptIds AND Hold_Payment__c = TRUE];
  
    for(Broker_Appointment__c appt: trigger.new){
       
		for (Commission__c c: comm){
							  
			if ((appt.Tax_ID__c==c.Tax_ID__c) && (appt.Underwriter_Account__r.Name == c.Underwriter__c) && (appt.Producer_Contact__c == c.Broker_Writing_Agent__c) && (appt.State__c == c.Domicile_State1__c))
			{
			c.Hold_Payment__c = FALSE;      
			}  
		}      
	} 
	update comm;
}

and please also set some debug and check in case this does not works. If this helps you then mark it as Solved.
Mamadou Diallo 14Mamadou Diallo 14
Hi Apoorv,

I found the issue. The criteria appt.Underwriter_Account__r.Name == c.Underwriter__c was not evaluated because one was a lookup field and the other one was a formula field (text). On the Commission, Underwriter account was coming from another custom object Plan. So I used this new criteria: appt.Underwriter_Account__c == c.Plan__r.Underwriter_Account__c.
It works perfecly.

I'm checking your last response as Best Answer. Thank you for your help.