m2m Relationship - populate variable on 'child' record

alexcharleswort
Tera Expert

Hi!

I have set up a m2m relationship with two tables and it works like its supposed to, but I would like to populate a variable based on which record the 'child' has been attached to. For example, lets say I have a table called u_colors and my second table is called u_shirts. On my u_color table I have record 'Red', 'Orange', 'Blue', 'Pink' and 'Black'. On my u_shirts table I have record 'TShirt', 'Polo', 'VNeck'. I want to work off of my shirt table and assign available colors to each type of shirt.

For TShirt I have added Red and Blue

For Polo I have added Red, Pink and Orange

For VNeck I have added Pink and Black

What I want to have happen is whenever a color is added to the TShirt record, I want 'u_tshirt_option' (which is a Boolean on each of the u_color records) to be marked as true. I want the same for every color that is added to the Polo record on instead I want it to mark "u_polo_option" as true on the Red, Pink and Orange Records.

In the end, Red would have u_polo_option and u_tshirt_option marked as true, Pink would have u_polo_option and u_vneck_option marked as true, and Black would have just u_vneck_option marked as true.

Hopefully this makes sense.

Any help would be greatly appreciated.

1 ACCEPTED SOLUTION

michael_baker
Tera Guru

Hi Alex,



I would suggest you create an after Business Rule on the newly created many to many table that updates the associated u_color record on insert/update/delete.



Hope this helps!


View solution in original post

5 REPLIES 5

michael_baker
Tera Guru

Hi Alex,



I would suggest you create an after Business Rule on the newly created many to many table that updates the associated u_color record on insert/update/delete.



Hope this helps!


ah okay I see what you're saying. Any way you might be able to help me with this business rule a bit??



I am making a business rule on my m2m table on insert and update. I've tried with both 'before' and 'after' and I couldn't get it to quite work. i'm not sure if its because the colors is still a related list in this table??



This is a record on the m2m table:


find_real_file.png


This is the business rule script:



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


// Add your code here
//if shirt style is vneck
if (current.getValue('u_shirts') == '13bfcc4c0fd80740ac50305be1050e34'){
//set u_vneck to true on color record
  current.setValue('u_colors.u_vneck', true);
}


})(current, previous);



Any idea what isn't working?


I would try replacing the below line:


current.setValue('u_colors.u_vneck', true);



With these:


var grColor = new GlideRecord('u_color');


grColor.addQuery('sys_id', current.getValue('u_colors'));


if (grColor.next()) {


  grColor.u_vneck = 'true';


  grColor.update();


}



I do not think you can update a reference field's record through dot-walking.


you are correct, looks like you can't dot walk on an m2m table from what I have found. And that worked wonderfully. Thank you for your help!!