02-23-2016 04:13 AM - edited 03-01-2019 06:40 AM
Task Name | Create a LOV in UCSD from a API call to Service Now. |
Description | |
Prerequisites | Minimum UCSD version: 5.4.0.0 |
Category | Custom task |
Components | |
User Inputs | |
User Output |
Instructions for Regular Workflow Use:
Instructions for Regular Workflow Use:
A big thank you goes to: Ryan Criss
The workflow file contains a custom workflow task:
Custom Task Comments:
Input to the Custom Task:
Before Marshal obtaining the credentials:
The code:
importPackage(java.util);
importPackage(java.lang);
importPackage(java.io);
importPackage(org.w3c.dom);
importPackage(com.cloupia.lib.util);
importPackage(com.cloupia.model.cIM);
importPackage(com.cloupia.service.cIM.inframgr);
importPackage(com.cloupia.sysmgr.upgrade.util);
importPackage(com.cloupia.fw.objstore);
importPackage(com.cloupia.lib.util.easytrust);
importPackage(com.cloupia.service.cIM.inframgr.customactions);
importPackage(com.cloupia.service.cIM.inframgr.forms.wizard);
importPackage(com.cloupia.feature.customactions.lovproviders);
importPackage(com.cloupia.service.cIM.inframgr.forms.wizard);
importPackage(org.apache.commons.httpclient);
importPackage(org.apache.commons.httpclient.cookie);
importPackage(org.apache.commons.httpclient.methods);
importPackage(org.apache.commons.httpclient.auth);
importPackage(org.apache.commons.httpclient.protocol);
importClass(org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory);
importPackage(com.cloupia.lib.connector.account);
importPackage(com.cloupia.lib.connector.account.credential);
//----------------------------------------------------------------------------------------
//
// Author: Ryan Criss (rcriss@cisco.com)
//
// Workflow Task Name: ServiceNow Retrieve System User Groups v1
//
// Version: 1.0
//
// Description:
// This custom task retrieves a list of System User Groups from ServiceNow and creates a custom LOV entry in UCSD
//
// Inputs:
// servicenowHost: The hostname of the ServiceNow host.
// servicenowAccountName: The credential policy that contains the username and password info for the ServiceNow host.
// proxyHost: The proxy host if a proxy server is required.
// proxyPort: The proxy port if a proxy server is required.
//
// Outputs:
// None
//
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
// ### FUNCTIONS ###
//----------------------------------------------------------------------------------------
//
// Name: getAccount
// Description: This function gets the account information from the credentials policy.
//
function getAccount(accountName){
logger.addInfo("Looking for the following account:" + accountName);
var account = PersistenceUtil.getCredentialPolicyByName(accountName);
if (account != null){
logger.addInfo("Account:" + accountName + " found.");
return account;
} else {
logger.addError("Account:" + accountName + " NOT found.");
ctxt.setFailed("No Account found with name: " + accountName);
}
} // function getAccount
//----------------------------------------------------------------------------------------
//
// Created By: Ryan Criss (rcriss@cisco.com)
//
// Function Name: servicenowRetrieveSystemUserGroups()
//
// Version: 1.0
//
// Description: Retrieves the list of System User Groups from ServiceNow
//
// Inputs: servicenowHost ;The hostname of the ServiceNow host.
// servicenowEncUserPass ;The credential policy that contains the username and password info for the ServiceNow host.
// proxyHost ;The proxy host if a proxy server is required.
// proxyPort ;The proxy port if a proxy server is required.
// lovName ;The name of the LOV in UCSD
// lovProviderName ;The name of the LOV Provider in UCSD
//
// Outputs: customLovPairs ;An array list of all LOV Pairs that will be added to the LOV
//
//----------------------------------------------------------------------------------------
function servicenowRetrieveSystemUserGroups(servicenowHost, servicenowEncUserPass, proxyHost, proxyPort, lovName, lovProviderName) {
logger.addInfo("function servicenowRetrieveSystemUserGroups");
var taskClient = new HttpClient();
taskClient.getHostConfiguration().setHost(servicenowHost, 443, "https");
if (proxyHost && proxyPort){
logger.addInfo("The following proxy is in use: " + proxyHost + ":" + proxyPort);
taskClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
}
taskMethod = new GetMethod("/api/now/table/sys_user_group?sysparm_limit=10&sysparm_query=active=true&sysparm_fields=u_display_name,sys_id");
taskMethod.addRequestHeader("Authorization", "Basic "+servicenowEncUserPass);
taskClient.executeMethod(taskMethod);
statusCode = taskMethod.getStatusCode();
response = taskMethod.getResponseBodyAsString();
taskMethod.releaseConnection();
var jsonResponse = JSON.getJsonElement(response, null);
logger.addInfo("Number of items to be added to LOV: " + jsonResponse.get("result").size());
if (statusCode == 200){
// logger.addInfo("Response received: "+response);
// Convert JSON Response to LOV pairs
var customLovPairs = new ArrayList();
for (var i = 0; i < jsonResponse.get("result").size(); i++){
var customLovPair = new CustomLOV();
var sys_id = jsonResponse.get("result").get(i).get("sys_id").getAsString();
var u_display_name = jsonResponse.get("result").get(i).get("u_display_name").getAsString();
customLovPair.setLovName(lovProviderName);
customLovPair.setLovLabel(u_display_name);
customLovPair.setLovValue(sys_id);
customLovPair.setDisplayedLovValue(sys_id);
customLovPairs.add(customLovPair);
}
return customLovPairs;
} else {
logger.addError("Request Failed. HTTP response code: "+statusCode);
logger.addError("Response = " + response);
ctxt.setFailed("ServiceNow Request failed.");
}
} //function servicenowRetrieveSystemUserGroups
//----------------------------------------------------------------------------------------
//
// Created By: Ryan Criss (rcriss@cisco.com)
//
// Function Name: createNewLov()
//
// Version: 1.0
//
// Description: Creates a new LOV in UCSD
//
// Inputs: lovName ;The name of the LOV in UCSD
// lovProviderName ;The name of the LOV Provider in UCSD
// customLovPairs ;An array list of all LOV Pairs that will be added to the LOV
//
// Outputs: None
//
//----------------------------------------------------------------------------------------
function createNewLov(lovName, lovProviderName, customLovPairs) {
logger.addInfo("function createNewLov");
// Make sure LOV is not already registered
var providerNameList = null;
providerNameList = LOVProviderRegistry.getInstance().getLOVProvidersList();
for(var i = 0; i < providerNameList.size(); i++){
var name = providerNameList.get(i);
if(lovProviderName.equals(name)){
logger.addError("A lov provider with name :"+lovProviderName+" is already registered in the system.Please provide an other Lov provider name.");
ctxt.setFailed("Task failed");
ctxt.exit();
}
}
// Create and register LOV Custom Input
var wfCustomLovInput = new WFCustomLovInput();
wfCustomLovInput.setBaseInputType("gen_text_input");
wfCustomLovInput.setCustomValuesProvider(lovProviderName);
wfCustomLovInput.setWfinputName(lovName);
wfCustomLovInput.setWfinputType(lovName);
wfCustomLovInput.setLov(true);
try{
CustomActionUtil.addWFCustomLOVInput(wfCustomLovInput);
}catch(e){
logger.addError("Error occured while persisting the WFCustomLovInput:"+e);
ctxt.setFailed("Task got failed");
ctxt.exit();
}
CustomActionUtil.persistLOVPairs(lovProviderName, customLovPairs);
LOVProviderRegistry.getInstance().registerProvider(lovProviderName, new WFCustomLovProvider(lovProviderName));
var wfInputDef = new WorkflowInputFieldTypeDeclaration(lovName,lovName,FormFieldDefinition.FIELD_TYPE_EMBEDDED_LOV,lovProviderName);
WorkflowInputTypeRegistry.getInstance().addDeclaration(wfInputDef);
WorkflowInputTypeRegistry.getInstance().setMappable(lovName, "gen_text_input");
return true;
} //function createNewLov
//----------------------------------------------------------------------------------------
//
// Created By: Ryan Criss (rcriss@cisco.com)
//
// Function Name: updateExistingLov()
//
// Version: 1.0
//
// Description: Updates an existing LOV in UCSD
//
// Inputs: lovName ;The name of the LOV in UCSD
// customLovPairs ;An array list of all LOV Pairs that will be added to the LOV
//
// Outputs: None
//
//----------------------------------------------------------------------------------------
function updateExistingLov(lovName, customLovPairs) {
logger.addInfo("function updateExistingLov");
// Update existing LOV
var wfCustomLovInput = CustomActionUtil.getWFCustomLOVInput(lovName);
CustomActionUtil.persistLOVPairs(wfCustomLovInput.getCustomValuesProvider(), customLovPairs);
return true;
} //function updateExistingLov
//----------------------------------------------------------------------------------------
// ### MAIN PROGRAM ###
//----------------------------------------------------------------------------------------
// Workflow Inputs Mappings
var servicenowHost = input.servicenowHost;
var servicenowAccountName = input.servicenowAccountName;
var proxyHost = input.proxyHost;
var proxyPort = input.proxyPort;
// Set LOV and Provider Names
var lovName = "servicenowSystemUserGroupsLov";
var lovProviderName = "custom_provider_"+lovName;
// Retrieve Account Information
var servicenowAccount = getAccount(String(servicenowAccountName));
var servicenowUserPass = servicenowAccount.getUserName() + ":" + servicenowAccount.getPassword();
var servicenowEncUserPass = Base64Coder.encodeString(servicenowUserPass);
// Retrieve System User Groups
var systemUserGroups = servicenowRetrieveSystemUserGroups(servicenowHost, servicenowEncUserPass, proxyHost, proxyPort, lovName, lovProviderName);
// Create LOV
var wfCustomLovInput = CustomActionUtil.getWFCustomLOVInput(lovName);
if(wfCustomLovInput == null){
logger.addInfo("Creating the lov:"+lovName);
createNewLov(lovName, lovProviderName, systemUserGroups);
}
else{
logger.addInfo("Updating the lov:"+lovName);
updateExistingLov(lovName, systemUserGroups);
}
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: