web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Business Rules AND Script

(0) ShareShare
ReportReport
Posted on by 10

Is it possible to have a business rule AND a script running off the same field? I can only seem to get 2013 to work with either one or the other.

eg. a business rule to hide specific fields and a script to hide specific tabs on the form layout according to a drop down chosen

*This post is locked for comments

I have the same question (0)
  • Royal King Profile Picture
    27,686 on at

    Yes you can have Business rule as well javascript on same field. Business rules only runs on onload and field onchange event. You can find more about Business rule and it's limitations here

    blogs.technet.com/.../crm-2013-understanding-portable-business-logic.aspx

  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Hi Hostis,

    Yes. It is possible.

    Business rule is not expected to replace the whole script and cannot be used to hide another component/element other than field.

    Here are some other limitations to using business rules:

    • Business rules run only when the form loads and when field values change. They do not run when a record is saved.

    • Business rules work only with fields. If you need to interact with other visible elements, such as tabs and sections, within the form you need use form scripts.

    • When you set a field value by using a business rule, any OnChange event handlers for that field will not run. This is to reduce the potential for a circular reference, which could lead to an infinite loop.

    • If a business rule references a field that is not present on a form, the rule will simply not run. There will be no error message.

    • Whole Number fields that use the formats for TimeZone, Duration, or Language will not appear in the rule editor for conditions or actions, so they cannot be used with business rules.

    • For Microsoft Dynamics CRM for tablets, the definition of the business rules are downloaded and cached when CRM for tablets opens. Changes made to business rules are not applied until CRM for tablets is closed and re-opened.

    • When you set the value of a lookup field, the text of the primary field value that is set in the form will always match the text that is visible in the rule definition. If the text representing the primary field value of the record you are setting in the lookup changes, the value set by your rule will continue to use the text portion of the primary field value defined by the rule. To fix this, update the rule definition to use the current primary name field value.

    But, it has its own order.

    The logic included in your business rules is applied together with other logic in the form that could include system scripts, custom scripts, and other business rules. The order in which this logic is applied will affect the outcome. The order is as follows:

    1. Any system scripts are applied first.

    2. Any logic in custom form scripts is applied.

    3. Logic in business rules is applied.

      When there are multiple business rules, they are applied in the order they were activated, from oldest to newest.

    This means that to control the order in which business rules are applied, you must deactivate and reactivate the ones you want to be applied last.

    Source: http://technet.microsoft.com/en-us/library/dn531086.aspx

    I have implemented both of them for the same field and it works.

    But, need to remember that it will only triggered when the field is changed by user, not by code, for example: you have business rules if field A is changed then change value of field B. (it is by Code, not by user key in), then you create Business Rules for field B as condition, it will not be triggered since field B is not 'changed'.

    Hope this helps!

    Thanks.

  • hostis_72 Profile Picture
    10 on at

    a brilliant response and thanks but I am still stuck. I have a field called Job Type with an option set. One option is CIP (1) and this script fires to hide tabs:

    function HideTab(TabName) {
        Xrm.Page.ui.tabs.get(TabName).setVisible(false);
    }

    function ShowTab(TabName) {
        Xrm.Page.ui.tabs.get(TabName).setVisible(true);
    }

    function ShowTabs() {
        var new_jobtype = Xrm.Page.getAttribute('new_jobtype').getValue();

        if (new_jobtype  == 1) {
            // CIP
            return;
        ShowTab('course');
        HideTab('apprentices');
        HideTab('candidate');
        }
    }

    and also a business rule for the CIP that hides fields in the main section:

    but the business rule is not firing. the script is OK and the tabs hide...

    Is there any reason for this as it seems to follow the logic of your brilliant reply? ....

    thanks

  • hostis_72 Profile Picture
    10 on at

    please see my other reply. I should have "replied" sorry

  • Aileen Gusni Profile Picture
    44,524 on at

    Hi Hostis,

    Are those fields exist in the same Tab that you want to hide?

  • hostis_72 Profile Picture
    10 on at

    no, its the first field on the main section.

  • Aileen Gusni Profile Picture
    44,524 on at

    Okay, now try to disable the script, the business rule works or not

  • Suggested answer
    Hosk Profile Picture
    on at

    best practices would be to either have business rules or javascript running on a field because you can end up writing conflicting business rules/javascript.  It is also confusing to maintain the solution.

    personally I would advise you to use one or the other because you will also have to remember the order of Javascript and business rules working, which adds more complexity to maintaining the solution

  • Community Member Profile Picture
    on at

    Great post and responses : both Ben and Aileen.

  • Nuno Profile Picture
    2,005 on at

    Hi Hostis,

    Is anything OK with the function ShowTabs()? You have a return just after the if condtion.

    You will never get the functions ShowTab('course'), HideTab('apprentices') and HideTab('candidate') called.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans