cancel
Showing results for 
Search instead for 
Did you mean: 
lucylee7
Nintex Newbie

How to set default for a lookup column using jquery in nintex

I was wondering anyone could shed some lights on this.

I want to set default to EN ENGLISH on my list lookup (Language) as below but its editable.

JQuery

NWF$(document).ready(function() {   
 
  //setLookupField(DetailLanguage, 'EN ENGLISH');
}); NWF.FormFiller.Events.RegisterAfterReady(function () {
 
 if (DetailLanguage == "Please select a value..."){
 
 setLookupField(DetailLanguage, 'EN ENGLISH');
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},1000);
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},2000);
 }else{
  setLookupField(DetailLanguage, 'EN ENGLISH');
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},1000);
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},2000);
  
 }});
function setLookupField(lookupField, lookupValue) {
 var lookupID = lookupField.substring(0, lookupField.length-4);
 var selectField = NWF$("#" + lookupID + " > option");
 
 for (var i = 0; i < selectField.length; i++){
  var option = selectField;
  if(option.text == lookupValue){
   NWF$("#" + lookupID).val(option.value);
   NWF$("#" + lookupField).val(option.getAttribute("data-nfchoicevalue"));
   NWF$("#" + lookupField).trigger("change");
   NWF$("#" + lookupField).trigger("blur");
   break;
  }
 }
}

0 Kudos
Reply
13 Replies
Highlighted
cju
Nintex Newbie

Re: How to set default for a lookup column using jquery in nintex

In O365, you can use this script to update a list lookup value:

var valueText = "EN ENGLISH";

var yourControl = NWF$("#"+yourControlId);

var value = yourControl.find("option[title='" + valueText + "']").attr("value");

yourControl.val(value);

yourControl.parent().find("input").val(valueText + "#--#" + value);

NWF$(NWF$(this).parent().find("input")[1]).val(value);

Hope this works

0 Kudos
Reply
lucylee7
Nintex Newbie

Re: How to set default for a lookup column using jquery in nintex

May I know what is [1] referring to?

0 Kudos
Reply
lucylee7
Nintex Newbie

Re: How to set default for a lookup column using jquery in nintex

where do i put this?

0 Kudos
Reply
lucylee7
Nintex Newbie

Re: How to set default for a lookup column using jquery in nintex

Hi Caroline Jung

it's working do you mind explaining these lines?

var valueText = "EN ENGLISH"; // default value which i needed

var yourControl = NWF$("#"+yourControlId); // jquery name of the above item

var value = yourControl.find("option[title='" + valueText + "']").attr("value"); // set var value to en english in option list lookup

yourControl.val(value); //set en english to yourControl

yourControl.parent().find("input").val(valueText + "#--#" + value);  //?

NWF$(NWF$(this).parent().find("input")[1]).val(value);  //? why the [1] ? is it the list item id of the particular item ?

0 Kudos
Reply
cju
Nintex Newbie

Re: How to set default for a lookup column using jquery in nintex

Hi, 

Sorry for the delay, here are explainations:

var valueText = "EN ENGLISH"; // Setting the title of the option to select in the list lookup field

var yourControl = NWF$("#"+yourControlId); // Getting the list lookup control on the form

var value = yourControl.find("option[title='" + valueText + "']").attr("value"); // Getting the value of the option to select in the list lookup

yourControl.val(value); // Setting the value of the list lookup control

yourControl.parent().find("input").val(valueText + "#--#" + value);  // As setting the value of the list lookup control is not enough to be taken into account by the Nintex Forms, another value has to be set:it's the value of an hidden input which should have a specific format

NWF$(NWF$(this).parent().find("input")[1]).val(value); // This is a mistake: you don't need to add this line

Don't hesitate to ask if it's not clear

0 Kudos
Reply
lucylee7
Nintex Newbie

Re: How to set default for a lookup column using jquery in nintex

Caroline Jung

Hi,

I dont know why suddenly the default to en english isnt working

its triggering from this line

yourControl.parent().find("input").val(valueText + "#--#" + value);  

0 Kudos
Reply
cju
Nintex Newbie

Re: How to set default for a lookup column using jquery in nintex

In the list lookup, does the option with this title still exist "EN ENGLISH"?

It doesn't seem to find the value corresponding to the title "EN ENGLISH".

If you're executing this script when the form loads, I advice you to use this script to be sure that it's executed when the list lookup has been loaded (maybe your script is executing when the list lookup is not loaded and doesn't have any option):

NWF$(document).ready(function() {

setDefaultValue();

});

 

function setDefaultValue(){

var valueText = "EN ENGLISH";

var yourControl = NWF$("#"+yourControlId);

if(yourControl.parent().find(".nf-lookup-loading").length > 0 && yourControl.parent().find(".nf-lookup-loading")[0].style.display != "none") {

setTimeout(setDefaultValue,200);

} else {

var value = yourControl.find("option[title='" + valueText + "']").attr("value");

yourControl.val(value);

yourControl.parent().find("input").val(valueText + "#--#" + value);

}

}

Hope this helps

0 Kudos
Reply
RaviACC
Nintex Newbie

Re: How to set default for a lookup column using jquery in nintex

worked perfectly for me.
0 Kudos
Reply
freerider38
Nintex Newbie

Re: How to set default for a lookup column using jquery in nintex

If you know the ID of your element, you can do this :

var control = ko.dataFor(NWF$("#" + varMyControlID)[0]);
control.initialValue  = "X";

where X is the ID of your element in the list

Reply