Answered by:
Triggering CRM Required field Form Validation

Question
-
Hi All
I want to find out if there is a way to Programmatically Trigger CRM Form Validator function which validates for Required field before the Save Event.
Any help would be much appreciated.
Thanks
Dkay
Thursday, February 9, 2012 2:39 PM
Answers
-
It quite easy , I figure out by decipering crm internal functionality
its one liner ... Makes me laugh
All you have to do is call the inbuild function which CRM uses to validate when "Save or
"SaveandClose" button is Clicked
All you have to do is crmForm.IsValid() ; and the function and this will do the trick
dkay
- Marked as answer by D kay Friday, February 10, 2012 12:05 AM
Friday, February 10, 2012 12:05 AM
All replies
-
Hi,
The following jscript can be added to get field validation.
var oAttr = Xrm.Page.getAttribute(“fieldname”); if (oAttr != null) oAttr.setRequiredLevel("required");
Nabanita Majumdar
- Proposed as answer by Jim Daly [MSFT]Microsoft employee, Editor Wednesday, May 16, 2012 7:38 PM
- Unproposed as answer by Jim Daly [MSFT]Microsoft employee, Editor Wednesday, May 16, 2012 8:00 PM
Thursday, February 9, 2012 11:38 PM -
It quite easy , I figure out by decipering crm internal functionality
its one liner ... Makes me laugh
All you have to do is call the inbuild function which CRM uses to validate when "Save or
"SaveandClose" button is Clicked
All you have to do is crmForm.IsValid() ; and the function and this will do the trick
dkay
- Marked as answer by D kay Friday, February 10, 2012 12:05 AM
Friday, February 10, 2012 12:05 AM -
The danger with using undocumented internal methods is that they change without notice.
In this case, the crmForm.IsValid method will not be in the next release and any code using this method will be broken.
Anyone who followed this advice will also have broken code.
Best to use the documented methods. I'd recommend something like this:
var requiredAttributesWithNullValues = []; Xrm.Page.data.entity.attributes.forEach( function (attribute, index) { if (attribute.getValue() == null && attribute.getRequiredLevel() == "required") { requiredAttributesWithNullValues.push(attribute); } } ); if (requiredAttributesWithNullValues.length > 0) { var message = "The following fields cannot be null:\n"; for (var i = 0; i < requiredAttributesWithNullValues.length; i++) { message += requiredAttributesWithNullValues[i].controls.get(0).getLabel() + "\n"; } alert(message); }
Jim Daly Technical Writer Microsoft Dynamics CRM
- Edited by Jim Daly [MSFT]Microsoft employee, Editor Wednesday, May 16, 2012 7:55 PM updated sample to show message
- Proposed as answer by Jim Daly [MSFT]Microsoft employee, Editor Wednesday, May 16, 2012 7:55 PM
Wednesday, May 16, 2012 7:49 PMAnswerer -
Hallo Jim,
you are my last hope :-)) .
When a user wants to deactivate a record in crm 4.0 an "required field" alert pops up. As the record has to be deactivated it is not important whether the data are corect or not. Does there exist a possibility to avoid this alert with javascript ?
Regards hpm
hpmx
Tuesday, February 26, 2013 7:54 AM -
- What entity is it?
- What field is it?
- What kind of code is in the OnSave event?
If you disable the code in the OnSave event, does the required field alert still pop up?
In CRM 4.0 there was no supported way to set the RequiredLevel, but you could read it.
I suspect that there may be code in the form that is using an unsupported method to set the required level.
Finally, keep in mind that the OnSave event has an event.Mode value where 5 indicates the save is due to deactivating the record. See http://msdn.microsoft.com/en-us/library/cc150868.aspx
FYI - I haven't looked at CRM 4.0 in nearly 5 years and I don't have a test environment. So I hope this helps.
Jim Daly Technical Writer Microsoft Dynamics CRM
Tuesday, February 26, 2013 4:39 PMAnswerer -
Hi Jim,
my JS-Code is the following (entity is account):
Inside of the Save-Event:
ifunction checkInaktivierung() {
if (event.Mode == DEACTIVATE) {crmForm.SetFieldReqLevel("ad_netzathletenmanagerid", 0); // too late :-((
.....}
But the required-field-event pops up before this js code has been reached.
Any hint?
hpmx
Wednesday, February 27, 2013 1:56 PM -
1. Use of crmForm.SetFieldReqLevel isn't supported. I don't even know what the signature for that function is, but I assume you are trying to set it to be not required.
2. event.Mode == DEACTIVATE will only work if somewhere else you have set :
var DEACTIVATE = 5;
I'd recommend you just change it to :
if(event.Mode == 5){...
Jim Daly Technical Writer Microsoft Dynamics CRM
Wednesday, February 27, 2013 4:10 PMAnswerer -
Of course at the beginning of the script file:
var DEACITVATE =5;
I presume that there is no way to avoid the required field popup, if a user wants to deactivate an account :-((
Nevertheless thank you for your effort.
hpm
hpmx
Wednesday, February 27, 2013 4:46 PM