How to set Assigned To field in CMDB_CI_Computer based on Username mentioned in Custom field of same

VIKAS45
Tera Guru

How to set "Assigned To" field in CMDB_CI_Computer based on Username mentioned in Custom field of same Table?

1 ACCEPTED SOLUTION

Issue Resolved.

 

Please find below Code:-

 

Script Include:-

var TESTCMDBCGC = Class.create();
TESTCMDBCGC.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    testfunction: function() {
        var userName = this.getParameter("sysparm_user_name");
        var grUser = new GlideRecord('sys_user');
        grUser.get("user_name", userName);
       
        // Build the payload. You can return additional data if needed.
       gs.addInfoMessage('Script Include Message' + userName);
        return grUser.sys_id.toString() ;
    },


type: 'TESTCMDBCGC'
});
 
Client Script:-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }


    g_form.addInfoMessage('Test Log');

    var sysUserid1 = g_form.getValue('u_primary_user');

    g_form.addInfoMessage('sysUserid1');

    var sysUserid2 = g_form.getValue('u_logged_on_user');



    if (sysUserid1 == sysUserid2) {
        g_form.addInfoMessage('Inside If');
        var ga = new GlideAjax('TESTCMDBCGC'); // GetUserInfo is the script include name
        ga.addParam('sysparm_name', 'testfunction'); // managerName is the function in the script include that we're calling
        ga.addParam('sysparm_user_name', sysUserid1); // set user to Fred Luddy

        /* Call GetUserInfo.managerName() with user set to Fred Luddy and use the callback function ManagerParse() to return the result when ready */
        ga.getXMLAnswer(ManagerParse);
    }

    // callback function for returning the result from the script include
    function ManagerParse(response) {
        alert(response);
        g_form.addInfoMessage('Before Assigned To');

        g_form.setValue('assigned_to', response);

        g_form.addInfoMessage('After Assigned To');

    }



}

View solution in original post

7 REPLIES 7

Bert_c1
Kilo Patron

Hi @VIKAS45,

 

You can use a client script (runs on the form), a business rule (runs when a record is saved). Maybe other means. Depends on the desired user experience.

please let me know the script used in Client script and script include to set "Assigned To" field in CMDB_CI_Computer based on Username mentioned in Custom field of same Table.

Hi vikas45,

 

See examples in your instance. I may try creating both for you when I have time. However, I do not have a 'Username' custom field on that table. I do have other OOB fields defined a Reference field to sys_user table that I may provide an example using the 'owned_by' field.

Issue Resolved.

 

Please find below Code:-

 

Script Include:-

var TESTCMDBCGC = Class.create();
TESTCMDBCGC.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    testfunction: function() {
        var userName = this.getParameter("sysparm_user_name");
        var grUser = new GlideRecord('sys_user');
        grUser.get("user_name", userName);
       
        // Build the payload. You can return additional data if needed.
       gs.addInfoMessage('Script Include Message' + userName);
        return grUser.sys_id.toString() ;
    },


type: 'TESTCMDBCGC'
});
 
Client Script:-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }


    g_form.addInfoMessage('Test Log');

    var sysUserid1 = g_form.getValue('u_primary_user');

    g_form.addInfoMessage('sysUserid1');

    var sysUserid2 = g_form.getValue('u_logged_on_user');



    if (sysUserid1 == sysUserid2) {
        g_form.addInfoMessage('Inside If');
        var ga = new GlideAjax('TESTCMDBCGC'); // GetUserInfo is the script include name
        ga.addParam('sysparm_name', 'testfunction'); // managerName is the function in the script include that we're calling
        ga.addParam('sysparm_user_name', sysUserid1); // set user to Fred Luddy

        /* Call GetUserInfo.managerName() with user set to Fred Luddy and use the callback function ManagerParse() to return the result when ready */
        ga.getXMLAnswer(ManagerParse);
    }

    // callback function for returning the result from the script include
    function ManagerParse(response) {
        alert(response);
        g_form.addInfoMessage('Before Assigned To');

        g_form.setValue('assigned_to', response);

        g_form.addInfoMessage('After Assigned To');

    }



}