Fix Scripts 'Not Available For' for Catalog Items

Anisse Mahtat
Tera Contributor

Hello, 

 

I have a problem to resolve and i don't know how to do that: 

 

I have to use a FIX SCRIPT that will ADD 'new hires' in all catalog items 'NOT AVAILABLE FOR' section for all catalog items who have not 'new hires' in their 'NOT AVAILABLE FOR' section

 

All my script does not work. 

 

Someone can help me please ? 

 

Thanks

2 ACCEPTED SOLUTIONS

Sainath N
Mega Sage
Mega Sage

@Anisse Mahtat : Please use the below code in your fix script. I have tested in my PDI and its working as required. 

 

var catGr = new GlideRecord("sc_cat_item");
catGr.addEncodedQuery('active=true'); // Looking for only Active catalog items
//catGr.addQuery('sys_id', 'ca21f15397527110aa7c50081153afb0');  //Uncomment this line, to test for a single catalog item before executing for all catalog items. Good for unit testing
catGr.query();
while (catGr.next()) {
    var query = 'user_criteria=0501d3801b08c0100db0fc88cc4bcb79' + '^sc_cat_item=' + catGr.sys_id.toString(); // replace '0501d3801b08c0100db0fc88cc4bcb79' with the sys_id of 'New Hires' in your case
    var gr = new GlideRecord("sc_cat_item_user_criteria_no_mtom"); // Table that stores relationship b/w catalog item and user criteria
    gr.addEncodedQuery(query);
    gr.query();
    if (!gr.next()) { // If doesn't exists
        var createRelGr = new GlideRecord("sc_cat_item_user_criteria_no_mtom");
        createRelGr.initialize();
        createRelGr.sc_cat_item = catGr.sys_id.toString();
        createRelGr.user_criteria = '0501d3801b08c0100db0fc88cc4bcb79'; // replace '0501d3801b08c0100db0fc88cc4bcb79' with the sys_id of 'New Hires' in your case
        createRelGr.insert();
    }
}

 

 Please make sure that you replace the sys_id of the user criteria and the filter condition in the query as per your requirement.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

View solution in original post

Aniket Chavan
Tera Sage
Tera Sage

Hello @Anisse Mahtat 

Please refer the script below and let me know how it works for you.

 

 

   // Define the user criteria sys_id to check and add
    var userCriteriaSysId = 'sys_id_of_your_UC'; // replace with the actual sys_id

    // Query active catalog items
    var catGr = new GlideRecord("sc_cat_item");
    catGr.addQuery('active', true);
    catGr.query();

    while (catGr.next()) {
        // Check if the relationship already exists
        var relGr = new GlideRecord("sc_cat_item_user_criteria_no_mtom");
        relGr.addQuery('user_criteria', userCriteriaSysId);
        relGr.addQuery('sc_cat_item', catGr.sys_id.toString());
        relGr.query();

        if (!relGr.next()) { // If the relationship doesn't exist, create it
            var createRelGr = new GlideRecord("sc_cat_item_user_criteria_no_mtom");
            createRelGr.initialize();
            createRelGr.sc_cat_item = catGr.sys_id.toString();
            createRelGr.user_criteria = userCriteriaSysId;
            createRelGr.insert();
        }
    }

    gs.print('Script executed successfully.');

 

 

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,
Aniket

View solution in original post

3 REPLIES 3

Sainath N
Mega Sage
Mega Sage

@Anisse Mahtat : Please use the below code in your fix script. I have tested in my PDI and its working as required. 

 

var catGr = new GlideRecord("sc_cat_item");
catGr.addEncodedQuery('active=true'); // Looking for only Active catalog items
//catGr.addQuery('sys_id', 'ca21f15397527110aa7c50081153afb0');  //Uncomment this line, to test for a single catalog item before executing for all catalog items. Good for unit testing
catGr.query();
while (catGr.next()) {
    var query = 'user_criteria=0501d3801b08c0100db0fc88cc4bcb79' + '^sc_cat_item=' + catGr.sys_id.toString(); // replace '0501d3801b08c0100db0fc88cc4bcb79' with the sys_id of 'New Hires' in your case
    var gr = new GlideRecord("sc_cat_item_user_criteria_no_mtom"); // Table that stores relationship b/w catalog item and user criteria
    gr.addEncodedQuery(query);
    gr.query();
    if (!gr.next()) { // If doesn't exists
        var createRelGr = new GlideRecord("sc_cat_item_user_criteria_no_mtom");
        createRelGr.initialize();
        createRelGr.sc_cat_item = catGr.sys_id.toString();
        createRelGr.user_criteria = '0501d3801b08c0100db0fc88cc4bcb79'; // replace '0501d3801b08c0100db0fc88cc4bcb79' with the sys_id of 'New Hires' in your case
        createRelGr.insert();
    }
}

 

 Please make sure that you replace the sys_id of the user criteria and the filter condition in the query as per your requirement.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks @Sainath N for your solution !! 😁

Aniket Chavan
Tera Sage
Tera Sage

Hello @Anisse Mahtat 

Please refer the script below and let me know how it works for you.

 

 

   // Define the user criteria sys_id to check and add
    var userCriteriaSysId = 'sys_id_of_your_UC'; // replace with the actual sys_id

    // Query active catalog items
    var catGr = new GlideRecord("sc_cat_item");
    catGr.addQuery('active', true);
    catGr.query();

    while (catGr.next()) {
        // Check if the relationship already exists
        var relGr = new GlideRecord("sc_cat_item_user_criteria_no_mtom");
        relGr.addQuery('user_criteria', userCriteriaSysId);
        relGr.addQuery('sc_cat_item', catGr.sys_id.toString());
        relGr.query();

        if (!relGr.next()) { // If the relationship doesn't exist, create it
            var createRelGr = new GlideRecord("sc_cat_item_user_criteria_no_mtom");
            createRelGr.initialize();
            createRelGr.sc_cat_item = catGr.sys_id.toString();
            createRelGr.user_criteria = userCriteriaSysId;
            createRelGr.insert();
        }
    }

    gs.print('Script executed successfully.');

 

 

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,
Aniket