Question on script include calling to on submit client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2024 05:39 AM
I have a table in back end , in catalog item in iteam level from country and to county variable are there same way MRVS contain from_country and to_country.based on the this 4 variables i need to auto populate the risk rating
in MRVS contain 4 records (ex:i will get the 4 risk rating in that i need to take the highest value
like high , medium , low, low for this values high is the highest value that value i need to auto populate the risk rating variable value. in script i am getting the one by one value .this values i try to put in array but i getting error
above mentioned diagram i am getting ans in one by one i need to pass the all values in one array
script include:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 09:35 AM
The current solution is the equivalent of loading a web page as many times as there are rows in the MRVS.
A way better approach would be to send the MRVS to the server (Ajax endpoint) in its entirety just once and get back the final result, doing all the processing server side.
The idea is to end up with as few server/Ajax calls as possible.
In that scenario the client code would be something like:
function onSubmit () {
// Verify that the risk rating has been computed prior to this submit
// Launch computing and exit returning false (to interrupt submit)
// if verificatin fails
if (!g_scratchpad.u_calculatedRiskRating) {
calculateRiskRating();
return false;
}
function JSONparse (json) {
try {
return JSON.parse(json);
}
catch (ex) {
console.error('Risk rate calculation', ex);
return {};
}
}
function calculateRiskRating () {
var vk = new GlideAjax('Decision_table_risk_rating');
vk.addParam('sysparm_name', 'getriskratings');
vk.addParam('sysparm_from', g_form.getValue('from'));
vk.addParam('sysparm_to', g_form.getValue('to'));
vk.addParam('sysparm_mrvs', g_form.getValue('risk_rating_from_country'));
vk.getXMLAnswer(setRiskRating);
}
function setRiskRating (payload) {
var data = JSONparse(payload);
if (data.u_calculatedRiskRating) {
// Set the flag that shows that risk rating has been complted
g_scratchpad.u_calculatedRiskRating = data.u_calculatedRiskRating;
// Save the risk rating in the appropriate field
g_form.setValue('', g_scratchpad.u_calculatedRiskRating);
// Re-submit the form
g_form.submit();
}
}
}
As for the server side, I'm not sure what you are trying to do there, but that is not how Decision Tables "work".
There is an API one should use: DecisionTableAPI.
Something like:
var dt = new sn_dt.DecisionTableAPI();
var dtInputs ={
'<decision table input name>': '<value for the given decision table input>',
// Adding all the inputs defined in the Descition Table
}
var response = dt.getDecision('4c171a6147878610f5539de4116d43f7', dtInputs);
var riskRating = response.result_elements.<the decision table output column that contains the risk rating information>;
Of course you would need to replace <decision table input name> and <the decision table output column that contains the risk rating information> with the input and output names as defined in the Decision Table.
I would also consider returning numeric information as risk rating.
In order to calculate the highest risk you will need to be able to sort risks.
Using text values as risk levels that is not really possible; e.g. a hierarchy of High - Medium - Low sorted alphabetically will end up High - Low - Medium, or Medium - Low - High which makes it impossible to establish the correct risk ranking between High and Low, High and Medium and Low and Medium using the same logic.
However if one uses numeric values, e.g. 1 for High, 2 for Medium and 3 for Low, calculating the highest risk using the same logic is trivial.
If one must show labels in the Catalog Item, one could define the Risk field as a drop-down which would have as choice values the risk output values.