UI Macro Script - Need to update a field using Jelly Script

Hari7
Kilo Guru

I am trying to update priority of the case ticket based on the button selected on the case form. 

I want the Priority to be set to 2 only when the "jvar_cq" is selected and Priority to be set to 5 when "jvar_pq" is selected.

I am not sure how to get the if condition added based on the button click and dont have much knowledge on the Jelly Script.

Request you help me out on this. Thanks. 

UI Macro:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<g:evaluate var="jvar_guid" expression="gs.generateGUID(this);" />
	<j:set var="jvar_cq" value="cq_fac_routing${jvar_guid}:${ref}"/>
	<j:set var="jvar_pq" value="pq_faa_routing${jvar_guid}:${ref}"/>

	<g:reference_decoration id="${jvar_cq}" 
							field="${ref}" 
							onclick="setRouting('cq_fac_routing', '${ref}');" 							
							title="${gs.getMessage('Set as Counselor Question')}" 
							image="images/icons/tasks.gifx" 
							icon="icon-hr"/>
	
	<g:reference_decoration id="${jvar_pq}" 
							field="${ref}" 
							onclick="setRouting('pq_faa_routing', '${ref}');" 
							title="${gs.getMessage('Set as Phone Question')}" 
							image="images/icons/tasks.gifx" 
							icon="icon-phone"/>
	
	<script>
		// react to an onchange event
		// show link if populated, hide if not
		// move to js file for multiple inclusion
		function decorationShow(element, original, changed, loading) {
			var $refButtonCQ = $j(gel('${jvar_cq}'));
			var $refButtonPQ = $j(gel('${jvar_pq}'));
		
			var routingID = g_form.getValue('${ref}');
		
			var gaClientCaseRoutingUtils = new GlideAjax('CaseUtilsAjax');
			gaClientCaseRoutingUtils.addParam('sysparm_name', 'controlRoutingMacro');
			gaClientCaseRoutingUtils.addParam('sysparm_case_routing_lookup_id', routingID);
			gaClientCaseRoutingUtils.getXMLAnswer(function(response) {
				var macroObj = JSON.parse(response);
		
				if (macroObj.display) {
					$refButtonCQ.show();
					$refButtonPQ.show();
				} else {
					$refButtonCQ.hide();
					$refButtonPQ.hide();
				}
			});
		}

		// todo: SB nice extjs event model
		var n = '${ref}'.replace(/\./g, '_');
		var h = new GlideEventHandler('onChange_' + n, decorationShow, '${ref}');
		g_event_handlers.push(h);
		
		function setRouting(actionName, reference) {
			var gaClientCaseRoutingUtils = new GlideAjax('CaseUtilsAjax');
			gaClientCaseRoutingUtils.addParam('sysparm_name', 'getEMCCRouting');
			gaClientCaseRoutingUtils.addParam('sysparm_action_name', actionName);
			gaClientCaseRoutingUtils.addParam('sysparm_contact_id', g_form.getValue('contact'));
			gaClientCaseRoutingUtils.getXMLAnswer(function(response) {
				var routingObj = JSON.parse(response);
			
				for (var field in routingObj) {
					if (routingObj[field].display_value) {
						g_form.setValue(field, routingObj[field].value, routingObj[field].display_value);
						<!-- g_form.setValue('priority', '2');	 -->					
					} else {
						g_form.setValue(field, routingObj[field].value);
					}
				}
			});
		}
	</script>
</j:jelly>
6 REPLIES 6

David Arbour
Tera Guru

Why can't you just set the value using g_form in your setRouting function? If the only thing you're basing priority on is the button clicked, you should be able to add this code outside of your AJAX call/callback.

// in the setRouting() function

if (actionName == 'cq_fac_routing') {
  g_form.setValue('priority', 2);

} else if (actionName == 'pq_faa_routing') {
  g_form.setValue('priority', 5);
}

Hi @David Arbour ,

Thanks for the response. Please find the below script include where the routing function is defined and I have tried your code on the script include but it didnt work. Could you please check where to apply this code to get this working. 

Script Include:

getEMCCRouting: function(actionName, contactID) {
		var grUser = this.getContactByID(contactID);
		var studentLevel = grUser.u_student_level.toString();
		
		switch (actionName) {
			case this.CQ_FAC_ROUTING_ACTION:
				return this._getCQ_FAC_Routing(studentLevel);
			case this.PQ_FAA_ROUTING_ACTION:
				return this._getPQ_FAA_Routing(studentLevel);
		}
			
		return {};
	},
	
	/**
	  * Description: 

	  * Parameters: 

	  * Returns:
	**/
	_getCQ_FAC_Routing: function(studentLevel) {
		var group;
				
		if (this.isCPSUndergraduate(studentLevel)) {
			group = this.getGroupByID(this.SFS_CPS_COUNSELORS);
		}
				
		if (this.isUndergraduate(studentLevel)) {
			group = this.getGroupByID(this.SFS_UDAY_FRONTLINE);
		}		
				
		return {
			assignment_group: group
		};
	},
	
	/**
	  * Description: 

	  * Parameters: 

	  * Returns:
	**/
	_getPQ_FAA_Routing: function(studentLevel) {
		var group;		
		
		if (this.isUndergraduate(studentLevel)) {
			group = this.getGroupByID(this.SFS_CX_FA_FRONTLINE);
		}		
		
		if (gs.nil(group)) {
			group = this.getGroupByID(this.SFS_CX_FA_FRONTLINE);
		}
		
		return {
			assignment_group: group
		};
	},
	
	/**
	  * Description: Function retrieves values to be set on a Case record
 
	  * Parameters: Boolean advanced script result, JSON script routing object result, GlideRecord Case Routing Record, Case field name
 
	  * Returns: JSON key/value object for attributes to be set on the affected Case
	**/
	getFieldData: function(scriptHasMatch, scriptedRoutingObj, grRoutingLookup, fieldName) {
		var fieldObj = {
			value: '',
			display_value: '',
		};
		
		// By default, retrieve a value set by the matched routing record
		fieldObj.value = (fieldName.substring(0, 2) == 'u_' ? grRoutingLookup[fieldName].toString() : grRoutingLookup['u_' + fieldName].toString());
		fieldObj.display_value = (fieldName.substring(0, 2) == 'u_' ? grRoutingLookup[fieldName].getDisplayValue() : grRoutingLookup['u_' + fieldName].getDisplayValue());
		
		// If the evaluated script evaluates or assigns a value to be set on the Case record, assign it here
		// This takes precedence over values on the routing record
		if (scriptHasMatch && scriptedRoutingObj.hasOwnProperty(fieldName)) {
			fieldObj.value = scriptedRoutingObj[fieldName].value || scriptedRoutingObj[fieldName];
			fieldObj.display_value = scriptedRoutingObj[fieldName].display_value || '';
		}
		
		return fieldObj;
	},

No, I was suggesting you put the code in your setRouting() function in your UI Macro like below. The getEMCCRouting() function returns a group, which presumably has nothing to do with the priority of the record. According to your requirements for this question, you only want to derive priority from the button that was clicked. If that's still the case, this should work for you.

function setRouting(actionName, reference) {
  if (actionName == 'cq_fac_routing') {
    g_form.setValue('priority', 2);

  } else if (actionName == 'pq_faa_routing') {
    g_form.setValue('priority', 5);
  }

  var gaClientCaseRoutingUtils = new GlideAjax('CaseUtilsAjax');
  gaClientCaseRoutingUtils.addParam('sysparm_name', 'getEMCCRouting');
  gaClientCaseRoutingUtils.addParam('sysparm_action_name', actionName);
  gaClientCaseRoutingUtils.addParam('sysparm_contact_id', g_form.getValue('contact'));
  gaClientCaseRoutingUtils.getXMLAnswer(function (response) {
    var routingObj = JSON.parse(response);

    for (var field in routingObj) {
      if (routingObj[field].display_value) {
        g_form.setValue(field, routingObj[field].value, routingObj[field].display_value);
      } else {
        g_form.setValue(field, routingObj[field].value);
      }
    }
  });
}

Regarding your Script Include:

  1. If you're calling a function with GlideAjax, you need to get your parameter values because you cannot pass parameters into a function call. This means you have to get your parameters using this.getParameter('sysparm_action_name') and this.getParameter('sysparm_contact_id') in your Script Include function.
  2. Your getEMCCRouting() function cannot return an object like it is now because returns from AJAX calls must be strings. You're already calling JSON.parse() on the response in your AJAX callback, but you're not calling JSON.stringify() in your Script Include.

Try replacing your getEMCCRouting function with the below code.

getEMCCRouting: function(actionName, contactID) {
  // this will look for the parameters using this.getParameter if actual parameters are not provided
  // this allows you to call this either with GlideAjax or from server code
  actionName = actionName || this.getParameter('sysparm_action_name');
  contactID = contactID || this.getParameter('sysparm_contact_id');

  if (!actionName || !contactID) {
    return null;
  }

  var grUser = this.getContactByID(contactID);

  if (grUser) {
    var studentLevel = grUser.getValue('u_student_level');

    switch (actionName) {
      case this.CQ_FAC_ROUTING_ACTION:
        return JSON.stringify(this._getCQ_FAC_Routing(studentLevel));
      case this.PQ_FAA_ROUTING_ACTION:
        return JSON.stringify(this._getPQ_FAA_Routing(studentLevel));
    }
  }

  return null;
}