- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 03:52 AM
Hi Team ,
am using the client script in UI action and trying to submit the for after filling the data ,but form is not saved or submitted
function getDetails() {
var v_displayRef = g_form.getReference('display_name');
var referencedSysId = g_form.getValue('display_name');
var assetID = referencedSysId;
var getValues = new GlideAjax('x_cenv_api_hbm_ton.FetchAdditionalDetails');
getValues.addParam('sysparm_name', 'GetDetailsAPI');
getValues.addParam('sysparm_asset', assetID);
getValues.getXML(getResponse)
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var formatJSON = JSON.parse(answer);
g_form.setValue('u_stock', formatJSON.Stock);
g_form.submit();
action.setRedirectURL(current);
}
Please guide me
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 04:02 AM
Hi @String
I think you need to correct the script , thats not how its done.
make sure of above highlighted areas
here is the script:
function getDetails() {
var v_displayRef = g_form.getReference('display_name');
var referencedSysId = g_form.getValue('display_name');
var assetID = referencedSysId;
var getValues = new GlideAjax('x_cenv_api_hbm_ton.FetchAdditionalDetails');
getValues.addParam('sysparm_name', 'GetDetailsAPI');
getValues.addParam('sysparm_asset', assetID);
getValues.getXML(getResponse);
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var formatJSON = JSON.parse(answer);
g_form.setValue('u_stock', formatJSON.Stock);
gsftSubmit(null, g_form.getFormElement(), "getDetailsAction");
}
}
if (typeof window == 'undefined')
setRedirect();
function setRedirect() {
current.update();
action.setRedirectURL(current);
}
Regards,Sushant Malsure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 05:28 AM
try this once
function getDetails() {
var v_displayRef = g_form.getReference('display_name');
var referencedSysId = g_form.getValue('display_name');
var loadingDialog = new GlideDialogWindow('load_meter', true);
//loadingDialog.setPreference('table', 'loading');
loadingDialog.setTitle('Please wait..');
loadingDialog.setSize(400, 200);
loadingDialog.render();
var assetID = referencedSysId;
var getValues = new GlideAjax('x_cenv_api_hbm_ton.FetchAdditionalDetails'); //Calling Script Include.
getValues.addParam('sysparm_name', 'GetDetailsAPI'); // Function name of the script include.
getValues.addParam('sysparm_asset', assetID);
getValues.getXML(getResponse.bind({
dialog: loadingDialog
}));
}
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != 500) {
try {
var formatJSON = JSON.parse(answer);
if (g_form.getValue('u_date_since_last_heart_beat') == formatJSON.DaysSinceLastHB && g_form.getValue('u_last_communicaton_date') == formatJSON.LastComDate && g_form.getValue('u_stock_location_id') == formatJSON.StockLocationId && g_form.getValue('u_stock_location_name') == formatJSON.StockLocationName && g_form.getValue('u_toner_enabled') == formatJSON.TonerEnabledFlag.toLowerCase()) {
this.dialog.destroy();
g_form.addInfoMessage("No new updates on Additional Details ");
//gsftSubmit(null, g_form.getFormElement(), "getDetailsAction");
} else {
g_form.setValue('u_date_since_last_heart_beat', formatJSON.DaysSinceLastHB);
g_form.setValue('u_last_communicaton_date', formatJSON.LastComDate);
g_form.setValue('u_toner_enabled', formatJSON.TonerEnabledFlag);
if (formatJSON.StockLocationId != undefined) {
g_form.setValue('u_stock_location_id', formatJSON.StockLocationId);
}
if (formatJSON.StockLocationName != undefined) {
g_form.setValue('u_stock_location_name', formatJSON.StockLocationName);
}
this.dialog.destroy();
g_form.addInfoMessage("Below fileds are updated ");
g_form.save();
}
} catch (error) {
this.dialog.destroy();
g_form.addErrorMessage("Error occurred, please try after some time.", error);
}
} else {
this.dialog.destroy();
g_form.addErrorMessage('Error occurred, please try after some time');
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 04:02 AM
Hi @String
I think you need to correct the script , thats not how its done.
make sure of above highlighted areas
here is the script:
function getDetails() {
var v_displayRef = g_form.getReference('display_name');
var referencedSysId = g_form.getValue('display_name');
var assetID = referencedSysId;
var getValues = new GlideAjax('x_cenv_api_hbm_ton.FetchAdditionalDetails');
getValues.addParam('sysparm_name', 'GetDetailsAPI');
getValues.addParam('sysparm_asset', assetID);
getValues.getXML(getResponse);
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var formatJSON = JSON.parse(answer);
g_form.setValue('u_stock', formatJSON.Stock);
gsftSubmit(null, g_form.getFormElement(), "getDetailsAction");
}
}
if (typeof window == 'undefined')
setRedirect();
function setRedirect() {
current.update();
action.setRedirectURL(current);
}
Regards,Sushant Malsure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 04:28 AM
@sushantmalsure thanks ,it works
can we pause redirect for 5secs ,because am trying to show g_form.addInfoMessage("filed is updated "); in the form .
Please guide me ,how to pause before redirect ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 04:52 AM
just add gs.addInfoMessage()
Something like below inside setRedirect():
current.update();
gs.addInfoMessage('Field is updated');
action.setRedirectURL(current);
Regards,Sushant Malsure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 04:04 AM
your script is wrong
1) action object won't work in client side
2) why not use g_form.save()
Can you share complete script?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader