From b5e1fa7a16200785303074e2b27fdfb69a68e228 Mon Sep 17 00:00:00 2001
From: Benjamin Ulrich <b.ulrich@adito.de>
Date: Mon, 25 May 2020 12:11:50 +0000
Subject: [PATCH] [Projekt: Entwicklung - Neon][TicketNr.: 1057842][Wechsel
 Kontakt im Angebot]

---
 entity/Offer_entity/Offer_entity.aod                 |  1 -
 .../entityfields/chosenaddress/valueProcess.js       |  2 +-
 .../entityfields/contact_id/onValueChange.js         | 12 ++++++++++++
 .../entityfields/lettersalutation/valueProcess.js    | 12 ------------
 4 files changed, 13 insertions(+), 14 deletions(-)
 delete mode 100644 entity/Offer_entity/entityfields/lettersalutation/valueProcess.js

diff --git a/entity/Offer_entity/Offer_entity.aod b/entity/Offer_entity/Offer_entity.aod
index 0f57da59c1..18b73ee9d7 100644
--- a/entity/Offer_entity/Offer_entity.aod
+++ b/entity/Offer_entity/Offer_entity.aod
@@ -930,7 +930,6 @@
       <name>LETTERSALUTATION</name>
       <title>Letter salutation</title>
       <stateProcess>%aditoprj%/entity/Offer_entity/entityfields/lettersalutation/stateProcess.js</stateProcess>
-      <valueProcess>%aditoprj%/entity/Offer_entity/entityfields/lettersalutation/valueProcess.js</valueProcess>
     </entityField>
     <entityActionField>
       <name>openAdminView</name>
diff --git a/entity/Offer_entity/entityfields/chosenaddress/valueProcess.js b/entity/Offer_entity/entityfields/chosenaddress/valueProcess.js
index 6ebcf9a49b..f2e8d829a6 100644
--- a/entity/Offer_entity/entityfields/chosenaddress/valueProcess.js
+++ b/entity/Offer_entity/entityfields/chosenaddress/valueProcess.js
@@ -3,7 +3,7 @@ import("system.result");
 import("system.neon");
 import("system.vars");
 
-if ((vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW || vars.get("$sys.recordstate") == neon.OPERATINGSTATE_EDIT) && !vars.get("$field.ADDRESS"))
+if ((vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW || vars.get("$sys.recordstate") == neon.OPERATINGSTATE_EDIT))
 {
     var defaultAddressId = ContactUtils.getDefaultAddressId(vars.get("$field.CONTACT_ID"));
     if (defaultAddressId)
diff --git a/entity/Offer_entity/entityfields/contact_id/onValueChange.js b/entity/Offer_entity/entityfields/contact_id/onValueChange.js
index 4d3ecbb18e..74fa8e077b 100644
--- a/entity/Offer_entity/entityfields/contact_id/onValueChange.js
+++ b/entity/Offer_entity/entityfields/contact_id/onValueChange.js
@@ -1,3 +1,4 @@
+import("PostalAddress_lib");
 import("Sql_lib");
 import("system.neon");
 import("system.vars");
@@ -17,6 +18,17 @@ if(contactid != "")
     if(lang != "") 
         neon.setFieldValue("field.ISOLANGUAGE", lang);
     
+    var defaultAddressId = ContactUtils.getDefaultAddressId(contactid);
+    if (defaultAddressId)
+    {
+        neon.setFieldValue("field.ChosenAddress", defaultAddressId);
+            neon.setFieldValue("$field.ADDRESS", AddressUtils.getAddressById(defaultAddressId));
+    }
+    var addrobj = new AddrObject(contactid);
+    var salutation = addrobj.getFormattedAddress(false, "{letter_salutation},");
+    if (salutation != "Err.,")
+        neon.setFieldValue("field.LETTERSALUTATION", salutation);
+    
     // set $field.CONTATCT_ORG_ID per contactid
     var orgid = ContactUtils.getPersOrgIds(contactid);
     neon.setFieldValue("$field.CONTACT_ORG_ID", orgid[2]);
diff --git a/entity/Offer_entity/entityfields/lettersalutation/valueProcess.js b/entity/Offer_entity/entityfields/lettersalutation/valueProcess.js
deleted file mode 100644
index c3b7838688..0000000000
--- a/entity/Offer_entity/entityfields/lettersalutation/valueProcess.js
+++ /dev/null
@@ -1,12 +0,0 @@
-import("system.result");
-import("system.neon");
-import("system.vars");
-import("Address_lib");
-
-if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW && vars.get("$this.value") == null) 
-{
-    var addrobj = new AddrObject(vars.get("$field.CONTACT_ID"));
-    var salutation = addrobj.getFormattedAddress(false, "{letter_salutation},");
-    if (salutation != "Err.,")
-        result.string(salutation);
-}
\ No newline at end of file
-- 
GitLab