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

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

@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 ?

just add gs.addInfoMessage()

Something like below inside setRedirect():

 

current.update();
gs.addInfoMessage('Field is 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

Ankur Bawiskar
Tera Patron
Tera Patron

@String 

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?

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