Problems with lookup fields

Posted by matman on 21-Nov-2014 09:11

I am trying to implement something but there seem to be problems with lookup fields. For example, setFieldValue, setFieldContent don't seem to work no matter I'm using Selector, Picklist or Hidden. I also can't find a way to do the same with JQuery.

Also, when I add an onChange event, it doesn't trigger when the lookup field changes value. JQuery's change() also doesn't, so I had to use setInterval and then read and compare the old and new values.

Anyone can confirm this? It's preventing me from implementing an important functionality.

All Replies

Posted by Gian Torralba on 21-Nov-2014 09:46
Hello,

If you are using Google Chrome as your test browser. You can open the developer console to check if there is any error in your javascript code. The onchange functionality should work fine so try adding a function call to the onchange event and add an alert() or console.log() to check if it is working. You can set the look up field value by using rbf_setFieldValue but unfortunately, it will not show the link in the selector field.

Thank you,
Gian
Posted by matman on 21-Nov-2014 09:51

Hey Gia,

I've tested my onChange code on an ordinary textarea which worked fine using alert() and rbf_growlError(). But when I switch to the lookup field it fails. Both my own script component and Rollbase event show the same behaviour.

Is there any way to make the link appear in the Selector field? My linklookup doesn't work using setFieldValue(), it seems to require an update or something.

Thanks,

Matt

Posted by Gian Torralba on 21-Nov-2014 11:42

Hello,

I am using this simple function to the lookup selector and it is working fine. I also added the test() method in the onchange event of the lookup.

<script>
function test(){
alert('test');
}
</script>

Thank you,

Gian

Posted by matman on 24-Nov-2014 02:58

Hi Gian, using your way of implementing and code I managed to get it working. Do you happen to know why no change() events are fired? I was using the following code to catch them:

<script>
	$(function() {
		$("#R114854155").change(function() {
			alert("Changed");
		});
	});
</script>


Posted by Santosh Patel on 24-Nov-2014 03:19

Hi matman,

Hidden input fields don't fire a change event when their value changes. The general solution used elsewhere is to trigger a change event on the hidden field via jquery/js when  you know the value have changed. We will discuss and put this change for all lookup fields so that onchange could be used in such cases.

Regards,

Santosh

Posted by matman on 24-Nov-2014 03:23

Hi Santosh, thanks for your reply. I would like that :) For anyone who's interested as to how I implemented my code before Gian's suggestion, I used the following code:

<script>
	$(function() {
		var oldValue = "";
		var newValue = $('#R114854155').val();
		
		setInterval(function() {
			newValue = $('#R114854155').val();
			if(newValue != oldValue) {
				oldValue = newValue;
				
				alert("Changed");
			}
		}, 250);
	});
</script>

Posted by Santosh Patel on 24-Nov-2014 04:03

I looked through the related functions again and apparently we do handle onchange events for Reference fields, picklists and Org Data selectors but not lookups. Will fix this as a bug.

In the meanwhile you can use the following code and avoid using the setInterval solution.

// $('R114854155').attr("hidenChange", "<your_javascript_code_that_would_execute_when_onChange_would_have_fired");

$('R114854155').attr("hidenChange", "console.log($('#R114854155').val());");


Couple of things to note here...

- hidenChange is our implementation that is already in place but does not execute for Lookup fields. The registered onChange events on other fields (where this already works) translate to this event and is evaluated as mentioned below.

- the javascript code that is passed as 2nd argument will be executed using the eval() function.

- using 'this' reference in the 2nd argument will refer to the top level window object.

- hidenChange is not a typo, use it as is.

Posted by matman on 24-Nov-2014 04:37

Thanks for the workaround Santosh, I'll use your solution (I hate setInterval) :)

I also found a workaround for a problem I talked about earlier. This was about changing another lookup field and it's related field that should show records related to that particular lookup field record. I was able to realise this by using Rollbase's own code.

This also means that when Rollbase updates the code, you may need to update your code.

I need this functionality to have a better page design and to ease work. I have the following scenario: 2 customer fields and 2 contactperson fields. I only want to set 1 customer field, not 2 (too much work, I'm lazy). Contactperson is a related field so when I select a customer in the first field, I also need to update the second customer. This is possible using the following method.

On the page you want to have this, you add the following script:

<script>
	function customLookupChange() {
		var mainLookup = document.getElementById('R114854155');
		if (!mainLookup) { return; }
		var buff = rbf_getAjaxURL()+'&cmd=relatedObjects&relId=108244227&id='+mainLookup.value;
		var ajaxRreq=rbf_getXMLHTTPRequest();
		if (!ajaxRreq) return;
		ajaxRreq.onreadystatechange = function() {
			if (ajaxRreq.readyState != READY_STATE_COMPLETE) return;
			var resultStr = ajaxRreq.responseText;
			var resultArr = resultStr.split('\n');
			var dependentLookup = document.getElementById('R115394010_Website');
			if (!dependentLookup) { return; }
			var currValue = dependentLookup.value;
			while(dependentLookup.length > 1) { dependentLookup.options[1] = null; }
			if (dependentLookup.length > 0 && dependentLookup.options[0].value != '') { dependentLookup.options[0] = null; }
			var counter = 0;
			if (resultArr && resultArr.length >= 2) {
				var arrLen = resultArr.length;
				while (counter < arrLen) {
					var optionId = resultArr[counter++];
					if (counter>=arrLen) break;
					var optionName = resultArr[counter++];
					var newOpt = new Option(optionName, optionId);
					if (customLookupInitVal && customLookupInitVal.indexOf(optionId)>0) newOpt.selected=true;
					var selLength = dependentLookup.length;
					dependentLookup.options[selLength] = newOpt;
				}
			}
			if (customLookupInitVal) customLookupInitVal=null;
			else dependentLookup.value = currValue;
			customLookupInitVal=null;
			if (dependentLookup.onchange && currValue!=dependentLookup.value) dependentLookup.onchange();
		};
		ajaxRreq.open('GET', buff, true);
		ajaxRreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		ajaxRreq.send(null);
		return false;
	}
	
	rbf_addOnLoadMethod(customLookupChange);
	var customLookupInitVal=' null ';
</script>


Make sure to update mainLookup and dependentLookup with your field IDs!

Also, edit the onChange event of the Customer field you use to select a customer and use the following code:

try { customLookupChange(); } catch(e) { if (rbv_errorsCallback) rbv_errorsCallback(e.message, 'rbf_showDependentLookup'); } ; try { rbf_showDependentLookup115395162(); } catch(e) { if (rbv_errorsCallback) rbv_errorsCallback(e.message, 'rbf_showDependentLookup'); };

Although I don't think there's a lot of chance, I hope I could help somebody with this code!

 

Posted by Santosh Patel on 24-Nov-2014 05:20

I'm sure people will find your solution useful.

P.S: setInterval was what provoked me to look further and ended up finding the solution. I hate it too.

This thread is closed