catalog I want to calculate total ticket price based on two variables like source and destination

gubbala kishore
Giga Contributor

In catalog, I want to calculate total ticket price based on two variables like source and destination.

example:

Variable:

Buse Type: A/C Sleeper

source: Hyderabad

Destination: Chennai

Ticket Price: 1500

Variable:

Buse Type: Non A/C Sleeper

source: Hyderabad

Destination: Vizag

Ticket Price: 1200

 

And Also discount to age below 2 years----10%

 Age above 75 ----- 8%

 

1 ACCEPTED SOLUTION

Mallidi Suma
Tera Guru

Hi, @gubbala kishore ,

You can simply create 2 on-change client scripts with one reusable Script Included. Please find my below scripts to achieve that.

 

But I would suggest you, create a custom table with the fields like Bus Type, Source, Destination, and Ticket Price. After that create records with all possible combinations. 

On the catalog Side, 2 client scripts onChange of source, and onChange of Destination fields, and create one reusable script include and call it from both the onChange client scripts.

 

Please find the below steps:-

Step 1:-

Created one table with Bus Type, Source, Destination, and Ticket Price.

MallidiSuma_0-1684056326534.png

Step 2:-

Create the records in your custom table with all the possible entries. 

MallidiSuma_1-1684056363162.png

Step 3:-

On the Catalog Side, Create a source variable with a type select box and fetch its choices from your custom tables source field and repeat the same for the destination field.

MallidiSuma_2-1684056457455.png

 

Step 4:-

Create Onchange client scripts on the source and destination fields to populate the ticket price.

MallidiSuma_3-1684056507010.png

 

onChange of Source field Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('ClientCallableAjax');
    ga.addParam('sysparm_name', 'getPrice');
    ga.addParam('sysparm_source', newValue);
ga.addParam('sysparm_destination',g_form.getValue('destination'));
ga.addParam('sysparm_busType', g_form.getValue('bus_type'));
    ga.getXML(getResponse);
 
    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('ticket_price',answer);
}
}
 
onChange destination client script:-
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 
    var ga = new GlideAjax('ClientCallableAjax');
    ga.addParam('sysparm_name', 'getPrice');
    ga.addParam('sysparm_destination', newValue);
ga.addParam('sysparm_source',g_form.getValue('source'));
ga.addParam('sysparm_busType', g_form.getValue('bus_type'));
    ga.getXML(getResponse);
 
    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('ticket_price',answer);
}
}

 

Step 5:-

Create a script Include and call it from both the client scripts.

Script Include Code:-

var ClientCallableAjax = Class.create();
ClientCallableAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getPrice: function() {
 var source_val = this.getParameter('sysparm_source');
var des_val = this.getParameter('sysparm_destination');
var busType_val = this.getParameter('sysparm_busType');
var ticket_rec = new GlideRecord("u_bus_ticket_price_calculation");
ticket_rec.addQuery("u_source", source_val);
ticket_rec.addQuery("u_destination", des_val);
ticket_rec.addQuery("u_bus_type", busType_val);
        ticket_rec.query();
        if (ticket_rec.next()) {
            return ticket_rec.u_ticket_price;
        }
    },
    type: 'ClientCallableAjax'
});

 

Result:-

MallidiSuma_4-1684057126575.png

From the process perspective for process owners, it is easy to maintain the Bus Ticket Prices. If changed simply they can change its related entry in the Custom table. 

 

In order to apply the discount create another variable called age and build the logic in the script include.

 

Please my answer as helpful, if it helps you!!

View solution in original post

1 REPLY 1

Mallidi Suma
Tera Guru

Hi, @gubbala kishore ,

You can simply create 2 on-change client scripts with one reusable Script Included. Please find my below scripts to achieve that.

 

But I would suggest you, create a custom table with the fields like Bus Type, Source, Destination, and Ticket Price. After that create records with all possible combinations. 

On the catalog Side, 2 client scripts onChange of source, and onChange of Destination fields, and create one reusable script include and call it from both the onChange client scripts.

 

Please find the below steps:-

Step 1:-

Created one table with Bus Type, Source, Destination, and Ticket Price.

MallidiSuma_0-1684056326534.png

Step 2:-

Create the records in your custom table with all the possible entries. 

MallidiSuma_1-1684056363162.png

Step 3:-

On the Catalog Side, Create a source variable with a type select box and fetch its choices from your custom tables source field and repeat the same for the destination field.

MallidiSuma_2-1684056457455.png

 

Step 4:-

Create Onchange client scripts on the source and destination fields to populate the ticket price.

MallidiSuma_3-1684056507010.png

 

onChange of Source field Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('ClientCallableAjax');
    ga.addParam('sysparm_name', 'getPrice');
    ga.addParam('sysparm_source', newValue);
ga.addParam('sysparm_destination',g_form.getValue('destination'));
ga.addParam('sysparm_busType', g_form.getValue('bus_type'));
    ga.getXML(getResponse);
 
    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('ticket_price',answer);
}
}
 
onChange destination client script:-
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 
    var ga = new GlideAjax('ClientCallableAjax');
    ga.addParam('sysparm_name', 'getPrice');
    ga.addParam('sysparm_destination', newValue);
ga.addParam('sysparm_source',g_form.getValue('source'));
ga.addParam('sysparm_busType', g_form.getValue('bus_type'));
    ga.getXML(getResponse);
 
    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('ticket_price',answer);
}
}

 

Step 5:-

Create a script Include and call it from both the client scripts.

Script Include Code:-

var ClientCallableAjax = Class.create();
ClientCallableAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getPrice: function() {
 var source_val = this.getParameter('sysparm_source');
var des_val = this.getParameter('sysparm_destination');
var busType_val = this.getParameter('sysparm_busType');
var ticket_rec = new GlideRecord("u_bus_ticket_price_calculation");
ticket_rec.addQuery("u_source", source_val);
ticket_rec.addQuery("u_destination", des_val);
ticket_rec.addQuery("u_bus_type", busType_val);
        ticket_rec.query();
        if (ticket_rec.next()) {
            return ticket_rec.u_ticket_price;
        }
    },
    type: 'ClientCallableAjax'
});

 

Result:-

MallidiSuma_4-1684057126575.png

From the process perspective for process owners, it is easy to maintain the Bus Ticket Prices. If changed simply they can change its related entry in the Custom table. 

 

In order to apply the discount create another variable called age and build the logic in the script include.

 

Please my answer as helpful, if it helps you!!