Async functions in client script

MAJID_Z
Tera Contributor

Hi everyone!

I have a table containing some products and their prices. A customer fills and submits a form on Order table. 

 

Order Record: 

item1 (reference from product table), item1.price (dot-walk field)

item2 (reference from product table), item2.price (dot-walk field)

item3 (reference from product table), item3.price (dot-walk field)

total_price (read-only)

 

I'm using client script onChange of each item fields to recalculate the total_price on the form. 

But, total_price doesn't get update on the items change. Because the item.price is a dot-walk and is still 0 when the calculation function executes. It seems that I need a kind of async/await which I think doesn't work in client scripts.

I will appreciate if anyone can help me solving this problem.

 

1 ACCEPTED SOLUTION

Rafael Batistot
Kilo Patron

Hi @MAJID_Z 

 

In your case the issue is that the dot-walked field (item1.price, etc.) is not immediately available on the client when you change the reference field — it only resolves on form save/refresh.

To make it work on the form you need to explicitly fetch the price from the Product table with a GlideAjax or g_form.getReference().

 

Direct fix:

Use g_form.getReference() in your onChange script.

 

See an executable of script:

 

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

g_form.getReference('item1', function(prod) {
var price = prod.price || 0;

var item2Price = g_form.getValue('item2.price') || 0;
var item3Price = g_form.getValue('item3.price') || 0;

var total = parseFloat(price) + parseFloat(item2Price) + parseFloat(item3Price);
g_form.setValue('total_price', total);
});
}

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

View solution in original post

3 REPLIES 3

Rafael Batistot
Kilo Patron

Hi @MAJID_Z 

 

In your case the issue is that the dot-walked field (item1.price, etc.) is not immediately available on the client when you change the reference field — it only resolves on form save/refresh.

To make it work on the form you need to explicitly fetch the price from the Product table with a GlideAjax or g_form.getReference().

 

Direct fix:

Use g_form.getReference() in your onChange script.

 

See an executable of script:

 

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

g_form.getReference('item1', function(prod) {
var price = prod.price || 0;

var item2Price = g_form.getValue('item2.price') || 0;
var item3Price = g_form.getValue('item3.price') || 0;

var total = parseFloat(price) + parseFloat(item2Price) + parseFloat(item3Price);
g_form.setValue('total_price', total);
});
}

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

Hi @Rafael Batistot and thanks!

My problem solved with your solution!

Bit, I wonder why when I declare "total" and also set total_price value outside the getReference function, it doesn't work as expected. Do you have any idea about it?

 

@MAJID_Z 

 

Kindly show me where is not working in your code? 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.