Hi,
We have a problem with a related lookup in a grid. We have the following setup: Relation > Contacts
When selecting the a relation the overview of the contacts only displays the the contacts of the relation. Now when we manually type in the list of contacts all contacts of all relations are displayed. We expect that this list is also filtered. Is this a bug or is there another workaround for this?
Example:
Hi,
There is an existing thread that is related to this one, see here https://community.progress.com/community_groups/rollbase/f/25/p/14764/52879.aspx#52879
Alternative way to make it work by using "rbf_setLookupFilter".
1. Go to Configure grid and set the onUpdate with your function in my sample is filterLookup(@@);
2. Save. Apply rbf_setLookupFilter in the script component with sample code below. Change the following codes:
R111029 --> Parent lookup (Warehouse) in grid, inspect element to get the integration name.
R130928 --> Dependent lookup (Product) in grid, inspect element the lookup to view integration name. See image
R56036 -- > This is an integration name of parent (warehouse) from Product Object. In my test, go to Product object definition and get the value of warehouse lookup integration name.
<script> function filterLookup(rowIndex) { var parentId = rbf_getGridValue2(0, "R111029", rowIndex); console.log(parentId); if(parseInt(parentId) > 0) { rbf_setLookupFilter("R130928_0_"+rowIndex, "R56036", parentId); } else { rbf_setLookupFilter("R130928_0_"+rowIndex, "R56036", 1); //set to 1 for empty parent lookup } } </script>
* I added the else to handle empty parent lookup which is not in the previous thread.
Hope this may help.
Regards,
Orchid
Hi Aede, I've encountered this problem in the past as well and customized some Rollbase scripts so I could get this to work. Using this script you can get it work:
<script> function updateLookup(rowIndex) { var mainLookupId = "R116432077_0_" + rowIndex; var relationshipId = 116225885; var dependentLookupId = "R116432098_0_" + rowIndex; customLookupChange(mainLookupId, relationshipId, dependentLookupId); } function customLookupChange(mainLookupId, relationshipId, dependentLookupId) { var mainLookup = document.getElementById('' + mainLookupId); if (!mainLookup) { return; } var buff = rbf_getAjaxURL()+'&cmd=relatedObjects&relId='+relationshipId+'&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('' + dependentLookupId); 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; } var customLookupInitVal; </script>
All you must do now is replace "mainLookupId", "relationshipId" and the "dependentLookupId" variables with the corresponding IDs.Also, add updateLookup(@@); to your onCreate and onUpdate grid configuration!
Please let me know if this works for you. I would also appreciate it if you could upvote my idea, as I think this is a major issue with such a useful tool.
Hi matman!
I've voted on your idea! Thanks for the script, I've implemented but it doesn't work yet, i have some errors:
Uncaught TypeError: Cannot set property 'undefined' of undefined
ajaxRreq.onreadystatechange
Points to this line of code: dependentLookup.options[selLength] = newOpt;
Error appears when entering a relation (in the main lookup)
Hi Aede, are you sure the lookup Ids are correct? They may be in a reverse order.
Also, I edited the code a bit. Here is an updated version:
<script> function updateLookup(gridNo, rowIndex) { var mainLookupId = "R116432077"; var relationshipId = "116225885"; var dependentLookupId = "R116432098"; customLookupChange(gridNo, rowIndex, mainLookupId, relationshipId, dependentLookupId); } function customLookupChange(gridNo, rowIndex, mainLookupId, relationshipId, dependentLookupId) { var mainLookup = document.getElementById('' + mainLookupId + "_" + gridNo + "_" + rowIndex); if (!mainLookup) { return; } var buff = rbf_getAjaxURL()+'&cmd=relatedObjects&relId='+relationshipId+'&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('' + dependentLookupId + "_" + gridNo + "_" + rowIndex); 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; } var customLookupInitVal; </script>
Make sure to change updateLookup(@@); to updateLookup(##, @@);
If it still doesn't work, try switching "mainLookupId" and "relatedLookupId". Also, did you add a try-catch? If you did I should try that as well, perhaps you receive an error I've never encountered.
Hi,
There is an existing thread that is related to this one, see here https://community.progress.com/community_groups/rollbase/f/25/p/14764/52879.aspx#52879
Alternative way to make it work by using "rbf_setLookupFilter".
1. Go to Configure grid and set the onUpdate with your function in my sample is filterLookup(@@);
2. Save. Apply rbf_setLookupFilter in the script component with sample code below. Change the following codes:
R111029 --> Parent lookup (Warehouse) in grid, inspect element to get the integration name.
R130928 --> Dependent lookup (Product) in grid, inspect element the lookup to view integration name. See image
R56036 -- > This is an integration name of parent (warehouse) from Product Object. In my test, go to Product object definition and get the value of warehouse lookup integration name.
<script> function filterLookup(rowIndex) { var parentId = rbf_getGridValue2(0, "R111029", rowIndex); console.log(parentId); if(parseInt(parentId) > 0) { rbf_setLookupFilter("R130928_0_"+rowIndex, "R56036", parentId); } else { rbf_setLookupFilter("R130928_0_"+rowIndex, "R56036", 1); //set to 1 for empty parent lookup } } </script>
* I added the else to handle empty parent lookup which is not in the previous thread.
Hope this may help.
Regards,
Orchid
Thanks Orchid,
This works really nice! We were trying some different approaches but this is a very nice solution and easy to understand!
Regards,
Aede
That's great that it works for you!
Cheers,
Orchid