Populate the watch list field from a variable that is on every catalog item

rachelconstanti
Mega Sage

I have a variable set with a field of "requested_for" on every catalog item.  I need that field to be copied to the watch list so that the requested for can receive email notifications.

I've seen some articles on this subject but none really seem to answer the question.

I know that I can do this through a business rule - I need help with what I need to do for a script.  Any help is appreciated.

~Rachel

 

1 ACCEPTED SOLUTION

Probably the easiest way would be to create an onBefore Business rule on the Requested Item table that will take the value from the Requested for [requested_for] variable and insert into the Watch list [watch_list] listing. Since the Watch list field is an array of sys_id values, you will need to push the value from your variable (which I am guessing is a reference field) into the Watch list field. Here are the details for the Business Rule:

Name: Set Watch list from Requested for
Table: Requested Item [sc_req_item]
Advanced: true
Description: Copies the Requested for variable into the Watch list field
When: before
Order: 100
Insert: true
Update: false
Delete: false
Query: false
Script:

(function executeRule(current, previous /*null when async*/) {
    var reqFor = current.variables.requested_for;
    var wlArray = [];
    if (reqFor != '') {
        wlArray.push(reqFor.toString());
        current.watch_list = wlArray.toString();
    }
})(current, previous);

Let us know if this works for you.

View solution in original post

8 REPLIES 8

1.  The requested_for variable is a reference type variable

2. It is to be copied to the requested item table - sc_req_item

 

Thank you!

 

Probably the easiest way would be to create an onBefore Business rule on the Requested Item table that will take the value from the Requested for [requested_for] variable and insert into the Watch list [watch_list] listing. Since the Watch list field is an array of sys_id values, you will need to push the value from your variable (which I am guessing is a reference field) into the Watch list field. Here are the details for the Business Rule:

Name: Set Watch list from Requested for
Table: Requested Item [sc_req_item]
Advanced: true
Description: Copies the Requested for variable into the Watch list field
When: before
Order: 100
Insert: true
Update: false
Delete: false
Query: false
Script:

(function executeRule(current, previous /*null when async*/) {
    var reqFor = current.variables.requested_for;
    var wlArray = [];
    if (reqFor != '') {
        wlArray.push(reqFor.toString());
        current.watch_list = wlArray.toString();
    }
})(current, previous);

Let us know if this works for you.

I did a variation on this, because I had a list of email addresses in a text field (semi-colon separated) and needed to parse them into the watch_list. It also works great for an email List Collector, so bonus win. Couldn't have done it without your example, so thanks!

(function executeRule(current, previous /*null when async*/) {
    var inputStr = current.variables.u_lookup_email.toString();
    var outputStr = inputStr.split(';');

    var strArr = [];
    for (var i = 0; i < outputStr.length; i++) {
        strArr.push(outputStr[i]);
        current.watch_list = strArr.toString();
    }
})(current, previous);

rachelconstanti
Mega Sage

ccajohnson - THANK YOU for the quick response and the solution.

This works like a charm and completely solves the issue at hand.