Javascript for Variables in processes
In a process, you can use a variable to pull data from the Service Catalog into your process. The text below gives examples on different data types.
With the new code update (Feb 2023) it is possible to update a variable using javascript.
This allows to refresh the data every time the variable is used in the process.
This document shows some examples, as the needed JS is different per type of data.
Where to place the JS
When editing a process, on the first tab, go to ‘Variables’.
- Select ‘JavaScript’ in the dropdown
- Check the box ‘ServiceCatalogInterface’
- Enter the JS in the box for the default value
Different types of JS
In yellow: the internal label if the field in the SC
In green : the GUID of the service, in this example, it comes from an action form.
Retrieving a plain value
var serviceparts = JSON.parse(ServiceCatalogInterface.getServiceParts(#REPLACE-FORMFIELD-serviceguid#, ["radiology_clinical_report"]));
if (serviceparts.length) return serviceparts[0].value;
return "";
Retrieving the full name of a custom actor
The JS below is long, as it covers all possible types of custom actor. To retrieve the email, see the next chapter.
var servicesummary = JSON.parse(ServiceCatalogInterface.getServiceSummary(#REPLACE-FORMFIELD-serviceguid#));
if (servicesummary && servicesummary.customactors && servicesummary.customactors.length) {
var customactors = servicesummary.customactors.filter(function (ca) { return ca.internalname === "CRA"; });
if (customactors.length) {
var customactor = customactors[0];
if (customactor.typename === "Single_person") {
//value is single object
if (customactor.value) {
return customactor.value.firstname + ' ' + customactor.value.lastname;
}
} else if (customactor.typename === "Single_persongroup") {
//value is single object
if (customactor.value) {
return customactor.value.name;
}
} else if (customactor.typename === "Multiple_persons") {
//value is array
if (customactor.value) {
return customactor.value.map(function (person) { return person.firstname + ' ' + person.lastname }).join(", ");
}
} else if (customactor.typename === "Multiple_persongroups") {
//value is array
if (customactor.value) {
return customactor.value.map(function (persongroup) { return persongroup.name }).join(", ");
}
}
}
}
return "";
Retrieving the email address of a custom actor
var servicesummary = JSON.parse(ServiceCatalogInterface.getServiceSummary(#REPLACE-VARIABLE-1067-ServiceGUID#));
if (servicesummary && servicesummary.customactors && servicesummary.customactors.length) { var customactors = servicesummary.customactors.filter(function (ca) { return ca.internalname === "Head_of_department"; });
if (customactors.length) { var customactor = customactors[0];
if (customactor.typename === "Single_person") { //value is single object if (customactor.value) { return customactor.value.email; } }
else
if (customactor.typename === "Single_persongroup") { //value is single object if (customactor.value) { return customactor.value.email; } }
else
if (customactor.typename === "Multiple_persons") { //value is array if (customactor.value) { return customactor.value.map(function (person) { return person.email }).join(", "); } }
else
if (customactor.typename === "Multiple_persongroups") { //value is array if (customactor.value) { return customactor.value.map(function (persongroup) { return persongroup.email }).join(", "); } } } } return "";
Retrieving a date in a readeable format
var serviceparts = JSON.parse(ServiceCatalogInterface.getServiceParts(#REPLACE-FORMFIELD-serviceguid#, ["Geplande_einddatum "]));
if (serviceparts.length && serviceparts[0].value) return CommonInterface.utcDateToString(new Date(serviceparts[0].value), "dd/MM/yyyy");
return "";
Retrieving data from a multiselect field
var serviceparts = JSON.parse(ServiceCatalogInterface.getServiceParts(#REPLACE-FORMFIELD-serviceguid#, ["locaties"]));
if (serviceparts.length && serviceparts[0].value) return serviceparts[0].value.join(", ");
return "";
Retrieving ‘yes’ from a Yes/No-field
var serviceparts = JSON.parse(ServiceCatalogInterface.getServiceParts(#REPLACE-FORMFIELD-serviceguid#, ["site_initiation_visit_is_required"]));
if (serviceparts.length && (serviceparts[0].value === true || serviceparts[0].value === false)) return serviceparts[0].value ? "Yes" : "No";
return "";
Retrieve the Phase
var service = JSON.parse(ServiceCatalogInterface.getServiceOverview(#REPLACE-VARIABLE-1682-ServiceGUID#)); return service && service.phase ? service.phase.name : ""
Retrieve the Technical Approvers Person Groups
var service = JSON.parse(ServiceCatalogInterface.getServiceSummary(#REPLACE-VARIABLE-1682-ServiceGUID#, { LoadRelations: false })); return service.technicalapprovalpersongroups ? service.technicalapprovalpersongroups.map(function (grp) { return grp.email; }).filter(function (mail) { return !!mail; }).join("||") : "";
Last updated
Was this helpful?