Using the Form Designer › How to Use JavaScript Expressions in Fields › How to Pre-Populate Fields Based on JavaScript Expressions
How to Pre-Populate Fields Based on JavaScript Expressions
When you use an element that includes the value attribute as a field on a form, you can use a JavaScript expression to pre-populate the element's value attribute with the runtime value of one of many objects and properties from the CA Service Catalog database.
For example, in the text field for First Name on your form, you could specify the JavaScript expression $(_.user.firstName) to pre-populate the field with the first name of the logged in user ID who is creating the request and completing the form. Similarly, in the text field for Last Name, you could specify the JavaScript expression $(_.user.lastName) to pre-populate the field with the user's last name.
To pre-populate fields on forms using JavaScript expressions, follow this process:
- Design and create the form.
- Verify that the best way to perform the automated task in the field or fields of interest is to use a JavaScript expression.
- Review the guidelines for using JavaScript expressions in fields and verify that you want to pre-populate the value attribute of an element in the field.
- Verify that the data that you want to pre-populate is one of the objects and properties that you can specify in JavaScript expressions. These objects and properties are related to the logged in user's personnel data, to one or more business units, or to service, service options, status, or other data related to the request that contains the form.
- Specify the JavaScript expression in the value attribute of the element for the field. When specifying the expression, follow all syntax rules in objects and properties that you can specify in JavaScript expressions, especially the rules related to using operators to link properties. The following are some valid expressions that you may find useful as-is or as models:
- User's first name: $(_.user.firstName)
- User's last name: $(_.user.lastName)
- User's first name and last name, concatenated in a single field: $(_.user.firstName + ' ' + _.user.lastName)
This example could be used to pre-populate a text field at the end of the form, such as a thank you message.
- User's city: $(_.user.location.city)
- User's state: $(_.user.location.state)
- User's physical address data, concatenated in a single field: $(_.user.location.address[0] + ‘ ‘ + _.user.location.address[1] + ' ' + _.user.location.city + ' ' + _.user.location.state)
This example returns the user's street address, city, and state, for an address in the United States.
- User's role: $(_.user.roles[domainId])
- User's business unit, with parent: $(_.bu.id)
- User's business unit, without parent: $(_.bu.id.parent)
- User ID for whom the request is made: $(_.request.requestedFor)
- User ID who made the request: $(_.request.requestedBy)
- Name of the service associated to the form: $(_.service.name)
- Status of the service associated to the form: $(_.service.status)
- Name of service option group in the service associated to the form: $(_.sog.name)
- Description of service option group in the service associated to the form: $(_.sog.name.description)
- Status of service option group in the service associated to the form: $(_.sog.name.status)
- Test the JavaScript expression to verify that it accomplishes your goals for pre-populating the field on the form.
- As a best practice, verify that you test the expression in a form used with a service in a test environment before you use the form and the service in a production environment.