Business Rule Update Related List - User Reference Field

michaelcory
Giga Expert

I have two custom Tables (u_training_class) & (u_attendee).   Attendee is a Related List on the Training Class table.  

I have a reference field (to sys_user) (u_trainer) on Training Class table where the Trainer will add their name.

I have a reference field (to sys_user) (u_trainer) on Attendee table that needs to get the Trainer value entered on the Classes table.

NOTE:   I can't show the u_training_class.u_trainer on the Attendee table because the required Survey cannot access the related field, so I need to write the value from Class to Attendee using two separate fields (u_trainer) reference fields.

I have an Insert BR that writes the value to the Attendee record when a a new Attendee is added and the Trainer name already exists on the Class table.   That's working fine.

But my update Business Rule is not working if the Trainer name is changed on the Training Class table or if a Trainer name is added to the Training Class table and related Attendee records already exist. In either case, I need to update all of the Attendee records to the added or changed Trainer name entered on the Training Class table.   I have the following BR.

BR added to u_attendee table

When = after

Insert = true, Update = true

(function executeRule(current, previous /*null when async*/) {

  var gr = new GlideRecord('u_training_class');

  gr.addQuery('u_trainer', current.getValue());

  gr.query();

  while (gr.next()){

  gr.u_trainer = current.u_trainer;

  gr.update();

  }


})(current, previous);

1 ACCEPTED SOLUTION

dvp
Mega Sage
Mega Sage

The business rule should be on u_training_class table as data is getting updated on class not on attendee table



Below is the script



please update the Training class field that is on attendee table



var gr = new GlideRecord('u_attendee');


  gr.addQuery('u_training_class_reference_field', current.sys_id);


  gr.query();


  while (gr.next()){


  gr.u_trainer = current.u_trainer;


  gr.update();


  }


View solution in original post

5 REPLIES 5

randrews
Tera Guru

ok so as I understand your issue when you UPDATE a trainer... the individual atendee records are not update, and on the attendee records you have TWO reference fields for the trainer <one that the surveys CAN see and one they can not see>



so i would ask is the one the surveys CAN NOT see updating and just not the other.. or are both not updating??



if the one they can see is not updating but the other is.. a simple change br on the attendee table is required...



if NEITHER is updating you are going to need to add a script inside your while loop that checks the attendee table for all records for that class and updates them.


dvp
Mega Sage
Mega Sage

The business rule should be on u_training_class table as data is getting updated on class not on attendee table



Below is the script



please update the Training class field that is on attendee table



var gr = new GlideRecord('u_attendee');


  gr.addQuery('u_training_class_reference_field', current.sys_id);


  gr.query();


  while (gr.next()){


  gr.u_trainer = current.u_trainer;


  gr.update();


  }


ok i am a little confused... in your question you stated...



I need to update all of the Attendee records to the added or changed Trainer name entered on the Training Class table.   I have the following BR.



now that implies you need to update the attendee records.. but in your follow up you say the data is getting updated on the class table.. so which records are not being updated correctly.. the class or the class field on the attendee table?


I had the logic twisted Raymond.   DVP set me straight with the Correct Answer. I simply need to update the Attendee related records when a Trainer name is added or changed on the Training Class table.   Sorry for the confusion.