NoMailing changes made by a Before Save Person trigger are not reflected in the UI after save

lock
push_pin
done
Beantwortet
3

 

Expected:
Changing NoMailing through a Before Save Person trigger should update both the stored data and the UI immediately after saving.

Actual:
Changing NoMailing through a Before Save Person trigger updates the value in the database/backend, but the UI continues to display the previous value until the record is reloaded.

P.S.: I also tried updating the consent information from the same
Before Save Person trigger.

Updating LegalBaseId, LegalBaseKey and LegalBaseName to the
withdrawn values did not change the observed behaviour either.

SuperOffice_noMailing_Recording.zip 
Steps to reproduce:

  1. Create a Before Save Person trigger.
  2. In the trigger, set the following script. maybe change your sTestPersonId:
#setLanguageLevel 4;

String sTestPersonId = "212";

EventData ed = getEventData();

String sPersonId = ed.getInputValue("ActivePersonId");

if (sPersonId != sTestPersonId)
    return;

String sFirstName = ed.getInputValue("PersonEntity.Firstname");
String sLastName = ed.getInputValue("PersonEntity.Lastname");
String sNoMailing = ed.getInputValue("PersonEntity.NoMailing");

// 1. Works with Lastname.
// UI reflects the modified value immediately after Save.
if (!sLastName.endsWith(" Test"))
{
    ed.setOutputValue("PersonEntity.Lastname", sLastName + " Test");
}

// 2. works not with nomailing
// searchagent does reflect the new value aftrer the Person Save
// but the UI does not.
// thus, if someone changes the Entity again in the UI, this gets toggeld again,
// and it becomes a mess really fast
if (sNoMailing == "false")
{
    ed.setOutputValue("PersonEntity.NoMailing", "true");
}
else if (sNoMailing == "true")
{
    ed.setOutputValue("PersonEntity.NoMailing", "false");
}

// But perhaps you'll find another way.

11. Nov. 2026 | 02:42 PM

Alle Antworten (3)

Not sure what you use case is here, but NoMailing is an legacy/obsolete field from before consent management was introduced. If you want to change the persons's consent setting you can use the SetConsent function of the PersonAgent in a after save trigger event.

Example;

NSPersonAgent personAgent;

personAgent.SetConsent(personId, "EMARKETING", "USER", "61F", "");
11. Nov. 2026 | 02:59 PM

Hi David, thank you for your reply.

I am not sure excactly, but i think the personAgent.SetConsent
will be overwritten through the person save itself.
We are in the Before Save Event after all.

One can modify the eventData, which we do and the consent becomes Withdrawn for example, 
but one has to refresh the UI manually. If someone change the email, saves and changes another field,
the UI Updates the noMailing back, which triggers the Consent to be changed as well.



The use case is that some customers have multiple persons with the same e-mail, and if one of those persons
changes the e-mail (one and only) we are supposed to withdraw the consent and disable the E-marketing Person-Interest.

There are other possibilities (like the privacy confermation email) to change that also.



We do indeed not only try to set the NoMailing field, but also are modyfiing the Consent for the on person change event, something like this for Withdrawing our own Consent.
ed.setOutputValue("PersonEntity.Consents[" + idx.toString() + "].LegalBaseKey", "WITHDRAW");

this works, kindof, because the person has the correct data when refreshing. but the UI data is stale, which is a issue.

18 h, 41 m vor | 04:36 PM

Hi Mathias,

We had a similar issue with setting consent.  We wanted to add consent when a contact was created, which you could do with the personAgent.SetConsent(personId, "EMARKETING", "USER", "61F", ""), but we also wanted to set the contact consent to withdrawn if the contact was made a former employee, however we found that the personAgent.SetConsent only seemed to work if the consent record was not already present.  If the record was already present then it would not update the consent record, or if it did update it, the change was not reflected in the SuperOffice interface (it was a long time ago that we came across the issue, so I cannot quite remember).

However we solved our problem writing this script below.

#setLanguageLevel 4;

Integer personId = getVariable("PrimaryKey").toInteger();
String eventCalled = getVariable("Event").stripLeadingAndTrailing(" ");

//If New Contact
if (eventCalled == "person.created")
{
    NSPersonAgent pAgent;
    NSPersonEntity person = pAgent.GetPersonEntity(personId);

    //Set E-marketing Consent To "Legitimate interest"
    pAgent.SetConsent(personId, "EMARKETING", "USER", "61F", "Updated by script - Auto opted in to E-marketing.");

    //Check All Subscriptions
    NSSelectableMDOListItem[] shipmentTypes = person.GetShipmentTypes();
    for(Integer i = 0; i < shipmentTypes.length(); i++)
    {
        shipmentTypes[i].SetSelected(true);
    }

    //Save Subscriptions And Contact
    person.SetShipmentTypes(shipmentTypes);
    pAgent.SavePersonEntity(person);
}
else if (eventCalled == "person.changed")
{
    NSPersonAgent pAgent;
    NSPersonEntity person = pAgent.GetPersonEntity(personId);

    //If Contact Found
    if (person.GetPersonId() > 0)
    {
        //If A Former Employee
        if (person.GetRetired())
        {
            Integer currentConsentRecord = 0;
            Integer consentRecordLegalBasis = 0;

            NSConsentInfo[] consentInfo;
            NSConsentPerson consentPerson;
            NSLegalBase legalBase;
            NSConsentSource consentSource;

            //Set Legal Base To Unselected            
            legalBase.SetLegalBaseId(7);
            consentSource.SetConsentSourceId(8);

            //See If Contact Already Has An E-Marketing Consent Record
            //"personAgent.SetConsent" Only Works If Consent Record Is Not Already Present
            consentInfo = pAgent.GetConsentInfos(personId);

            //Loop Through All Consent Records
            for (Integer i = 0; i < consentInfo.length(); i++)
            {
                //If Consent Record Is The Consent Record For E-Marketing
                if (consentInfo[i].GetConsentPurposeId() == 2)
                {                    
                    //Store The Consent Record
                    currentConsentRecord = consentInfo[i].GetConsentPersonId();
                    consentRecordLegalBasis = consentInfo[i].GetLegalBaseId();

                    //If Consent Record Is Found And Not Equal To "Withdrawn" Update It
                    if (currentConsentRecord > 0 && consentRecordLegalBasis != 7)
                    {
                        //Update Consent Record
                        consentPerson = pAgent.GetConsentPerson(currentConsentRecord);
                        consentPerson.SetLegalBase(legalBase);
                        consentPerson.SetConsentSource(consentSource);
                        consentPerson.SetComment("Updated by script - Removed opt in to E-marketing due to contact being a former employee.");
                        pAgent.SaveConsentPerson(consentPerson);
                    }

                    break;
                }
            }            
        }
    }
}

Hopefully you can adjust this to meet the requirements you have on setting the consent record.  This was done using web hooks, so that if the customer is created/updated but not via the main contact dialog, I.E. a contact is added from a SuperOffice form, the script still fires.  But if you use the standard scripting events you do need to use the after save event, as this call updates the consentperson record, not the person record, so the contact should be saved before the script runs.

Hope this helps,

Trevor

18 h, 4 m vor | 05:12 PM

Hello Trevor,

Thank you for your suggestion, I really appreciate that.

Since I am working on the email_changes I kindof need to have both the old and new email,
and thus need to use the before_save trigger.

Maybe I try to save the last email in a custom field on before_save and do the sctual work
(find and update all clones, set consent etc) in the after_save trigger.

Not sure if that would change the issue.
Maybe I try to change consent through agent in after save and see if the UI responds.

For newsletters and webhooks the consent change works fine.
(we just use searchagent and update the table values)

It is indeed the UI not updating what gives me headaches.

Workflow 
1. Associate changes the email of a person saves

2. trigger -> Consent withdrawn in backend but not UI

3. Then, staying in that person also changes some other field, lets say address house number,
   saves again. -> no email change so our trigger does nothing, but

During that second save, the UI appears to submit stale entity data,
causing the consent value to be changed again.
My best guess is that the No Mailing toggle is not being visually refreshed after the first save.

If I press F5 in between, everything works as expected.
(Because the 'withdrawn consent' and the nomailing toggle are visualized properly).

Thanks again
2 h, 28 m vor | 08:49 AM

Hi Mathias,

The script does update the GUI for us, when we run it via the web hook (which is the equivelent of the After Save event).  But we also did have the same issue when we just used the pAgent.SetConsent function.

If you need to get the old email address into the After Save script, just create a Before save script to get the email address and store it in a state value.

ev.setStateValue("EmailsAddress", eMailAddress);
 
Then in the After Save script retrieve the email address from the state value.
 
eMailAddress = ev.getStateValue("EmailsAddress")
 
Trevor
1 h, 31 m vor | 09:45 AM

Antwort hinzufügen