Auto-populate Configuration Item field on incident form with certain conditions :
						
					
					
				
			
		
	
			
	
	
	
	
	
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2023 04:31 AM
I want to Auto-populate CI field on incident form from reference table 'cmdb_ci_pc_hardware'(this table consists of PC assigned to users) with some conditions :
1. If user has one device then auto-populate CI field .
2.if user has multiple devices assigned then clear the CI field .
3 . It should not work on old incidents which is already created .
Howvever we have tried this with script include and onchange client script but it did't work.
Please suggest.
function onChange(control, oldValue, newValue, isLoading) {
    //     if (newValue == '') {
    // 		g_form.addInfoMessage('newValue ');
    //         g_form.setVisible('cmdb_ci', true);
    //         g_form.clearValue('cmdb_ci');
    //         return;
    //     }
    var caller = g_form.getValue('caller_id');
    if (isLoading && oldValue != '') { // newValue == ''
        alert('Old value running ');
        var ref = g_form.getReference('cmdb_ci', pullAttributes);
        
//         g_form.addInfoMessage('Old Value ' + g_form.getValue('cmdb_ci'));
//         g_form.setVisible('cmdb_ci', true);
//         g_form.clearValue('cmdb_ci', false);
									}
	
		else if (isLoading && newValue != '') { //  && oldValue == ''
        alert('New Value running ');
        var ga = new GlideAjax('Inc_ci');
        ga.addParam('sysparm_name', "getCIList2");
        ga.addParam('sysparm_role', caller);
        ga.addParam('sysparm_sysID', g_form.getUniqueValue());
        alert('Caller ' + caller);
        alert('SysID  ' + g_form.getUniqueValue());
        ga.getXMLAnswer(function(answer) {
            g_form.addInfoMessage('Answer ' + answer);
            if (answer != '' && answer != 'List') { // Ci available - only 1
                g_form.setDisplay('cmdb_ci', true);
                g_form.setValue('cmdb_ci', answer);
            } else if (answer == 'List') { // Ci available - multiple
                g_form.clearValue('cmdb_ci');
                g_form.setVisible('cmdb_ci', true);
            } else if (answer == '') { // Ci avaialble - No
                g_form.clearValue('cmdb_ci');
            }
        });
    }
}
function pullAttributes(ref) {
			g_form.addInfoMessage('pullAttributes running');
            g_form.setValue('cmdb_ci', ref.cmdb_ci);
        } getCIList2: function() {
        gs.log('running CI ');
        var myDateTime = new GlideDateTime();
        myDateTime.setValue('2023-08-01 00:00:00');
        var user_id = this.getParameter('sysparm_role');
        var record = this.getParameter('sysparm_sysID');
        gs.log('running CI2');
        var gr = new GlideRecord('incident');
        gr.addQuery('sys_id', record);
        gs.log('running inc ' + record);
        gr.query();
        if (gr.next()) {
            gs.log('running gr.next() ');
            var createdDateTime = new GlideDateTime(gr.sys_created_on);
            gs.log('running ' + createdDateTime);
            if (createdDateTime.before(myDateTime))
                return 'false';
            else {
                var ci = new GlideRecord('cmdb_ci_pc_hardware');
                ci.addQuery('assigned_to', user_id);
                gs.log('running user ' + user_id);
                ci.query();
                if (ci.next()) {
                    if (ci.getRowCount() == 1) {
                        //gs.log('JT: Row count is ' + ci.getRowCount());
                        return ci.getValue('sys_id');
                    }
                    return "List";
                }
                return "";
            }
        }
    },
    type: 'Inc_ci'
});
 
					
				
		
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2023 04:58 AM
Hello @Adityanshu Sing ,
The onchange event is triggered when the user changes the value of the 'caller_id' field. it will not work initially when form gets load.
you can try below script
onLoad client script
function onLoad() {
    var caller = g_form.getValue('caller_id');
    // Check if the current record is a new incident or an existing one
    if (g_form.isNewRecord()) {
        var ga = new GlideAjax('Inc_ci');
        ga.addParam('sysparm_name', 'getCIForCaller');
        ga.addParam('sysparm_role', caller);
        ga.addParam('sysparm_sysID', g_form.getUniqueValue());
        ga.getXMLAnswer(function(answer) {
            if (answer) {
                g_form.setValue('cmdb_ci', answer);
            } else {
                g_form.clearValue('cmdb_ci');
            }
        });
    }
}Script Include :
 
    getCIForCaller: function() {
        var myDateTime = new GlideDateTime();
        myDateTime.setValue('2023-08-01 00:00:00');
        var user_id = this.getParameter('sysparm_role');
        var record = this.getParameter('sysparm_sysID');
        var gr = new GlideRecord('incident');
        gr.addQuery('sys_id', record);
        gr.query();
        if (gr.next()) {
            var createdDateTime = new GlideDateTime(gr.sys_created_on);
            if (createdDateTime.before(myDateTime)) {
                return false;
            } else {
                var ci = new GlideRecord('cmdb_ci_pc_hardware');
                ci.addQuery('assigned_to', user_id);
                ci.query();
                if (ci.getRowCount() == 1) {
                    if (ci.next()) {
                        return ci.getValue('sys_id');
                    }
                }
            }
        }
        return false;
    },Kindly mark correct and helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2023 06:33 AM
Thanks for the solution @Chetan Mahajan . Onload client script is working but I want that Ci field Auto populate when user is selected or when we change the user.
 
					
				
		
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2023 06:49 AM
Understood, same script provided by me can be work in OnChange As well have you tried it ? or if not working try to add logs and check where its breaking so i can help you accordingly.
 
					
				
		
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2023 02:43 PM
Hi @Chetan Mahajan , I am attempting to use your scripts provided above, but its not working. Any help is greatly appreciated 🙂
 
