Auto Populate Entitlement

Pandu3
Tera Contributor

Hello,

Based on "Asset" selection on case from need to auto populate the recently updated Entitlement  in "Entitlement" field on Case form.

Conditions: 

  • Selected Asset Entitlements only to show
  • Recently updated Entitlement need to populate
  •  Start Date should not be a future date
  • End Date -Future Date

Please, Can anyone help me on this.

11 REPLIES 11

So here we are looking at 2 onChange client scripts.
1. When Asset changes

var gaCaseEnt = new GlideAjax('global.EntitlementValidation');
gaCaseEnt.addParam('sysparm_name', 'checkEntitlement');
gaCaseEnt.addParam('sysparm_assetName', g_form.getValue('asset'));
gaCaseEnt.getXML(mycallback);

function mycallback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 400) {
getMessage("No active contract entitlement is associated with the asset", function(msg) {
alert(msg);
});

}else if(answer=="Start date should not be in future"){
	getMessage("YOUR CUSTOM MESSAGE", function(msg) {
alert(msg);
});
}
	else if(answer=="End date should not be in the past"){
		getMessage("YOUR CUSTOM MESSAGE", function(msg) {
alert(msg);
});}
	else{
		g_form.setValue('asset',answer);
	}

}


2. When Category changes

var gaCaseEnt = new GlideAjax('global.EntitlementValidation');
gaCaseEnt.addParam('sysparm_name', 'checkEntitlement');
gaCaseEnt.addParam('sysparm_assetName', g_form.getValue('asset'));
gaCaseEnt.addParam('sysparm_assetCategory',g_form.getValue('category'));
gaCaseEnt.getXML(mycallback);

function mycallback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 400) {
getMessage("No active contract entitlement is associated with the asset", function(msg) {
alert(msg);
});

}else if(answer=="Start date should not be in future"){
	getMessage("YOUR CUSTOM MESSAGE", function(msg) {
alert(msg);
});
}
	else if(answer=="End date should not be in the past"){
		getMessage("YOUR CUSTOM MESSAGE", function(msg) {
alert(msg);
});}
	else{
		g_form.setValue('asset',answer);
	}

}


The script include can be adjusted slightly.


var EntitlementValidation = Class.create();
EntitlementValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkEntitlement: function() {
var assets = this.getParameter('sysparm_assetName');
if(this.getParameter('sysparm_assetCategory'))
var category= this.getParameter('sysparm_assetCategory');
var type;
if(category=='Hardware')
type= gs.getProperty('<Name of your property>'); //or you can mention the static choice value
else if(category=='Software')
type=gs.getProperty('<Name of your property>'); //or you can mention the static choice value

var serviceEnt = new GlideRecord('service_entitlement');
serviceEnt.addQuery('asset', assets);
serviceEnt.addActiveQuery();
if(type)
serviceEnt.addQuery('u_type', type);//query for the exact type
serviceEnt.orderByDesc('sys_updated_on'); //to get the latest updated record
serviceEnt.setLimit(1);
serviceEnt.query();
if (!serviceEnt.next()) {
return 400;
}
else{
var start= serviceEnt.start_date;
var end= serviceEnt.end_date;
var now= new GlideDate();

if ((start>=now) || (end<=now))
return "No Active Entitlements."
else 
return serviceEnt.getUniqueValue();
}

},

type: 'EntitlementValidation'
});

 
Then it should work. You can test and further adjust based on requirement.

You can mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards

Pandu3
Tera Contributor

Hi mdash,

Thank you