Client Script - OnChange alter display value of same field

balu3
Kilo Guru

Hi All,

I have this doubt from sometime now. And there was like 2 situation when this would have been useful. But because I couldn't find a solution, I had to implement a lengthy solution.

There are many situations where this would be useful. I'll classify them to 2 cases.

Updating the same field in onChange script will lead to stack size exceed error, if g_form.setValue is used. So html element control is used.

Assumption: I have a table ('test') with 2 fields [description -> String, demo_date_time -> DateTime]

Case 1: Alter display of string based fields

Say I have an OnChange Client Script on 'description' which should prepend a text 'Description is '. But this should not be stored. It is only for display.

Table: test

Type: onChange

Field name: Description

Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (newValue === '') {

          return;

    }

    control.value = 'Description is ' + newValue;

}

The above script will run when the form is loaded and when the value is changed.

But the above code is actually replacing the value, which is not what I want. Like if I save the form 'Description is ' is also saved.

So I want to know what is the display value exactly. And also it's equivalent HTML.

I mean the 3rd parameter in g_form.setValue(<field>, <value>, <display_value>);

Case 2: Display only date in a date time field

This is the most useful case.

There are a lot of scenarios where the client doesn't want a new date field to be added to an existing table (especially task), because there is a date-time (timestamp) field already solving the purpose. However they require the time to be hidden in the form view.

Table: test

Type: onChange

Field name: Demo Date Time

Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (newValue === '') {

          return;

    }

      control.value = newValue.split(' ')[0];

}

The above code will definitely not work. Because submitting only the date part for the date-time will pop an error message.

This is where I think, instead of the actual value if I was able to only set the display value then the users will see only the date part, and also the submit would have been successful.

Please help me know if there is a way to do it.

Thanks in Advance

Balu

1 ACCEPTED SOLUTION

Deepak Ingale1
Mega Sage

g_form.setValue(<field>, <value>, <display_value>);



This third parameter comes in to play when you deal with "Reference" type of fields. Field points to column name, Value points to sys_id of the reference field, display_value will point to display value of the reference record. This ensures that you dont have to make extra server call for setting up the reference field value from client end



For you requirement, you want to write a before business rule which will replace a "Description is " part before data gets committed to database. OR You can do it via onSumbit script as well on client side.




View solution in original post

4 REPLIES 4

roehltablada
Mega Guru

Hi Balakrishnan,



The 'control' is mapped to the column so there is no way to alter its display value without modifying its true value.



If the 'control' is meant for display only, I would create a calculated column to complement the true column and have the display value in the calculated column.



Be aware though that calculated columns may slow down your forms.




Regards,



Roehl Tablada


Deepak Ingale1
Mega Sage

g_form.setValue(<field>, <value>, <display_value>);



This third parameter comes in to play when you deal with "Reference" type of fields. Field points to column name, Value points to sys_id of the reference field, display_value will point to display value of the reference record. This ensures that you dont have to make extra server call for setting up the reference field value from client end



For you requirement, you want to write a before business rule which will replace a "Description is " part before data gets committed to database. OR You can do it via onSumbit script as well on client side.




Hi Deepak,



Sorry. It was my mistake. I should have conducted more tests before asking the question.



Well, like the string based fields are not taking the value of display at all. And the date time based fields are taking the value of display, but is setting it as the actual value instead of just display.




Thanks


Balu


Hi Balakrishnan,



Please never say sorry for asking question