Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

how to achieve this in js ?

(0) ShareShare
ReportReport
Posted on by 40

3124.Screenshot-_2800_365_29005F00_LI.jpg


And one more condition u need to add
--when case field is visible make it mandatory 
else non mandatory
  • necsa Profile Picture
    necsa 3,455 on at
    RE: how to achieve this in js ?

    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.

    1263.pastedimage1619567685149v1.png

        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()

  • Ainee ahsan Profile Picture
    Ainee ahsan 40 on at
    RE: how to achieve this in js ?

    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

  • Ainee ahsan Profile Picture
    Ainee ahsan 40 on at
    RE: how to achieve this in js ?

    hi thank you for solving my query but i didnt get for what const is being used ??

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,321 on at
    RE: how to achieve this in js ?

    Hi,

    Thank you for your query.

    You can do this using Business Rules, here is the high level logic:

    • Add a Condition: two-options field is "Yes"
      • Add Set Visibility action and set Case lookup's visible
      • Add Set Business Required action and set the Case to business required.
    • When No (false:
      • Add Set Visibility action and set Case lookup's not visible.
      • Add Set Business Required action and set the Case to not required.

    Here is how Business Rule look like:

    2781.pastedimage1619515331160v2.png

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,735 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans