Attach and update records automatically

Posted by leonc on 02-Nov-2018 05:07

I have three objects in the app : 

Users object,

Premises object,

Codes Object.

The Users' object connects to Premise object with a many to many relationship.

The Premise object connects to Code object with a many to many relationship.

We create Premises and we attach Codes to them.

Then we create Users and attach Premises to them.

I am trying to find a way to attach and update Codes to Users automatically.

I tried to do that with combination of triggers but with no luck so far.

Any ideas would be very much appreciated.

Thank you in advance.

Posted by mpiscoso@gmail.com on 02-Nov-2018 13:08

[mention:a98a28982f5c49b796f238b3b11db08d:e9ed411860ed4f2ba0265705b8793d05] What you did was actually incomplete. I'm assuming your trigger is an update field value trigger type and that you're trying to update the codes relationship on the user object. That trigger type expects a return statement and a lookup field expects a comma separated string of IDs as the value.

To give you an example, your end code would look somewhat like this:

var returnArr = [], //This is the return array in which we'll be keeping all the codes for the return statement

premises = rbv_api.getRelatedRecordIds("USER","R######", '{!id}'); //First, get the user's premises (Where R###### is the User:Premise relationship integration code)

for (var count in premises) { //Iterate through all related premises of the user

var codes = rbv_api.getRelatedRecordIds("Client_Premise","R461886", premises[count]); //Get the codes for each premise

for (var cCount in codes) { //Iterate through the codes of each premise

returnArr.push(codes[cCount]); //Store each code's ID in the return array

}

}

return returnArr.join(","); //After getting all of the codes of all of the premises, join() the array to make a comma separated string of codes and return to the lookup

If you swap R###### with the actual relationship code I think that should work for you.

If not, minor debugging of the code should handle the matter.

Hope that helps

Piscoso, Martin

All Replies

Posted by Ruben Dröge on 02-Nov-2018 05:49

Not quite sure what you are trying to achieve here:

- if you attach a premise to a user, the codes that were attached to the premise will still be there

- but since there is no direct relation between users and codes, you won't see those, unless you click through on the premise record

"I am trying to find a way to attach and update Codes to Users automatically."

Could you explain what you mean by this?

Posted by leonc on 02-Nov-2018 06:13

As you mentioned above, there is no direct relation between users and codes.

So, I am trying to create that relationship, so that anyone can able to see in a user list or view the codes and in a code list or view the users without having to click through the middle object, in our case the premise.

Hope that helps.

Posted by mpiscoso@gmail.com on 02-Nov-2018 06:27

[mention:a98a28982f5c49b796f238b3b11db08d:e9ed411860ed4f2ba0265705b8793d05] you need to create a many to many relationship between users and codes.

After you have that you need to create a trigger on the user object that will iterate through all your related premises using getRelatedRecordIds(). For each premise you'll need to get and store all related codes of that premise in an array then move onto the next premise.

After getting all codes of all premises, attach it to your user.

You'll need to run this after create/update when the premises of a user changes.

Hope that helps

Piscoso, Martin

Posted by leonc on 02-Nov-2018 12:56

Hi Martin,

Many to many relationship between users and codes was there already. 

I followed your suggestion and created a trigger that updates the codes in users

looks like this:

var arr = rbv_api.getRelatedRecordIds("Client_Premise","R461886", {!R462750.id});

rbv_api.printArr(arr); // added this line for debugging - prints out correctly the codes

I added a new trigger that fires every time a premise is created or updated.

But it doesn't update the codes and the reason in the Debugging info is:

No need to change Codes: formula returned null.

Posted by mpiscoso@gmail.com on 02-Nov-2018 13:08

[mention:a98a28982f5c49b796f238b3b11db08d:e9ed411860ed4f2ba0265705b8793d05] What you did was actually incomplete. I'm assuming your trigger is an update field value trigger type and that you're trying to update the codes relationship on the user object. That trigger type expects a return statement and a lookup field expects a comma separated string of IDs as the value.

To give you an example, your end code would look somewhat like this:

var returnArr = [], //This is the return array in which we'll be keeping all the codes for the return statement

premises = rbv_api.getRelatedRecordIds("USER","R######", '{!id}'); //First, get the user's premises (Where R###### is the User:Premise relationship integration code)

for (var count in premises) { //Iterate through all related premises of the user

var codes = rbv_api.getRelatedRecordIds("Client_Premise","R461886", premises[count]); //Get the codes for each premise

for (var cCount in codes) { //Iterate through the codes of each premise

returnArr.push(codes[cCount]); //Store each code's ID in the return array

}

}

return returnArr.join(","); //After getting all of the codes of all of the premises, join() the array to make a comma separated string of codes and return to the lookup

If you swap R###### with the actual relationship code I think that should work for you.

If not, minor debugging of the code should handle the matter.

Hope that helps

Piscoso, Martin

Posted by leonc on 02-Nov-2018 14:04

Thank you very much Martin.

Works great.

Posted by mpiscoso@gmail.com on 02-Nov-2018 14:07

[mention:a98a28982f5c49b796f238b3b11db08d:e9ed411860ed4f2ba0265705b8793d05] Good to know that solved your problem.

You can message me if you need any more help.

Piscoso, Martin

This thread is closed