- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
I'm am trying to populate the Collaborators field in a Demand using a script include and business rule, but it doesn't get populated and I don't know why.
For those who may not use Demands, the Collaborators field is a list of users who are collaborating with the demand. The field is defined as a glide_list; it's the same as a watch list or additional assignee field on incident or any table extended from Task.
I created a script include that uses specific criteria to get a list of users who should be collaborators. At the end of the script include, I remove the duplicate entries and return the unique array to the business rule that calls the script include and is supposed to populate the users to the collaborators field. I'm getting good data back from the script include, but the collaborators field never gets populated and I don't know why and could use some help.
Script include snippet (that returns the correct data):
Can anyone spot what is missing?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi @gjz ,
Thanks for sharing the complete code. With the additional information, I think we can narrow this down.
The behavior you saw in your PDI with the quotes is expected JavaScript behavior.
This is valid:
var userList = ['be82abf03710200044e0bfc8bcbe5d1c', 'af2a757b1b74c4500750fc87cc4bcbc1'];
The sys_ids have to be quoted because they are JavaScript strings.
This is not valid:
var userList = [be82abf03710200044e0bfc8bcbe5d1c, af2a757b1b74c4500750fc87cc4bcbc1];
Without quotes, JavaScript treats those values as variable names.
However, you do NOT need to add quotes around every sys_id before setting the Collaborators field. A Glide List field can be populated with a comma-separated string of sys_ids.
I would remove ArrayUtil and the GlideElement concatenation from this implementation and use the actual stored values from the Glide List fields.
Replace your getCollaborators method with this:
getCollaborators: function(dept) {
var collaborators = [];
var uniqueUsers = {};
if (gs.nil(dept)) {
return '';
}
var divisionGR = new GlideRecord('u_cmn_divisions');
divisionGR.addQuery('u_department', dept);
divisionGR.query();
while (divisionGR.next()) {
var liaisons = divisionGR.getValue('u_liaisons') || '';
var fundingLiaisons = divisionGR.getValue('u_funding_approvers') || '';
var users = [];
if (liaisons) {
users = users.concat(liaisons.split(','));
}
if (fundingLiaisons) {
users = users.concat(fundingLiaisons.split(','));
}
for (var i = 0; i < users.length; i++) {
var userSysId = users[i] + '';
if (!userSysId) {
continue;
}
if (!uniqueUsers[userSysId]) {
uniqueUsers[userSysId] = true;
collaborators.push(userSysId);
}
}
}
var collaboratorString = collaborators.join(',');
gs.info('@@@ getCollaborators result: ' + collaboratorString);
return collaboratorString;},
Then configure the Demand Business Rule as follows:
When: Before
Insert: Checked, if this should also populate during insert
Update: Checked
Advanced: Checked
For testing, leave the condition empty so that it runs on every update.
Most importantly, make sure this is a BEFORE Business Rule, not After or Async.
Use this exact Business Rule script:
(function executeRule(current, previous) {
var dept = current.getValue('u_department');
gs.info('@@@ Department: ' + dept);
if (gs.nil(dept)) {
current.setValue('collaborators', '');
return;
}
var ppm = new XXX_PPM_Utils();
var liaisonList = ppm.getCollaborators(dept);
gs.info('@@@ Collaborators returned: ' + liaisonList);
current.setValue('collaborators', liaisonList || '');
gs.info('@@@ Collaborators on current before save: ' + current.getValue('collaborators'));})(current, previous);
Do not use current.update() inside this Business Rule.
The important part is that the Business Rule runs Before the Demand is written to the database. The value assigned to current.collaborators will then be included in the same database update.
For example, assume your divisions contain:
Division 1
u_liaisons:
userA,userB
u_funding_approvers:
userC
Division 2
u_liaisons:
userA,userD
u_funding_approvers:
userC
The method will return:
userA,userB,userC,userD
Duplicates are removed before the value is returned.
There is also one thing I noticed in your description that I would verify.
You referred to the second field as "Funding Liaisons", but your script is querying:
u_funding_approvers
Please confirm that u_funding_approvers is really the backend field name of that Glide List field.
If the actual field is something like:
u_funding_liaisons
then change this line:
var fundingLiaisons = divisionGR.getValue('u_funding_approvers') || '';
to use the actual backend field name.
I would test this exact version first.
After updating the Demand, check the logs for:
@@@ getCollaborators result:
@@@ Collaborators returned:
@@@ Collaborators on current before save:
All three should contain the same comma-separated sys_ids.
If "@@@ Collaborators on current before save" contains the correct sys_ids but the Collaborators field is empty after the record is saved, then the Script Include is no longer the issue. That would indicate another Business Rule, Flow, or other server-side process is clearing or replacing Collaborators later in the transaction.
But with the Business Rule running Before and the correct backend field names, the above code should populate the Glide List directly without needing quotes around individual sys_ids.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Did you able to fix this?