Copy values from lookup to single line of text

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
User avatar
vegard.grutle
Posts: 59
Joined: Fri Mar 17, 2017

30 May 2018

Hi!

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

Thansk!

Vegard

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

30 May 2018

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.
Cheers

User avatar
vegard.grutle
Posts: 59
Joined: Fri Mar 17, 2017

30 May 2018

Hi!

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

Thanks!

Vegard

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

30 May 2018

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;
});
Cheers

User avatar
vegard.grutle
Posts: 59
Joined: Fri Mar 17, 2017

31 May 2018

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

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

01 Jun 2018

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);
Cheers

User avatar
vegard.grutle
Posts: 59
Joined: Fri Mar 17, 2017

04 Jun 2018

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!

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

04 Jun 2018

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());
}
Cheers

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 14 guests