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

mdash
Giga Guru

Hi There,

Just a few questions.

If there are more than one entitlements associated with an asset, only the latest one will populate?

Start and End dates are fields on the entitlement and contract table. If you already have the entitlement with all details, why do we need to enforce past and future?

Thanks

Pandu3
Tera Contributor

Hello,

  • If there are more than one entitlements associated with an asset, only the latest one will populate?

                             ---Yes ,the latest updated entitlement need to populate if more than one entitlements available . 

 

  • Start and End dates are fields on the entitlement and contract table. If you already have the entitlement with all details, why do we need to enforce past and future? 

                     here we are checking the entitlement start date <=current date and end date >=current date

 

Hope I answered your questions. Thanks.

Pandu3
Tera Contributor

Script include : 

var EntitlementValidation = Class.create();
EntitlementValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkEntitlement: function() {
var assets = this.getParameter('sysparm_assetName');
var serviceEnt = new GlideRecord('service_entitlement');
serviceEnt.addQuery('asset', assets);
serviceEnt.addQuery('active', true);
serviceEnt.query();
if (!serviceEnt.next()) {
return 400;
}

},

type: 'EntitlementValidation'
});

 

 

Client Script : 

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);
});

}

}

 

Through above script I'm checking the Selected Asset has "Active" entitlement or not . If there is no active entitlement available then displaying error message. If active entitlement available then need to auto populate the entitlement with above mentioned validations.

 

Thanks for sharing the script.

You can tweak the script include to return valid entitlement.

var EntitlementValidation = Class.create();
EntitlementValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkEntitlement: function() {
var assets = this.getParameter('sysparm_assetName');
var serviceEnt = new GlideRecord('service_entitlement');
serviceEnt.addQuery('asset', assets);
serviceEnt.addActiveQuery();
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)
return "Start date should not be in future"; //or some other error code
else if (end<now)
return "End date should not be in the past";//or some other error code
else 
return serviceEnt.getUniqueValue();
}

},

type: 'EntitlementValidation'
});



And in your client script, you can add two more conditions

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);
	}

}


Notes:
- Please change your alert statements/error codes as required and test it.
- There is an OOTB Reference Qualifier on Entitlement field on Case form. You may need to disable that.


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