how to auto populate project manager field in demand form when business unit head field filled

gpr123
Tera Contributor

good morning all
i need steps to auto populate the project manager field in demand form when the business unit head field is given.can anyone suggest the solution for it.

 

1 REPLY 1

PrashantLearnIT
Giga Sage

Hi @gpr123 

 

To auto-populate the "Project Manager" field in the Demand form when the "Business Unit Head" field is given in ServiceNow, you can use a combination of Client Scripts and GlideAjax. Here are the detailed steps:

Step-by-Step Solution:

1. Create a Script Include:
- Navigate to System Definition > Script Includes
- Create a new Script Include to fetch the Project Manager based on the Business Unit Head.

```javascript
var DemandHelper = Class.create();
DemandHelper.prototype = {
initialize: function() {},

getProjectManager: function(businessUnitHead) {
// Query the appropriate table to get the Project Manager
var userGr = new GlideRecord('sys_user'); // Adjust the table if needed
userGr.addQuery('manager', businessUnitHead); // Adjust the query based on your logic
userGr.query();
if (userGr.next()) {
return userGr.sys_id.toString();
}
return '';
},

type: 'DemandHelper'
};
```

- Ensure the Script Include is Client Callable.

2. Create a Client Script:
- Navigate to Service Catalog > Catalog Definition > Maintain Items (or Maintain Catalogs if your demand form is in the Service Catalog).
- Open the form for your Demand item and go to the Catalog Client Scripts related list.
- Create a new Client Script.

```javascript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}

var ga = new GlideAjax('DemandHelper');
ga.addParam('sysparm_name', 'getProjectManager');
ga.addParam('sysparm_business_unit_head', newValue);
ga.getXMLAnswer(function(response) {
var projectManagerSysId = response.responseXML.documentElement.getAttribute('answer');
if (projectManagerSysId) {
g_form.setValue('project_manager', projectManagerSysId);
} else {
g_form.clearValue('project_manager');
}
});
}
```

- Set the Type to onChange.
- Select the Business Unit Head field for the `Field name`.

3. Update the Field Names:
- Ensure the field names in your script (like `business_unit_head` and `project_manager`) match the field names in your Demand form.

Explanation:

1. Script Include:
- `DemandHelper` script include has a method `getProjectManager` that queries the `sys_user` table to find a user with the given Business Unit Head as their manager.
- This method returns the `sys_id` of the Project Manager.

2. Client Script:
- When the Business Unit Head field changes, the onChange client script is triggered.
- The client script uses `GlideAjax` to call the `getProjectManager` method in the `DemandHelper` Script Include.
- The response from the server is used to set the value of the Project Manager field.

By following these steps, the "Project Manager" field will be auto-populated based on the "Business Unit Head" field value in the Demand form.

 

 

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************