g_form.submit() returns true or false in the Service Portal

hamzaberouil
Kilo Contributor

Hi all,

I need clarifications on the GlideForm function subtmit() returns.

In the ServiceNow docs, the returns type is "void" but when I test "alert(g_form.submit())" it returns 'true' when a catalog item is submitted and 'false' when the submission is aborted. It sounds like the ServiceNow docs is wrong.

find_real_file.png

I want to use this because the customer ask to redirect to another page after the submission of a catalog request, the first try was like :

find_real_file.png

But we realized if the client forgot to fill mandatory fields, he is redirect to the page even though the submission is aborted because of the mandatory fields and the client loses the data he filled before he submits the form.

So we tried the code below, the if statement verifies if the the submission was accepted or not, if yes then we redrect else we do nothing

find_real_file.png

I am wondering if someone can confirm this solution. Is it steady ?

4 REPLIES 4

Loki2
Tera Contributor

Hi Hamza,



I would suggest that you use an OnSubmit catalog client script and then redirect the users, by doing that form will not be submitted without mandatory fields so there will be no data loss also user will not be redirected to any page.



You can make a condition check in OnSubmit if you want and then redirect the users accordingly.


For Example:



If(condition)


{


location.href = "?id=sc_cart";


}



If your are using this for portal as well then do make sure that you make Onsubmit script available to portal by selecting "All" in UI type field of client script.



Regards,


Lokesh Garg



P.S. : Please mark correct or helpful if appropriate.


Thank you Lokesh.



The redirection must be set only for Portal.



Can we do it in Catalog Client Script without dom manipulation ?



Thanks


Hi Hamza,



In catalog client script you can get the current URL parameters and based on that you can check whether it is portal or any other page and based on the condition check you can redirect using this script:



var id= getParmVal();



If(id){


location.href = "?id=sc_cart";}



function getParmVal(){


      var url = document.URL


      if(url.indexOf("sp")!=-1){


              return true;


      }


      else{


              return false;


      }


}



I hope it helps and will solve your problem.



Regards,


Lokesh Garg



P.S. : Please mark correct or helpful if appropriate.


Thanks Lokesh.



That would mean the Client Script will always be executed, which is not a good practice.



It seems the solution I described works fine, I'll keep you informed