Client script needed. Use case: when user selects category and dependent subcategory then show alert

BiancaK
Tera Expert

Good day

 

I am needing show an alert message when the user selects the category and subcategory on a catalog item. 

Once the user selects the category and subcategory then an alert should appear. 

 

I managed to achieve this : when user selects category show alert, but not able to achieve category and subcategory 

Here is my code:

function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || newValue == '') {
return;
 }

// Get the value of Category
var cat = g_form.getValue('category');
var subCat = g_form.getValue('sub_category');

 // If  Category is A show alert
 if (cat == 'A') {
 alert('Are you sure you require this category');

 }
}
 
How do I cater for subcategory in the if statement?

 

1 ACCEPTED SOLUTION

Bidduam
Tera Guru

This should work for you - you just need to make sure that what you are looking for is the value, not the display value

 

I've tested this and it works for me

Bidduam_0-1723702726517.png

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var Cat = g_form.getValue('u_category');
	var SubCat = g_form.getValue('u_sub_category');
    
	if (Cat == 'a' && SubCat == 'x') {

        spModal.open({
            'title': "Cat and Sub Cat pop upl",
            'message': "Are you sure you want to do this?",
			
            'buttons': [{
                label: 'Yes',
                primary: true,
            }, ],
            'backdrop': 'static',
            'keyboard': false,
        });

    }
}

View solution in original post

4 REPLIES 4

Bidduam
Tera Guru

This should work for you - you just need to make sure that what you are looking for is the value, not the display value

 

I've tested this and it works for me

Bidduam_0-1723702726517.png

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var Cat = g_form.getValue('u_category');
	var SubCat = g_form.getValue('u_sub_category');
    
	if (Cat == 'a' && SubCat == 'x') {

        spModal.open({
            'title': "Cat and Sub Cat pop upl",
            'message': "Are you sure you want to do this?",
			
            'buttons': [{
                label: 'Yes',
                primary: true,
            }, ],
            'backdrop': 'static',
            'keyboard': false,
        });

    }
}

Thanks I have tried this already but did not work

Thanks @Bidduam 

I see where I went wrong now. Looked at your screenshot and see you have the field as subcategory. I changed the field on my side now as well from Category to Subcategory and now the code works. 

Thanks so much!

@BiancaK glad it worked.

You can also swap out the && if you want it to work if either the category is 'a' or sub category is 'x' with ||