Page 1 of 1

Copy values from lookup to single line of text

Posted: 30 May 2018
by vegard.grutle
Hi!

Is it possible to copy values from lookup to single line of text using forms designer?

Thansk!

Vegard

Re: Copy values from lookup to single line of text

Posted: 30 May 2018
by Nikita Kurguzov
Dear Vegard,
Of course it is! Very easy actually, just use the following code:

Code: Select all

var lookuptext = fd.field('Lookup').control('getSelectedText');
fd.field('Title').value(lookuptext);
You can place it on button click or on change event of the Lookup field, or even simply in the JS editor and it will run when the form opens if it has value in the lookup field.

Re: Copy values from lookup to single line of text

Posted: 30 May 2018
by vegard.grutle
Hi!

This worked really well! Could the text field also be updated on a change event when saving the New/Edit form?

Thanks!

Vegard

Re: Copy values from lookup to single line of text

Posted: 30 May 2018
by Nikita Kurguzov
Dear Vegard,
You mean on save? Sure, something like this will work:

Code: Select all

fd.onsubmit(function(){
  var lookuptext = fd.field('Lookup').control('getSelectedText');
  fd.field('Title').value(lookuptext);
  return true;
});

Re: Copy values from lookup to single line of text

Posted: 31 May 2018
by vegard.grutle
Many thanks!

This worked like a charm! Could the lookup name be changed to a css class somehow? I have a number of fields that needs to be copied into plain text. An example would be highly appriciated :)

Vegard

Re: Copy values from lookup to single line of text

Posted: 01 Jun 2018
by Nikita Kurguzov
Dear Vegard,
I am not sure in what way you want to save the results, but you can try something like this - it will save all lookup values into Title field:

Code: Select all

var lookups = $('[id*="Lookup"]').find(":selected");
var text = '';
for (var i = 0; i < lookups.length; i++){
    if (i < lookups.length - 1)
      text += $(lookups[i]).text() + ' - ';
    else
      text += $(lookups[i]).text();
}
fd.field('Title').value(text);

Re: Copy values from lookup to single line of text

Posted: 04 Jun 2018
by vegard.grutle
Hi,

I would like to save the lookup values into plain text fields e.g. "Lookup" field is copied into "Tex" field. "Lookup2" is copied into "Text2" field.

Example:

Code: Select all

fd.onsubmit(function(){
  var lookuptext = fd.field('Lookup').control('getSelectedText');
  fd.field('Text').value(lookuptext);
  
  var lookuptext = fd.field('Lookup2').control('getSelectedText');
  fd.field('Text2').value(lookuptext);
	return true;
}

);
I would like to use the CSS class instead of the field name. Then it will be easier to manage many fields. Here is an example using the CSS class instead of the field name when using a hide script. Is it possible to achieve something similar?

Code: Select all

$('.CSS-Class').hide();
Thansk!

Re: Copy values from lookup to single line of text

Posted: 04 Jun 2018
by Nikita Kurguzov
Dear Vegard,
The previous code I gave you works with all Lookups, but it can be reworked to work only with ones that have a specific class, that's not an issue at all. But we still need to know Internal Names of the Text fields, or at least the pattern that you follow, e.g. 'Text1', 'Text2', 'Text3' - it needs to be precise and consistent. Otherwise, how to tell which field to copy where? There needs to be some structure with field names.

The code I've given is enough to make it work, it shouldn't be too hard to make it work with a class, like this:

Code: Select all

var lookups = $('.lookup-class').find(":selected");
for (var i = 0; i < lookups.length; i++){
    var num = i + 1;
    var fieldName = 'Text' + num;
    fd.field(fieldName).value($(lookups[i]).text());
}