Identify changes in a Many-To-Many relationship

Posted by smartsysISV on 17-Jul-2014 06:56

How can I (In a trigger) find the changes done in a many-to-many relationship.

I want a trigger to identify wich id's is released or attached to this relationship.

I did not get any values when trying to use #before....?

All Replies

Posted by Mani Kumar on 21-Jul-2014 08:44

Hi Stein,

You can try using rbv_api.getRelatedIds(). This returns an array of related IDs for a specified relationship and object ID.

Syntax

rbv_api.getRelatedIds (relName, id)

Example

var arr = rbv_api.getRelatedIds("R1321", {!id});

for (var k=0; k<arr.length; k++) {

var id = arr[k];

rbv_api.println("id="+id);

}

Hope this helps.

Regards,

Mani.

Posted by Gian Torralba on 21-Jul-2014 11:42

Hi Stein,

You cannot specifically use [tag:before] on many to many relationship as well as using the [tag:id] to return all ID's because it will only return a single ID. As Mani suggested, you can use the rbv_api.getRelatedIds() to return all ID's as a javascript array.

What you can do is create a hidden lookup 1-Many to hold the previous ID's, create a trigger that will set the new value on the hidden lookup and then a trigger to compare which ID's are added or removed. Please follow the trigger hierarchy.

  1. Compare ID's Trigger After Update
  2. Update hidden lookup value (Update Field Value Trigger) After Update

For trigger 1, you can check this sample code:

var newIds = rbv_api.getRelatedIds("relName", parseInt("{!id}")); //main lookup

var oldIds = rbv_api.getRelatedIds("relName", parseInt("{!id}")); //hidden lookup

//Add your own javascript function to compare both arrays and remove the duplicates.

For trigger 2,

var ids = rbv_api.getRelatedIds("relName", parseInt("{!id}")); //relname of main lookup

return ids.join(",");

This will update the value of the hidden lookup field so it can be compared to the main lookup when the first trigger executes.

Hope this helps.

Thanks,

Gian

This thread is closed