Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Update Catalog Item Available For

Fin Nguyen
Tera Guru

Hi Community,

 

I am doing an automation form that allows users to update the Available For of a catalog item. I have a list collector field (Available for?) that gets all data from the [sc_cat_item_user_criteria_mtom] table when we input an item in the Select a Catalog item question.

 

ThinhNguyen_1-1699254059572.png

I am finding a way to update the Available For list from the workflow. For example: If a user removed France and/or added a new country Korea to the list above, how can we update exactly what users add/remove and keep the existing ones?

Regards,
Fin Nguyen
1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @Fin Nguyen 

Let's try the approach below.

1. Define a function to Add new criteria case.

function addUserCriteria(cat_item, user_criteria){
    var gr = new GlideRecord('sc_cat_item_user_criteria_mtom');
    gr.addQuery('sc_cat_item', cat_item);
    gr.addQuery('user_criteria', user_criteria);
    gr.query();
    if(gr.hasNext()){
        return '';
    }
    gr.initialize();
    gr.sc_cat_item = cat_item;
    gr.user_criteria = user_criteria;
    return gr.insert(); 
}

 

2. Another function to Remove existing criteria case.

function removeUserCriteria(cat_item, arr_user_criteria){
    var gr = new GlideRecord('sc_cat_item_user_criteria_mtom');
    gr.addQuery('sc_cat_item', cat_item);
    gr.addQuery('user_criteria', 'NOT IN', arr_user_criteria);
    gr.query();
    gr.deleteMultiple();
}

 

3. Call the function above in your workflow (flow, producer).

var cat_item = current.variables.cat_item.toString(); //replace your Cat Item variable
var arrUserCriteria = current.variablers.user_criteria.split(','); //replace your Criteria variable 
for (var i in arrUserCriteria){
    addUserCriteria(cat_item, arrUserCriteria[i]);
}
removeUserCriteria(cat_item, arrUserCriteria);

 

Let me know if it works for you.

 

Cheers,

Tai Vu

View solution in original post

1 REPLY 1

Tai Vu
Kilo Patron
Kilo Patron

Hi @Fin Nguyen 

Let's try the approach below.

1. Define a function to Add new criteria case.

function addUserCriteria(cat_item, user_criteria){
    var gr = new GlideRecord('sc_cat_item_user_criteria_mtom');
    gr.addQuery('sc_cat_item', cat_item);
    gr.addQuery('user_criteria', user_criteria);
    gr.query();
    if(gr.hasNext()){
        return '';
    }
    gr.initialize();
    gr.sc_cat_item = cat_item;
    gr.user_criteria = user_criteria;
    return gr.insert(); 
}

 

2. Another function to Remove existing criteria case.

function removeUserCriteria(cat_item, arr_user_criteria){
    var gr = new GlideRecord('sc_cat_item_user_criteria_mtom');
    gr.addQuery('sc_cat_item', cat_item);
    gr.addQuery('user_criteria', 'NOT IN', arr_user_criteria);
    gr.query();
    gr.deleteMultiple();
}

 

3. Call the function above in your workflow (flow, producer).

var cat_item = current.variables.cat_item.toString(); //replace your Cat Item variable
var arrUserCriteria = current.variablers.user_criteria.split(','); //replace your Criteria variable 
for (var i in arrUserCriteria){
    addUserCriteria(cat_item, arrUserCriteria[i]);
}
removeUserCriteria(cat_item, arrUserCriteria);

 

Let me know if it works for you.

 

Cheers,

Tai Vu