- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2023 06:18 PM
Requirement is, we have a list collector variable referencing User table. When the catalog is submitted it should create a seperate RITM for each user selected in the list collector as requested for.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 12:07 AM
You can create an onBefore - Insert business rule on sc_req_item table like the one below to achieve this. I tried to create a separate RITM based on the users present in the List Collector variable and make the current RITM as parent for the individual.
(function executeRule(current, previous /*null when async*/ ) {
    var users = current.variables.req_for_users + ''; //Change variable name to your List collector variable name
    users = users.split(',');
    var parent_variables = current.variables;
    for (var i = 0; i < users.length; i++) {
        var ritmGr = new GlideRecord("sc_req_item");
        ritmGr.initialize();
        for (key2 in current) {
            if (key2 != 'number' && key2 != 'sys_id') {
                ritmGr[key2] = current[key2];
            }
        }
        ritmGr.setValue('requested_for', users[i]);
        ritmGr.setValue('parent', current.getUniqueValue());
        var ritm_sys_id = ritmGr.insert();
		
		copyVariables(ritm_sys_id, current.getValue('cat_item'), parent_variables);
    }
    function copyVariables(ritm, cat_item, variables) {
        var grCatItemVar = new GlideRecord("item_option_new");
        grCatItemVar.addQuery("cat_item", cat_item);
        grCatItemVar.query();
        while (grCatItemVar.next()) {
            var grRitmVar = new GlideRecord("sc_item_option");
            grRitmVar.initialize();
            grRitmVar.item_option_new = grCatItemVar.sys_id + "";
            grRitmVar.order =grCatItemVar.order + "";
            if (variables[grCatItemVar.name]) {
                grRitmVar.value = variables[grCatItemVar.name];
            } else {
                grRitmVar.value = grCatItemVar.default_value + "";
            }
            grRitmVar.insert();
            var grRitmVarM2M = new GlideRecord("sc_item_option_mtom");
            grRitmVarM2M.initialize();
            grRitmVarM2M.request_item = ritm;
            grRitmVarM2M.sc_item_option = grRitmVar.sys_id + "";
            grRitmVarM2M.insert();
        }
    }
})(current, previous);
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 12:07 AM
You can create an onBefore - Insert business rule on sc_req_item table like the one below to achieve this. I tried to create a separate RITM based on the users present in the List Collector variable and make the current RITM as parent for the individual.
(function executeRule(current, previous /*null when async*/ ) {
    var users = current.variables.req_for_users + ''; //Change variable name to your List collector variable name
    users = users.split(',');
    var parent_variables = current.variables;
    for (var i = 0; i < users.length; i++) {
        var ritmGr = new GlideRecord("sc_req_item");
        ritmGr.initialize();
        for (key2 in current) {
            if (key2 != 'number' && key2 != 'sys_id') {
                ritmGr[key2] = current[key2];
            }
        }
        ritmGr.setValue('requested_for', users[i]);
        ritmGr.setValue('parent', current.getUniqueValue());
        var ritm_sys_id = ritmGr.insert();
		
		copyVariables(ritm_sys_id, current.getValue('cat_item'), parent_variables);
    }
    function copyVariables(ritm, cat_item, variables) {
        var grCatItemVar = new GlideRecord("item_option_new");
        grCatItemVar.addQuery("cat_item", cat_item);
        grCatItemVar.query();
        while (grCatItemVar.next()) {
            var grRitmVar = new GlideRecord("sc_item_option");
            grRitmVar.initialize();
            grRitmVar.item_option_new = grCatItemVar.sys_id + "";
            grRitmVar.order =grCatItemVar.order + "";
            if (variables[grCatItemVar.name]) {
                grRitmVar.value = variables[grCatItemVar.name];
            } else {
                grRitmVar.value = grCatItemVar.default_value + "";
            }
            grRitmVar.insert();
            var grRitmVarM2M = new GlideRecord("sc_item_option_mtom");
            grRitmVarM2M.initialize();
            grRitmVarM2M.request_item = ritm;
            grRitmVarM2M.sc_item_option = grRitmVar.sys_id + "";
            grRitmVarM2M.insert();
        }
    }
})(current, previous);
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 03:37 AM
Thanks Anvesh!
I'll try this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 04:19 AM
@AnveshKumar M Thanks it worked for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 03:41 AM
you can try to use Flow designer for this and use Submit Catalog Item request
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
