How to set Assigned to in ALM Hardware to "Last logged on User" if "Primary User" is same?

VIKAS45
Tera Guru

 

How to assign assets in Servicenow HAM to "Last logged on User" if "Primary User" is same as?

 

How to set Assigned to in 'alm_hardware' Table in Servicenow HAM to "Last logged on User" of cmdb_ci_computer table  if "Primary User" is same as "Last logged on User"  of 'cmdb_ci_computer' 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

12 REPLIES 12

Tushar
Kilo Sage
Kilo Sage

Hi Vikas,

 

something like this can be useful?

 

 

(function executeRule(current, previous) {
  // Get the 'cmdb_ci_computer' record for the hardware asset
  var computerRecord = current.cmdb_ci_computer.getRefRecord();

  if (computerRecord) {
    var primaryUser = computerRecord.primary_user.toString();
    var lastLoggedOnUser = computerRecord.last_logged_on.toString();

    // Check if 'Primary User' is the same as 'Last logged on User'
    if (primaryUser === lastLoggedOnUser) {
      // Set the 'Assigned to' field in the 'alm_hardware' table to 'Last logged on User'
      current.assigned_to = computerRecord.last_logged_on;
    }
  }
})(current, previous);

 

 

 

Mark as correct and helpful if it solved your query.

Regards,
Tushar

Hi Tushar,

 

We have to check condition in cmdb_ci_computer as you mentioned and set value in alm_hardware table.

Please confirm where should I write this code.

 

1)In Client Script

2)In Fix Script

3)Background script

4)Business Rule

Hi Tushar it is not working. 

 

please let me know to use the below script in Client Script instead of Business Rules.

 

How to set Assigned to in 'alm_hardware' Table in Servicenow HAM to "Last logged on User" of cmdb_ci_computer table  if "Primary User" is same as "Last logged on User"  of 'cmdb_ci_computer' Table?

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');

    }



}