Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Js code to merge

Posted on by Microsoft Employee

Hi Team,

I have a requirement like 

eg: suppose there are two option set "a", "b" , if a is selected as yes then show  alert message message as "Hi crm". in the same way if b optionset is selected as yes then show same alert message "Hi crm". i want this in single function to be executed .

i want this to be executed on change of both the fields 

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Js code to merge

    thanks Hari Narayanan this worked for me chears

  • Suggested answer
    migsbeja Profile Picture
    migsbeja 655 on at
    RE: Js code to merge

    Hi Nithin.K

    You can do a function like below and then pass as parameter in the field the schema name of the field that the function is firing and the message that you want.

    pass_5F00_parameter.jpg

    function validateDateOnChange(string schemaName, string message) {

    try {

    if (Xrm.Page.context.client.getClient() == "Web" || Xrm.Page.context.client.getClient() == "Outlook") {

    var date = Xrm.Page.getAttribute(schemaName).getValue();

    if (date != null) {

    var currDate = new Date(new Date().getYear(), new Date().getMonth(), new Date().getDate());

    var fieldDate = new Date(date.getYear(), date.getMonth(), date.getDate());

    if (fieldDate< currDate) {

    Xrm.Page.getControl(schemaName).setNotification(message);

    }

    else {

    Xrm.Page.getControl(schemaName).clearNotification();

    }

    }

    }

    }

    catch (err) {

    alert(err.message.toString());

    }

    }

    Hope this helps

  • Hari Narayanan Profile Picture
    Hari Narayanan 589 on at
    RE: Js code to merge

    The attribute is retrieved from the execution context of the function. So this means when adding the event handler, you need to tick the box to ‘Pass execution context as first parameter’. You do not need to enter the field name into the parameters.

  • Verified answer
    Hari Narayanan Profile Picture
    Hari Narayanan 589 on at
    RE: Js code to merge

    function ValidateDateChagne(executionContext) {

       //Expected PO Date Validation(Expected PO Date Cannot be of past date).

       try {

           var attribute = executionContext.getEventSource();

           var fieldName = attribute.getName()

           if (Xrm.Page.context.client.getClient() == "Web" || Xrm.Page.context.client.getClient() == "Outlook") {

               if (fieldName == "ac_expectedpodate") {

                   var expectedPODate = Xrm.Page.getAttribute("ac_expectedpodate").getValue();

                   if (expectedPODate != null) {

                       var currDate = new Date(new Date().getYear(), new Date().getMonth(), new Date().getDate());

                       var expectedPODateVal = new Date(expectedPODate.getYear(), expectedPODate.getMonth(), expectedPODate.getDate());

                       if (expectedPODateVal < currDate) {

                           Xrm.Page.getControl("ac_expectedpodate").setNotification("Expected PO date cannot be of past date...");

                       } else {

                           Xrm.Page.getControl("ac_expectedpodate").clearNotification();

                       }

                   }

               } else if (fieldName == "ac_expectedresponsedate") {

                   var responseExpectedDate = Xrm.Page.getAttribute("ac_expectedresponsedate").getValue();

                   if (responseExpectedDate != null) {

                       var currDate = new Date();

                       var currDateVal = new Date(currDate.getYear(), currDate.getMonth(), currDate.getDate());

                       var responseExpectedDateVal = new Date(responseExpectedDate.getYear(), responseExpectedDate.getMonth(), responseExpectedDate.getDate());

                       if (responseExpectedDateVal < currDateVal) {

                           Xrm.Page.getControl("ac_expectedresponsedate").setNotification("Expected Response Date cannot be of past date...");

                       } else {

                           Xrm.Page.getControl("ac_expectedresponsedate").clearNotification();

                       }

                   }

               }

           }

       } catch (err) {

           alert(err.message.toString());

       }

    }

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Js code to merge

    this is my code and i dont want to use two functions and want to minimise it

    function validatePODateOnChange() {

       //Expected PO Date Validation(Expected PO Date Cannot be of past date).

       try {

           if (Xrm.Page.context.client.getClient() == "Web" || Xrm.Page.context.client.getClient() == "Outlook") {

               var expectedPODate = Xrm.Page.getAttribute("ac_expectedpodate").getValue();

               if (expectedPODate != null) {

                   var currDate = new Date(new Date().getYear(), new Date().getMonth(), new Date().getDate());

                   var expectedPODateVal = new Date(expectedPODate.getYear(), expectedPODate.getMonth(), expectedPODate.getDate());

                   if (expectedPODateVal < currDate) {

                       Xrm.Page.getControl("ac_expectedpodate").setNotification("Expected PO date cannot be of past date...");

                   }

                   else {

                       Xrm.Page.getControl("ac_expectedpodate").clearNotification();

                   }

               }

           }

       }

       catch (err) {

           alert(err.message.toString());

       }

    }

    function validateResponseExpectedDateOnChange() {

       //Expected Response Date(Expected Response Date cannot be of past date)

       try {

           if (Xrm.Page.context.client.getClient() == "Web" || Xrm.Page.context.client.getClient() == "Outlook") {

               var responseExpectedDate = Xrm.Page.getAttribute("ac_expectedresponsedate").getValue();

               if (responseExpectedDate != null) {

                   var currDate = new Date();

                   var currDateVal = new Date(currDate.getYear(), currDate.getMonth(), currDate.getDate());

                   var responseExpectedDateVal = new Date(responseExpectedDate.getYear(), responseExpectedDate.getMonth(), responseExpectedDate.getDate());

                   if (responseExpectedDateVal < currDateVal) {

                       Xrm.Page.getControl("ac_expectedresponsedate").setNotification("Expected Response Date cannot be of past date...");

                   }

                   else {

                       Xrm.Page.getControl("ac_expectedresponsedate").clearNotification();

                   }

               }

           }

       }

       catch (err) {

           alert(err.message.toString());

       }

    }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans