I have 2 environments: DEV & TEST. I want to achieve functionality as mentioned as below:
if (fields.email.getValue() == null) {
fields.emailAllowed.setValue(null);
fields.emailAllowed.setRequiredLevel("required");
}
else {
fields.emailAllowed.setRequiredLevel("none");
}
if (fields.phone.getValue() == null) {
fields.phoneAllowed.setValue(null);
fields.phoneAllowed.setRequiredLevel("required");
}
else {
fields.phoneAllowed.setRequiredLevel("none");
}
This JavaScript code is working fine in DEV environment but due to some unknown reason it is NOT saving the NULL value in TEST environment.
To support that, I have added a new CRM plugin in which I have created two handlers:
Create Plugin ==> PreValidation
Update Plugin ==> PreValidation
In both of these plugins, I am using logic as below:
// Gets PreImage & Target Image combined
public void Execute()
{
var combinedTarget = GetCombinedTarget();
if (combinedTarget == null)
{
// log & throw error
}
MapEmailField(combinedTarget);
MapPhoneField(combinedTarget);
}
private void MapEmailField(combinedTarget)
{
// if Email is NULL and EmailAllowed IS NULL
if(combinedTarget.Metadata.Attributes.Email != null && combinedTarget.Metadata.Attributes.EmailAllowed == null)
{
//log & throw error
}
else if(combinedTarget.Metadata.Attributes.Email == null)
{
combinedTarget.Metadata.Attributes.EmailAllowed = null; // To save NULL value into CRM field
}
}
// same method for phone field mapping
// save the record
Now in CRM UI, when I update email field, it automatically sets TRUE value to Phone Allowed field too, even though it is not changed and I have externally set it NULL in CRM plugin. And there are 5 more such fields are getting affected in same way.
The same changes are working fine in DEV environment but not in TEST environment.
NOTE: I have cross verified that all changes of DEV environment are moved into managed solution and it is there in TEST environment.
UPDATE 1:
I tried today debugging the JavaScript change in TEST environment, and through that it updated the records correctly. But when I closed the debugger and test the same scenario with new record, it updated all records again!!
UPDATE 2:
As my JavaScript function is getting called on onLoad event too so that it can set NULL value, but looks like it is not able to find the control or something and that's the reason it is working while debugging but not in regular mode.
Below is the Javascript function which gets executed onLoad event:
function handleAllowedFields(dataField, allowedField) {
return function () {
if (dataField == null || allowedField == null) {
return;
}
if (dataField.getValue() == null) {
allowedField.setValue(null);
allowedField.setRequiredLevel("none");
allowedField.controls.forEach(function (c) {
c.setDisabled(true);
});
} else {
// If dataField is having a value, then contact preference fields
// can have an value as Allow OR DoNotAllow ONLY.
allowedField.controls.forEach(function (c) {
c.setDisabled(false);
});
allowedField.setRequiredLevel("required");
}
}
}
Any help that how can I make my function to wait till all controls gets loaded on the form in onLoad event?
Found the issue.
It's true that Managed solution was unable to save the NULL value for two option type
field. While it was able to do that in unmanaged solution.
Alongside with that, I found that onLoad event was not executed after onSave event in JavaScript and this was the reason behind the issue.
Hence, what I did as a solution is I added an onChange event to modifiedon
field on the form and it is working like butter.
P.S.: I'm not using OptionSet
instead of TwoOptionSet
field is so that the default fields gets utilised properly by the customer for every other built in functionality.