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

@VIKAS45

 

See below, change the 'owned_by' to the name of your custom field. You can remove the 'alert()' line I used to debug.

 

Screenshot 2023-09-21 093814.png

Bert_c1
Kilo Patron

@VIKAS45 

 

the above will work if your custom Username field is defined as a Reference field to the sys_user table. If not, but will match a record in the sys_user table 'name' field, then the client script changes to use GlideAjax, and a script include with logic to search the sys_user table based on the 'name' field matching the custom Username value will be needed. It will return the sys_id value of the record that the client script will then set the 'assigned_to' value on the cmdb_ci_computer table.  The solution here depends on what is contained in the custom username field.

Riya Verma
Kilo Sage
Kilo Sage

Hi @VIKAS45 ,

 

Hope you are doing great.

 

To set the "Assigned To" field in the CMDB_CI_Computer table based on the username mentioned in a custom field:

  1. Create a Business Rule or Script in ServiceNow.

  2. Use a server-side script to retrieve the username from the custom field.

  3. Query the User table to find the corresponding User record based on the username.

  4. Set the "Assigned To" field in the CMDB_CI_Computer record to the User record you found.

Below is reference script for same :

(function() {
  // Get the current CMDB_CI_Computer record
  var computerRecord = new GlideRecord('CMDB_CI_Computer');
  computerRecord.get(current);
  var username = computerRecord.getValue('custom_username');

  // Query the User table to find the User record
  var userRecord = new GlideRecord('sys_user');
  if (userRecord.get('user_name', username)) {
    // Set the 'Assigned To' field to the User record
    computerRecord.setValue('assigned_to', userRecord.sys_id);
  }

  computerRecord.update();
})();
 
 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma