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

Ankur Bawiskar
Tera Patron
Tera Patron

@VIKAS45 

when should this happen?

where are you planning to write script for this?

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

Hi Tushan,

 

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 the script,

 

1)In Client Script

2)In Fix Script

3)Background script

4)Business Rule

@VIKAS45 

where to write the script 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

@VIKAS45 

Thank you for marking my response as helpful.

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

Hi Ankur,

 

To set value based on condition on another table in servicenow.DO WE NEED TO WRITE SCRIPT INCLUDE? and where to write the script mentioned in the post by Tushar?