Create RITMs from script Scheduled job using CART

Adi Hirsh Bibi
Tera Contributor

Hello Gurus,
I have a customed table, let's call it T7, a scheduled job script (for debug purposes, in the meanwhile, I'm using 'execute now' to run it) where I scripted a GlideRecord query on that table. The RowCount might return more then 1 row.

I've created in advance a catalogue item, attached it to a workflow (only approval )which is connected to the sc_req_item table.

The goal is to create a Ritm for every record that answers the query thereby initiating my workflow. The scheduled job script goes like this:
var gr = new GlideRecord('T7');

gr.addQuery('My_query');

gr.query();
while (gr.next()){
var cartId = glideGuid.generate(null);

var cart = new Cart(cartId, gs.getUserID());

var item = cart.addItem('sys_id of my catalog item');

cart.setVariable(item,"variable1 name in my catalog item", gr.value1);

cart.setVariable(item,"variable2 name in my catalog item", gr.value);

.

.

etc. I fill all my variables and necessary fields to trigger my workflow

cart.placeOrder();

}

The problematic behavior is that it works only once, it creates only 1 Ritm even though I have more than 1 record (I've written to gs.log the Rowcount). It doesn't create more then one cart. It creates only 1 ritm .
We're on the Tokyo version. I'm prevented from attaching any files.

Help, anybody?

Thanks in advance

AH.

3 REPLIES 3

bradleydebono
Mega Guru

Hi AH, 

 

This might not be exactly what you're after but you can create RITMs programmatically via the CartJS API. The code would look something similar to the below...

var gr = new GlideRecord("<whichever table you're querying on>");
gr.addQuery(<your query in here>);
gr.query();

while (gr.next()) {
    var cart = new sn_sc.CartJS();
    var item =
    {
        "sysparm_id": "<the sys_id of the catalog item>",
        "sysparm_quantity": "1",
        "variables":{
            "<variable_name_1": "<the value you want to give it>",
            "<variable_name_2": "<the value you want to give it>",
            .
            . 
        }
    }
    cart.orderNow(item);
}

If this was helpful / solved your query. Please mark it as helpful / as the solution. It really helps me!

 

Adi Hirsh Bibi
Tera Contributor

Hi Bradley,

Thank you for answering!

Unfortunately, I didn't help. The behavior is the same. Only one RITM with one Request were created , not 3, for example as expected. I wonder,  hasn't anybody encountered this issue..? I thought this automation might be a common one.

I'd appreciate any ideas.

AH

Adi Hirsh Bibi
Tera Contributor

Hi again,

Continuing asking on this subject, I've made some progress: I have now succeeded in triggering as many Ritms as I get from my T7 table. I've changed a bit the script:
Instead of a 'while' loop I use 'For' loop like this:
for (i=0; i<rc ; i++){

while (gr.next()){
var cartId = glideGuid.generate(null);

var cart = new Cart(cartId);

var item = cart.addItem('sys_id of my catalog item');

cart.setVariable(item,"variable1 name in my catalog item", gr.value1);

cart.setVariable(item,"variable2 name in my catalog item", gr.value);

.

.

etc. I fill all my variables and necessary fields to trigger my workflow

cart.placeOrder();

gr.next();

}

Now I see in the log that several Ritms are created in the sc_req_item table but only one of them has actually started the workflow associated with my catalog item. The rest of the Ritms were empty since they did not get the gr.value for the relevant variable and, therefore didn't trigger any workflow. One of the fields I'm passing to my ritm from my T7 table is of type reference from the sys_user table (It's inserted to a variable 'u_requested_for' in my catalog item).

I see that it takes just one value and smears it to all 'requested for' variable in all the ritm records created.

Please, Can anybody point out what am I missing? it should take the [i] values from the gr record but it take just one.

Thanks a lot in advance.

AH