how to auto populate project manager field in demand form when business unit head field filled
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 10:15 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 11:51 PM
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.