Setting textbox value to calculated field output


Badge +3

Hi

I have a calculated field with formula

If(MRMapType=="SSCC Report Map","ANNEXURE A:","")

I would like the output of this calculated field to appear in a textbox on the form

For example

The Textbox will display ANNEXURE A if the MRMapType is equal to 'SSCC Report Map'

I thought that setting the Connected to field in the calculated field would work but not so.

Does anyone know how this might be done?

Cheers

Laurie


13 replies

Badge +8

Try the below workaround (this worked for me):

  1. Set 'Store Client ID in JavaScript variable' for the Calculated and Textbox field controls
  2. In Custom Javascript of the Form, on document click event check if Calculated field value is not same as Textbox field value to assign Calculated field value to Textbox

201491_pastedImage_1.png

Userlevel 6
Badge +12

Hi Laurie,

What do you want to do with the text box?  Do you simply want the calculated value to be saved back to your list or did you want the text box to be populated with that value and still be editable?

Cheers,

Chris

Badge +10

That does not work here sad.png

I did exactly as you told:

1. Set 'Store Client ID in JavaScript variable' for the Calculated and Textbox field controls:

201493_pastedImage_1.png

201494_pastedImage_2.png

2. Put this code into Custom Javascript of the form:

NWF$(document).ready(function() {
NWF$(document).click(funktion() {
if(NWF$("#"+ calField).val() != NWS$("#"+ txtField).val())
NWS$("#"+ txtField).val(NWS$("#"+ calField).val());
});
});

Chris Ben‌: yes, I want to have a text box populated with a (dynamic) value and it should be editable.

BTW: that is something I would like to have as a standard. If you think so too, please vote for it here:

https://nintex.uservoice.com/forums/229406-2-nintex-forms-for-sharepoint/suggestions/18838480-use-a-calculated-default-value-in-a-single-line-te 

Badge +3

Hi

I tried Chaitra's solution and while it did put the value in the textbox when I clicked on another textbox on the form the event fired again causing any text after ANNEXURE to be deleted and replaced with  ANNEXURE A:

Not what I wanted but this is how the code would work I think.

To answer Chris's question I want the text box to be populated with that value ANNEXURE A and still be editable.

The users will add further text so the final text might be  ANNEXURE A: Yuendumu

Cheers

Laurie

Badge +8

So is it one time onload of the form you require the Calculated field value in TextBox, it can be done using the below script:

NWF.FormFiller.Events.RegisterAfterReady(function () {
NWF$(document).ready(function() {

var calculateControl = document.getElementById(calField);
var textboxControl = document.getElementById(txtField);
textboxControl.value = calculateControl.value;
});
});

Badge +10

That did not work here. Reason: script is faster than the calculated value calculation.

I built in a delay, and with that it works:

NWF.FormFiller.Events.RegisterAfterReady(function () { NWF$(document).ready(function() {function sleep(ms) {return new Promise(resolve => setTimeout(resolve, ms));} async function demo() { await sleep(500);textboxControl.value = calculateControl.value;  }
var calculateControl = document.getElementById(calField); var textboxControl = document.getElementById(txtField);
if (calculateControl.value===''){demo()}; }); });

Cheers

mai-kel

Userlevel 5
Badge +14

look here for one of possible solutions  

I believe you would find several other examples if you searched the forum

Badge +3

Hi

Thanks for all the help.

The calculated value would be written to the textbox on the change of a drop down, not on a form load or a general

The link provided by Marian talks about what I want but does not include the code for the following

"fire a change() method on control like a dropdown then write the value from the calculated value into my text box" 

In my case the dropdown is called MRMapType. I don't know javascript syntax (obviously) but I think code that would do the above would do the trick

Userlevel 5
Badge +14

The link provided by Marian talks about what I want but does not include the code for the following

"fire a change() method on control like a dropdown then write the value from the calculated value into my text box" 

if I understood your situation correctly you not do need any dropdown change handler.

since your calculated value control's formula depends on dropdown value (MRMapType), it will be automatically recalculated with every change of the dropdown. and if the formula contains call to a javascript function, the function will be invoked as well.

so, what's provided on linked discussion should be really all what you need.

Badge +3

Yes I see what you mean.

I altered the calculated field to read

If(MRMapType=="SSCC Report Map", ANNEXTextBox(),BlankTextBox())

and placed the functions under Settings > Custom Javascript

function BlankTextBox(){
   NWF$('#'+txtField).val("");
   return "";
}
 
function ANNEXTextBox(){
   NWF$('#'+txtField).val("ANNEX");
   return "";
}

Unfortunately changing the dropdown just blanked out the txtField.

I think there is problem with the javascript or how I am using it.

Badge +3

Hi All

OK here's what I did that seems to work.

1) Gave java IDs to the drop down and textbox as suggested earlier

2) Added the function below to the Settings > Custom JavaScript , as suggested earlier

3) Used the change event on the dropdown as suggested in the link provided earlier

4) Learnt a bit of javascript 

No need for calculated fields

Thanks for all the help, it is greatly appreciated 

NWF$ ("#" + mapDD).change(function()
{
   if(NWF$("#" + mapDD).val() == "SSCC Report Map")
      NWF$("#"+txtField).val("ANNEXURE A:");
   else
      NWF$("#"+txtField).val("");
});

Userlevel 5
Badge +14

glad you have a solution , despite is it's something different from what you've originally asked.

do not forget to mark the post that helped you the most as correct answer.

Badge +4

for example @Chris Ben

IF I want to put that calculated value in IF Statement , IF Statement will not work so we need to populate the calculated value to text box to do what we want in  IF Statement .

Reply