Crow Canyon Software Forum
Script action error
Quote from Jay on December 15, 2024, 8:08 pmI have a few calcs to make on a form.
Some sub totals and a grant total. Rather than using many on change events to do the job I thought I would put it all in a script action and have the user press the script action to generate the totals.
I started off with a single calculation to see if it would work but it is failing straight away.
I get an error in the browser saying: Cannot read properties of null (reading 'get_item')My script is
function(spContext, formContext, listColumnsInfo, currentItem, functionCallback) {var TotalHours = 0var HourlyRate = 0HourlyRate = currentItem.get_item("Child_x0020_Care_x0020_costs_x00");TotalHours = currentItem.get_item("Number_x0020_of_x0020_Child_x002")var sumChildCare = TotalHours + HourlyRateCurrentItem.set_item(TotalChildCareAmount, sumChildCare)}
I have a few calcs to make on a form.
Some sub totals and a grant total. Rather than using many on change events to do the job I thought I would put it all in a script action and have the user press the script action to generate the totals.
I started off with a single calculation to see if it would work but it is failing straight away.
I get an error in the browser saying: Cannot read properties of null (reading 'get_item')My script is
function(spContext, formContext, listColumnsInfo, currentItem, functionCallback) {var TotalHours = 0var HourlyRate = 0HourlyRate = currentItem.get_item("Child_x0020_Care_x0020_costs_x00");TotalHours = currentItem.get_item("Number_x0020_of_x0020_Child_x002")var sumChildCare = TotalHours + HourlyRateCurrentItem.set_item(TotalChildCareAmount, sumChildCare)}
Quote from supportTeam on December 31, 2024, 6:26 amThanks for sharing the script and requirement details above.
We believe you are using the script in the form event action. If so, please use below syntax to read the column value. Please refer sample article on this from https://www.crowcanyon.help/article/469/.
Read column value: var HourlyRate = _ccs_FormUI.fetchColumnValueUI("Child_x0020_Care_x0020_costs_x00");
To set the required value to the column, we can use return statement as shown below.
return sumChildCare;
Thanks for sharing the script and requirement details above.
We believe you are using the script in the form event action. If so, please use below syntax to read the column value. Please refer sample article on this from https://www.crowcanyon.help/article/469/.
Read column value: var HourlyRate = _ccs_FormUI.fetchColumnValueUI("Child_x0020_Care_x0020_costs_x00");
To set the required value to the column, we can use return statement as shown below.
return sumChildCare;
Quote from Jay on January 20, 2025, 5:04 pmI'm still missing something here I got the field to update by using column value mapping and then selecting value (instead of script) and then updating a column with the product() of 2 other columns. This worked fine except that it would always update the column with the value of the first amount before the second had been provided. For example of the user typed 50 into ColumnA then ColumnC would show 50 even though the user had not entered a value into columnB. It was the NULL value in columnB was being treated as a 1.
So, as I have a few calcs to do on this form I thought I do it all in one script as if(ColumnA && ColumnB) sounded like it would solve the issue mentioned above.
However I can't get the "script" version of "column value mapping to work at all. I can't even get it to alert() me the return value and I get no errors in the dev tools console.I need (in the first instance) to be able the column called TotalChildCareAmount once I get that working I'll need to do likewise for another couple of fields - and then finally a grand total.
If I can get the first one working I should be good from there. I have checked and doubled checked the column internal names are correct.
It is set to execute on column value change and be applied in the new and edit formsfunction() {var CCperhour = _ccs_FormUI.fetchColumnValueUI("Child_x0020_Care_x0020_costs_x00");var numCChours = _ccs_FormUI.fetchColumnValueUI("Number_x0020_of_x0020_Child_x002");if(CCperhour && numCChours) {var sumChildCare = CCperHour * numCChours;return sumChildCare;alert('sumChildCare');}}
I'm still missing something here I got the field to update by using column value mapping and then selecting value (instead of script) and then updating a column with the product() of 2 other columns. This worked fine except that it would always update the column with the value of the first amount before the second had been provided. For example of the user typed 50 into ColumnA then ColumnC would show 50 even though the user had not entered a value into columnB. It was the NULL value in columnB was being treated as a 1.
So, as I have a few calcs to do on this form I thought I do it all in one script as if(ColumnA && ColumnB) sounded like it would solve the issue mentioned above.
However I can't get the "script" version of "column value mapping to work at all. I can't even get it to alert() me the return value and I get no errors in the dev tools console.I need (in the first instance) to be able the column called TotalChildCareAmount once I get that working I'll need to do likewise for another couple of fields - and then finally a grand total.
If I can get the first one working I should be good from there. I have checked and doubled checked the column internal names are correct.
It is set to execute on column value change and be applied in the new and edit formsfunction() {var CCperhour = _ccs_FormUI.fetchColumnValueUI("Child_x0020_Care_x0020_costs_x00");var numCChours = _ccs_FormUI.fetchColumnValueUI("Number_x0020_of_x0020_Child_x002");if(CCperhour && numCChours) {var sumChildCare = CCperHour * numCChours;return sumChildCare;alert('sumChildCare');}}
Quote from supportTeam on January 21, 2025, 8:22 amPlease make sure the variable CCperhour is consistent in the script(Please check the attachment). You can use the script below and let us know if it works for you.
var CCperhour = _ccs_FormUI.fetchColumnValueUI("Child_x0020_Care_x0020_costs_x00");
var numCChours = _ccs_FormUI.fetchColumnValueUI("Number_x0020_of_x0020_Child_x002");
if(CCperhour && numCChours) {
var sumChildCare = CCperhour * numCChours;
alert(sumChildCare);
return sumChildCare;}
Please make sure the variable CCperhour is consistent in the script(Please check the attachment). You can use the script below and let us know if it works for you.
var CCperhour = _ccs_FormUI.fetchColumnValueUI("Child_x0020_Care_x0020_costs_x00");
var numCChours = _ccs_FormUI.fetchColumnValueUI("Number_x0020_of_x0020_Child_x002");
if(CCperhour && numCChours) {
var sumChildCare = CCperhour * numCChours;
alert(sumChildCare);
return sumChildCare;
}
Uploaded files:Quote from Jay on February 3, 2025, 4:06 pmThanks - a step closer. Now I can get the alert to show but after I dismiss the alert box the script button will process until the script ultimately times out. The dev tools gives me the error CCperHour is not defined (I don't see any such spelling in my script only CCperhour)
var CCperhour = _ccs_FormUI.fetchColumnValueUI("Child_x0020_Care_x0020_costs_x00");
var numCChours = _ccs_FormUI.fetchColumnValueUI("Number_x0020_of_x0020_Child_x002");
if(CCperhour && numCChours) {
var sumChildCare = CCperhour * numCChours;
alert(sumChildCare);
return sumChildCare;
CurrentItem.set_item(TotalChildCareAmount, sumChildCare);
}The alert shows the value of sumChildCare but then the script errors out.
If I comment out //CurrentItem.set_item(TotalChildCareAmount, sumChildCare); it still fails so it seems to be the return statement that is generating the issue?
Thanks - a step closer. Now I can get the alert to show but after I dismiss the alert box the script button will process until the script ultimately times out. The dev tools gives me the error CCperHour is not defined (I don't see any such spelling in my script only CCperhour)
var CCperhour = _ccs_FormUI.fetchColumnValueUI("Child_x0020_Care_x0020_costs_x00");
var numCChours = _ccs_FormUI.fetchColumnValueUI("Number_x0020_of_x0020_Child_x002");
if(CCperhour && numCChours) {
var sumChildCare = CCperhour * numCChours;
alert(sumChildCare);
return sumChildCare;
CurrentItem.set_item(TotalChildCareAmount, sumChildCare);
}
The alert shows the value of sumChildCare but then the script errors out.
If I comment out //CurrentItem.set_item(TotalChildCareAmount, sumChildCare); it still fails so it seems to be the return statement that is generating the issue?
Quote from supportTeam on February 4, 2025, 7:15 amThe timeout error is due to "functionCallback();" missing after the script is executed. If you want to update the value of the column, please use "formContext.setColumnControlValueByName("TotalChildCareAmount",sumChildCare);" instead of "CurrentItem.set_item(TotalChildCareAmount, sumChildCare);".
Please try the below script provided and let us know if it is working as expected.
var CCperhour = _ccs_FormUI.fetchColumnValueUI("Child_x0020_Care_x0020_costs_x00");
var numCChours = _ccs_FormUI.fetchColumnValueUI("Number_x0020_of_x0020_Child_x002");
if(CCperhour && numCChours) {
var sumChildCare = CCperhour * numCChours;
alert(sumChildCare);
formContext.setColumnControlValueByName("TotalChildCareAmount",sumChildCare);
functionCallback();
}
functionCallback();
The timeout error is due to "functionCallback();" missing after the script is executed. If you want to update the value of the column, please use "formContext.setColumnControlValueByName("TotalChildCareAmount",sumChildCare);" instead of "CurrentItem.set_item(TotalChildCareAmount, sumChildCare);".
Please try the below script provided and let us know if it is working as expected.
var CCperhour = _ccs_FormUI.fetchColumnValueUI("Child_x0020_Care_x0020_costs_x00");
var numCChours = _ccs_FormUI.fetchColumnValueUI("Number_x0020_of_x0020_Child_x002");
if(CCperhour && numCChours) {
var sumChildCare = CCperhour * numCChours;
alert(sumChildCare);
formContext.setColumnControlValueByName("TotalChildCareAmount",sumChildCare);
functionCallback();
}
functionCallback();