Adding a Custom Field to the Service Catalog Checkout form when Cart Layout(sc_layout) is enabled!
If you want to add a new field when sc_layout is disabled, then, check this: Legacy:Adding a Field to the Service Catalog Checkout - ServiceNow Wiki.
This document is also written on the same lines, with Cart layouts enabled.
So, lets take an example of adding a Company field to the checkout below the Requested for field:
This field will then pass the value of that field to the Company field of the Service Catalog Request.
This example makes the following assumptions.
- This example is for an instance using two-step checkout with new cart layout v2. If two-step checkout is not enabled, enable it
- This example populates the Company field on the Service Catalog Request form. If the field does not appear on the form, configure the form
To add a field to the Service Catalog Checkout:
Go to System UI > UI Macros and create a new UI Macro named sc_cart_view_company.
In the XML editor, add the following code:
<?xml version="1.0" encoding="utf-8" ?>
${gs.getMessage('Company')}
Now, navigate to System UI > UI Pages and select the servicecatalog_checkout_one_v2 UI Page. Add this script to the Client script field:
function setCartValue() {
var newField = gel('core_company');
var myCart = gel('cart_id');
var cart_item = new GlideRecord('sc_cart_item');
cart_item.addQuery('cart', myCart.value);
cart_item.query();
if(cart_item.next()) {
cart_item.hints = "";
cart_item.update();
}
}
- For this example, the reference field was called core_company, and the field being populated on the request is company. If different fields are used:
- Find this line: var company = gel('core_company');
- In the line that starts with 'cart_item.hints' replace 'request.company' with the name of the field to be populated on the request ticket where 'request' is the request being generated and 'company' is the name of the field.
- Find this line: var company = gel('core_company');
- Now navigate to Service Catalog > Maintain Cart Layouts and select Target type: Browser Screen > Cart Preview Screen (Two Step).
- Go to Widget Macros tab, Select Edit and add the UI Macro created "sc_cart_view_company" from the list collector.
- Update the Order(say, 650) of the created macro to decide its position relative to other macros.
If i missed something, or some corrections, any kinda queries are always welcome.
https://www.servicenow.com/community/itsm-articles/adding-a-custom-field-to-the-service-catalog-checkout-form-when/ta-p/2313378