Catalog Item Add to cart vs Submit - client Script

BartoszN
Tera Contributor

Folks, 

 

I'm having an quesiton - maybe someone of You have already archive it, I have build the client script to validate if the user has anything into the cart, if yes, the confirmation popup will populate.

 

 

the problem is, "add to cart" and "submit" buttons are "submit" types of the buttons ( g_form.getActionName() ) - return submit, have You had any idea how to verify which button gets "clicked" by the end user ? 

 

Thanks in advance.

 

function onSubmit() {
if (g_scratchpad.isFormValid){
	return true;
}
  var items = new GlideRecord('sc_cart_item');
items.addQuery('cart.user',  g_user.userID);
items.query(validate);
return false;
}

function validate(items)
{

	if(items.next())
	{
		g_scratchpad.isFormValid=false;
						var show_popup=confirm(getMessage("You have few items in Cart, please validate else submit."));
						if(show_popup==true)
						{
							g_scratchpad.isFormValid=true;
							g_form.submit(actionName);
						}
	}
	else
	{
		g_scratchpad.isFormValid=true;
		g_form.submit(actionName);
	}
	g_scratchpad.isFormValid=false;
}
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@BartoszN 

what's your business requirement?

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

@Ankur Bawiskar I actually have a similar requirement, where there are variations of the same item, which can all be added to the same request. So if the user clicks Submit, I want to create a request right away, and be done with it. However, when the user clicks Add to Cart, I want to clear all fields on the form to allow the user to enter new values (which will then be added to the same cart). I am calling

location.reload();
in my onSubmit handler, which works great for Add to Cart, but causes a reload when the user clicks Submit, which is redundant and undesired. If I had a way to check whether onSubmit is called for Submit, I would skip reloading the form.
Hope this use case makes sense. However, if there is a better way to solve my problem, I am all ears.