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
dkndkn 

after update trigger help

Hi I have an issue with the after update , I have an application object and recommender object.I have an custom field as recommenders email address in application, once a student enters the email address and trigger should fire and create a corresponding recommender object record , it works fine for the first time.But if I make any changes in the recommender's email address it doesn't change that in the corresponding recommender object record.

 

Only after insert will not work in my scenario, because an application record it created once an contact record is created .So, i need to update the application record.

 

The problem is I am checking for if the recommender record is empty or not with recs.size==0, if it is not empty it recognizing it , but not updating....Please...help....me...

 

 

Trigger Rectrigger on EnrollmentrxRx__Enrollment_Opportunity__c (after insert,after update)
{
  Set<Id> appids= New Set<Id>();
  for(EnrollmentrxRx__Enrollment_Opportunity__c appli : Trigger.New)
  {
      appids.add(appli.Id);
  }
 
  List<TestErx__Recommender__c> recs=[select TestErx__Application_ID__c from TestErx__Recommender__c where TestErx__Application_ID__c in : appids];
  System.Debug('Size=='+recs.size());
  if(recs.size()==0)
  {
      for (EnrollmentrxRx__Enrollment_Opportunity__c newApplication: Trigger.New)
      {
          if (newApplication.TestErx__Recommender_s_email_address__c != null)
          {
              recs.add(new TestErx__Recommender__c(
              
              TestErx__Application_ID__c = newApplication.Id,
              
              TestErx__Recommender_E_mail_Address__c=newApplication.TestErx__Recommender_s_email_address__c,
              
              TestErx__First_name__c=newApplication.TestErx__Recommender_s_first_name__c,
              
              TestErx__Surname__c=newApplication.TestErx__Recommender_s_last_name__c));
          }
      }
  }
  upsert recs;
 

ahab1372ahab1372

after the first time, there is a TestErx__Recommender__c in the database, which means that recs.size() is not 0 anymore, and your code is not executed. 

You probably need to store the TestErx__Recommender__c records in a map with the TestErx__Application_ID__c as the key. Then when you loop through the trigger.New for the second time, you can check if the map contains a corresponding key/record, and then update it if it exists and create a new one if it does not exist.