Automatically populate logged in user install by field

bharathkumar Da
Tera Contributor

Hi Team
I am working on Hardware Asset Management (HAM) in ServiceNow.

Requirement:

When the State field in the alm_hardware table is changed to “In Use”, the custom field u_installed_by should be automatically populated logged in user

Thanks and Regards
Bharath

1 ACCEPTED SOLUTION

SIVASANKARIS
Giga Guru

Hi @bharathkumar Da ,

You can use Business Rule to achieve this requirement.., Because business rule will work even if the record is edited in the List View also

 

Business Rule Configuration

  1. Navigate to System Definition > Business Rules.

  2. Click New and configure the following:

    • Name: Populate Installed By on In Use (Server-side)

    • Table: Hardware [alm_hardware]

    • Advanced: Check this box.

    • When: before

    • Insert: Checked

    • Update: Checked


Conditions Tab

To ensure this only runs when necessary, set the following Filter Conditions:

  • State | changes to | In Use


Advanced Scripting

On the Advanced tab, use the following code. Using gs.getUserID() is the server-side equivalent of fetching the currently logged-in user.

JavaScript
 
(function executeRule(current, previous /*null when async*/) {

    // gs.getUserID() returns the sys_id of the logged in user
    current.u_installed_by = gs.getUserID();

})(current, previous);

How it works in different scenarios:

  • Manual Update: If you (as an admin or technician) change the state on the form and click "Save," gs.getUserID() will return your Sys ID.

  • List Edit: If someone changes the state directly from the list view, it will return the Sys ID of the person who made that list edit.

  • Data Imports/Integrations: If a record is updated via an Integration (like SCCM or a REST API), it will return the Sys ID of the Service Account used for that integration.

  • Scheduled Jobs: If a background script or scheduled job triggers the update, it may return the "System" user ID, unless a specific user session is impersonated.

 

If this works, please mark as helpful and please accept my solution.....

 

Best Regards

SIVASANKARI S

View solution in original post

4 REPLIES 4

yashkamde
Tera Guru

Hello @bharathkumar Da ,

Use on change client script giving your condition

if(state == 'in_use'){

var ga = ne GlideAjax("Glide ajax name");

ga.addParam("sysparm_name", "function name");

ga.getXMLAnswer(demo);

 

function demo(response){

g_form.setValue("u_installed_by", response);

}

}

and then use script include:
function_name : function(){

return gs.getUserName();

}

If my response helped mark as helpful and accept the solution..

SIVASANKARIS
Giga Guru

Hi @bharathkumar Da ,

You can use Business Rule to achieve this requirement.., Because business rule will work even if the record is edited in the List View also

 

Business Rule Configuration

  1. Navigate to System Definition > Business Rules.

  2. Click New and configure the following:

    • Name: Populate Installed By on In Use (Server-side)

    • Table: Hardware [alm_hardware]

    • Advanced: Check this box.

    • When: before

    • Insert: Checked

    • Update: Checked


Conditions Tab

To ensure this only runs when necessary, set the following Filter Conditions:

  • State | changes to | In Use


Advanced Scripting

On the Advanced tab, use the following code. Using gs.getUserID() is the server-side equivalent of fetching the currently logged-in user.

JavaScript
 
(function executeRule(current, previous /*null when async*/) {

    // gs.getUserID() returns the sys_id of the logged in user
    current.u_installed_by = gs.getUserID();

})(current, previous);

How it works in different scenarios:

  • Manual Update: If you (as an admin or technician) change the state on the form and click "Save," gs.getUserID() will return your Sys ID.

  • List Edit: If someone changes the state directly from the list view, it will return the Sys ID of the person who made that list edit.

  • Data Imports/Integrations: If a record is updated via an Integration (like SCCM or a REST API), it will return the Sys ID of the Service Account used for that integration.

  • Scheduled Jobs: If a background script or scheduled job triggers the update, it may return the "System" user ID, unless a specific user session is impersonated.

 

If this works, please mark as helpful and please accept my solution.....

 

Best Regards

SIVASANKARI S

Hi @bharathkumar Da 

If my reply is useful, please mark it as helpful and please accept my solution...

DiveshTyagi
Mega Guru

Hi @bharathkumar Da ,

 

 

This can be achieved cleanly using a Before Update Business Rule on the alm_hardware table.

Recommended Approach: Before Update Business Rule

Why Business Rule?
Because you want to automatically set a field value at the time the record is being updated (when State changes to In Use), a Before Update Business Rule is the correct and efficient approach.


Business Rule Configuration

  • Table: alm_hardware

  • When: Before

  • Update: checked

  • Condition:

State changes to In Use

(function executeRule(current, previous) {

    // Check if State is changing to "In Use"
    if (current.state.changesTo('In Use')) {
        current.u_installed_by = gs.getUserID(); // or gs.getUserName() if field is String
    }

})(current, previous);

Important Notes

  • If u_installed_by is a Reference field to sys_user, use:

gs.getUserID()
​
  • If it is a String field, use:
gs.getUserName()

 

  • Using changesTo() ensures the field is populated only once, when the state transitions to In Use, not on every update.


Best Practice

Avoid client scripts for this requirement, as server-side logic ensures consistency even for updates coming from:

  • Background scripts

  • Imports

  • Integrations

  • Flows

 

-------------------------------------------------------------------------------------------------------------------------------------------------

If my response helped, please mark it as the accepted solution so others can benefit as well.

Regards,

Divesh Tyagi