The CreatorCon Call for Content is officially open! Get started here.

How to populate an embedded list with a script? (advanced scripting needed)

Smith Johnson
Tera Guru

Hello all,

I would like to know how can I populate an embedded list. For example, in the following image, all fields of the form (request form) can be populated with a mapping between user's selections and table's fields. However, there is no any option for making a mapping between a record producer's option and a column of an embedded list.

In my case, I want once a user selects an item on Service Portal, this item is populated and appears on the embedded list of the record.

find_real_file.png

I guess it needs advanced scripting techniques, but since I don't have them your assistance would be incredibly valuable for me.

Thank you in advance for your time and your willingness to help me.

Best regards,

Smith.

1 ACCEPTED SOLUTION

To use the cart functionality - test with one catalog item you can pick from the record producer.

1. Add a new variable to the catalog item

  • Type: Reference
  • Table: sc_request

2. Add a UI Policy to hide the variable on load

3. In the BR you have in the REQ table, replace the code with this: 

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem(current.u_cat_item); 
cart.setVarible(item, 'request', current.sys_id);
var rc = cart.placeOrder(); 

View solution in original post

22 REPLIES 22

Hi Smith, 

I am curious to know why you are using a record producer to create an REQ and not use a catalog item to create the RITM directly. Is there a specific requirement for that? I think it would be easier to create a catalog item for the RITM and then you'll see the values that the user entered in the RITM without the need to add a business rule for that. 

I see in your screen shot of the record producer, the user would be selecting an existing RITM? and you want that to show in the REQ's embedded list? If so, this is going to be a bit confusing. When an RITM is created, it already has an REQ associated to it by default... so by selecting an existing one and creating a new REQ every time, you're going to be removing assocaition from those exising REQs that were previously associated to the RITM. 

If that's not the case, I think you're better off with a catalog item instead of a record producer.

 

Yeny

 

@YenGar Hey Yeny, I don't want to use the normal ordering of catalog items.

So, to be more precise, I will try to describe my case.

I have 3 categories "Hardware","Software","Network". Each category has over 100 catalog items. Each catalog item has only a name (for example: Windows10) and not an image, a price, etc. So, this means that the user wo enters on "Software" category, will see over than 100 items with no image (so, 100 boxes). It is really confusing for the end user.

That's why, my logic is the following:

These 3 categories, described above, will be record producers which create records on sc_request table. When the user enters on the "Software" record producer, he/she will see a list that refers to catalog items table. So, its more intuitive for a user to see a list with catalog items rather than 100 boxes, described above.

So, once the user selects an item and submits the form, a request is created. I can get the user's selection, but what I can not do, is to populate the embedded list (requested item) of the created request. If I do so, then I create again a relationship between a request and a catalog item.

Please let me know, if you can understand my point of view and my requirement.

@YenGar 

So, looking at the following image, it is one record on sc_request table. How can I refer to the fields, marked with a red circle on the image, within a script? 

find_real_file.png

I see your point, although going this route may be a bit tricky when it comes to handling the workflow for those items the user is selecting, specially if at some point you'll have them select more than one item. 

I'd suggest you create a custom reference field to pass the catalog item selected by the user and then use that in the business rule. Let's say you call the field Cat Item (u_cat_item). 

In the record producer script, add the following:
current.u_cat_item = producer.<fieldname where user makes selection>;

this will pass the selection to the REQ record.

Then create an After Insert business rule

var cat = current.u_cat_item; //catalog item selected 
var ritm = new GlideRecord('sc_req_item');
ritm.initialize();
ritm.request = current.sys_id; //associate the REQ to the RITM
ritm.quantity = '1';
ritm.item = cat; //populate Item with the user's selection
ritm.insert();
gs.log("RITM Inserted: " + ritm);

This will insert a new RITM and relate it to the REQ.

If you have a workflow attached to the catalog item that the user is selecting though, you'll have to make sure it's triggered. Maybe add a line after the insert to kick off the workflow like ritm.setWorkflow(true);

However, I suggest you kicking of the catalog item in a different way using the cart. Add a new variable to the catalog items with reference to the sc_request table to associate the REQ.

Then add this to the After Insert business rule:

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('u_cat_item'); 
cart.setVariable(item, 'requested_for', current.opened_by);
cart.setVarible(item, 'request', current.sys_id);
var rc = cart.placeOrder(); 
gs.addInfoMessage(rc.number);

I haven't tested any of these but just off the top of my head what you can start testing with.

Yeny

@YenGar Yeny,

thank you so so so much for your answer.

I will try it tomorrow and I will come back with an update! Again, thank you s much for your willingness to help me and your kindness to provide me a block of code.

Regards,

Smith.