From f79a987f12379b15ab35b5a36933b3f78aeb7a51 Mon Sep 17 00:00:00 2001
From: "S.Listl" <S.Listl@SLISTL.aditosoftware.local>
Date: Wed, 2 Oct 2019 11:51:11 +0200
Subject: [PATCH] Attribute Filter fix

---
 .../dropdownfilter/valueProcess.js            | 20 +++++++++++++------
 process/Attribute_lib/process.js              |  8 ++++++--
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/entity/Attribute_entity/entityfields/dropdownfilter/valueProcess.js b/entity/Attribute_entity/entityfields/dropdownfilter/valueProcess.js
index 83bdb17d262..c040a5fb108 100644
--- a/entity/Attribute_entity/entityfields/dropdownfilter/valueProcess.js
+++ b/entity/Attribute_entity/entityfields/dropdownfilter/valueProcess.js
@@ -3,12 +3,20 @@ import("system.vars");
 import("system.result");
 import("Attribute_lib");
 
-var thisValueNotValid = "{\"entity\":\"\",\"filter\":{\"type\":\"group\",\"operator\":\"AND\",\"childs\":[]}}";
-
-if (vars.get("$field.DROPDOWNDEFINITION") == $AttributeTypes.OBJECTSELECTION 
-    && vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW && (vars.get("$this.value") == null || vars.get("$this.value") == thisValueNotValid)) 
+if (vars.get("$field.ATTRIBUTE_TYPE").trim() == $AttributeTypes.OBJECTSELECTION 
+    && (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW || vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW))
 {
     var entity = vars.getString("$field.DROPDOWNDEFINITION");
-    var condJson = JSON.stringify({entity: entity, filter: {type: "group", operator: "AND", childs: []}});
-    result.string(condJson);
+    var emptyFilter = {entity: entity, filter: {type: "group", operator: "AND", childs: []}};
+
+    var currentFilter = vars.get("$this.value");
+    if (currentFilter)
+        currentFilter = JSON.parse(currentFilter);
+
+    //if the filter field is empty or if the entity was changed, reset the filter
+    if (!vars.get("$this.value") || currentFilter.entity != emptyFilter.entity)
+    {
+        var condJson = JSON.stringify(emptyFilter);
+        result.string(condJson);
+    }
 }
\ No newline at end of file
diff --git a/process/Attribute_lib/process.js b/process/Attribute_lib/process.js
index f517199cb89..73a5f9a4ac9 100644
--- a/process/Attribute_lib/process.js
+++ b/process/Attribute_lib/process.js
@@ -183,8 +183,12 @@ AttributeUtil.getPossibleListValues = function (pAttributeId, pAttributeType, pI
             var config = entities.createConfigForLoadingRows()
                 .entity(module)
                 .fields([uid, title]);
-            if (filter && filter.filter)
-                config.filter(filter.filter);
+            if (filter)
+            {
+                filter = JSON.parse(filter);
+                if (filter.filter)
+                    config.filter(JSON.stringify(filter.filter));
+            }
             var rows = entities.getRows(config);
             for (let i = 0, l = rows.length; i < l; i++)
                 objects.push([rows[i][uid], rows[i][title]])
-- 
GitLab