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:
- Create a Before Save Person trigger.
- 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.
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", "");
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
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.