There is some reason for this error.
First, please make sure that you check the “Pass execution context as first parameter” checkbox in the event handler properties.
If the field is added in header/footer sections or BPF stages then it will be renamed like header_name
Check if it’s hidden or added multiple times in the form & use the browser developer toolbar to inspect the DOM
You can validate like if(formContext.getAttribute("name") != null before accessing getValue()
function setCaseVisibility(executionContext) { const formContext = executionContext.getFormContext(); var MaxcareID = formContext.getAttribute("mitcrm_maxcareid").getValue(); // 1 const caseLookupAttr = formContext.getAttribute("new_case"); if (MaxcareID===1) { caseLookupAttr.setVisible(true); caseLookupAttr.setRequiredLevel(true); } else { caseLookupAttr.setRequiredLevel(false); caseLookupAttr.setVisible(false); } }
i have written the code but when i am testing it is throwing me an error
TypeError: Cannot read property 'getValue' of null
at setCaseVisibility (mercuriusqa.crm4.dynamics.com/.../mitcrm_TimeentryJS:6:66)
at S._executeFunctionInternal (mercuriusqa.crm4.dynamics.com/.../app.js
at S.execute (mercuriusqa.crm4.dynamics.com/.../app.js
at mercuriusqa.crm4.dynamics.com/.../app.js
at i (mercuriusqa.crm4.dynamics.com/.../app.js
at K._executeIndividualEvent (mercuriusqa.crm4.dynamics.com/.../app.js
at K._executeEventHandler (mercuriusqa.crm4.dynamics.com/.../app.js
at Object.execute (mercuriusqa.crm4.dynamics.com/.../app.js
at j._executeSyncAction (mercuriusqa.crm4.dynamics.com/.../app.js
at j._executeSync (mercuriusqa.crm4.dynamics.com/.../app.js
Error Details:
Event Name: onload
Function Name: setCaseVisibility
Web Resource Name: mitcrm_TimeentryJS
Solution Name: Active
Publisher Name: DefaultPublishermercuriusit
hi thank you for solving my query but i didnt get for what const is being used ??
Hi,
Thank you for your query.
You can do this using Business Rules, here is the high level logic:
Here is how Business Rule look like:
Make sure to set the Business Rule scope as per your requirement (select the forms you use).
Activate the Business Rule.
For the Case lookup filter, open the Case lookup properties on the form and select only related Cases based on the relationship.
Dynamics 365: Lookup Field Filtering - The Marks Group | Small Business Consulting | CRM Consultancy
For your reference, you can also use Javascript (if you need to for any reason):
function setCaseVisibility(executionContext) { const formContext = executionContext.getFormContext(); const twoOptionsAttribute = executionContext.getEventSource(); // Get Selected value (Yes/No) const selectedOption = twoOptionsAttribute.getValue(); const caseLookupAttr = formContext.getAttribute("new_case"); if (selectedOption) { caseLookupAttr.setVisible(true); caseLookupAttr.setRequiredLevel(true); } else { caseLookupAttr.setRequiredLevel(false); caseLookupAttr.setVisible(false); } }
You can register above code on-change of the two-options field, do not forget to pass execution context as first paramter.
André Arnaud de Cal... 291,735 Super User 2024 Season 2
Martin Dráb 230,466 Most Valuable Professional
nmaenpaa 101,156