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

Hi @VIKAS45 ,

 

I think you should create a BR -

 

 

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

 

 

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

Hi Tushar it is not working. 

 

please let me know 

 

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?

 

@VIKAS45 

I already informed it depends on your use-case

1)In Client Script - if you want to see the real-time change on UI

2)In Fix Script - this is like a 1 time activity and you want to update all the records

3)Background script - not preferred

4)Business Rule - when you want to see the change once record is saved

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader