How to submit the form in UI action

String
Kilo Sage

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 

@Ankur Bawiskar 

2 ACCEPTED SOLUTIONS

sushantmalsure
Mega Sage
Mega Sage

Hi @String 

I think you need to correct the script , thats not how its done.

sushantmalsure_0-1690455663280.png

 

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

 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

View solution in original post

@String 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

@String 

can you share complete script?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

String_0-1690460112070.png

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 ");
 
                gsftSubmit(null, g_form.getFormElement(), "getDetailsAction");
 
            }
        } 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 (typeof window == 'undefined')
    setRedirect();
 
function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}
 
now code is able to submit the form and redirect to the same record but  g_form.addInfoMessage("Below fileds are updated "); is not showed because form is immediately getting redirect .Is there any way to Pause the form for 5 secs to show the message to the agent on screen (form) and redirect  !

@String remove   g_form.addInfoMessage("Below fileds are updated "); 

line above gsftSubmit().

and add it in setRedirect()

 

function setRedirect() {
current.update();
gs.addInfoMessage('Below fileds are updated');
action.setRedirectURL(current);
}

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

@String 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@String 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader