Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Suggested answer

Show custom button based on the value of a field date in contact form

(0) ShareShare
ReportReport
Posted on by 235

Hello CS Experts,

I have a custom button on the form and I want to make it visible only when a date field has a value.

Initially, the date field is set to blank. After sometime, the record will be updated and a value for the date field will be supplied.

I am using a javascript to set the value for the date field and on the OnChange event I also want to save the value entered.

However, I am getting an error like: getAttribute is not a function.

Below is my javascript code:

function setDeletionDate(executionContext){

    var formContext=executionContext.getFormContext();
    var delDate = new Date();
   
    formContext.getAttribute("ava_gdprdeletiondate").setValue(delDate);
    formContext.data.entity.save().then(
        function () {
            // Success callback, you can do other logic here if needed.
        },
        function (error) {
            // Error callback
            Xrm.Utility.alertDialog(error);
        }
    )

}
I am not sure if I'm getting the js code correctly or not. Please help!
Regards,
Jolas365
  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,959 Super User 2024 Season 1 on at
    RE: Show custom button based on the value of a field date in contact form

    Hi,

    Update below line

    Var formContext= executionContext.getFormCintext();

    With

    Var formContext= executionContext;

  • Jolas365 Profile Picture
    Jolas365 235 on at
    RE: Show custom button based on the value of a field date in contact form

    Hello Bipin Kumar ,

    Thank you for your reply.

    Yes, I am passing Primary Control as parameter for my Display Rule.

    However, the javascript is not working, below is my javascript:

    function updateContactByFormSave(executionContext) {
        // Set attribute value as you want

        var formContext=executionContext.getFormContext();
        formContext.getAttribute("firstname").setValue("XXXXX");
        formContext.getAttribute("lastname").setValue("XXXXX");
        formContext.getAttribute("emailaddress1").setValue("abc@xyz.com");
        formContext.getAttribute("telephone1").setValue(null);

        // Call save method
     
        formContext.data.save().then(
            function () {
                // Success callback, you can do other logic here if needed.
            },
            function (error) {
                // Error callback
                Xrm.Utility.alertDialog(error);
            }
        )
    }
    Kind regards,
    Jolas365
  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Show custom button based on the value of a field date in contact form

    Hi Jolas365,

    If so, you need to create another JScript type web resource and its code should be like this. And replace the previous web resource to it in your display rule. 

    function setDeletionDate(primaryControl){
    
        formContext = primaryControl;
        var delDate = new Date();
       
        formContext.getAttribute("ava_gdprdeletiondate").setValue(delDate);
        formContext.data.entity.save().then(
            function () {
                // Success callback, you can do other logic here if needed.
            },
            function (error) {
                // Error callback
                Xrm.Utility.alertDialog(error);
            }
        )
    
    }

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,959 Super User 2024 Season 1 on at
    RE: Show custom button based on the value of a field date in contact form

    Hi,

    If you are using this function in ribbon button command rule then it will fail .

    You need to pass Primary Control as parameter in your ribbon Rule and define different function to be used in ribbon instead of using same function.

    Please see below link for more info

    d365demystified.com/.../

  • Jolas365 Profile Picture
    Jolas365 235 on at
    RE: Show custom button based on the value of a field date in contact form

    Hello ,

    Thank you for your reply.

    I was able to trace the cause of the issue and it was in my display rule in my ribbon workbench solution.

    Kind regards,

    Jolas365

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Show custom button based on the value of a field date in contact form

    Hi Jolas365,

    Based on the screenshot of your event handler and the code you provided, there is no something wrong. You would better replace formContext.data.entity.save with formContext.data.save. The formContext.data.entity.save() is deprecated. However, the error message indicate that the error occur on this line  formContext.getAttribute("ava_gdprdeletiondate").setValue(delDate), so this should be the reason.

    Did you use this function in other place, for example create a custom rule and use this function in it?

  • Jolas365 Profile Picture
    Jolas365 235 on at
    RE: Show custom button based on the value of a field date in contact form

    Hello a33ik ,

    Thank you very much for giving attention to my issue.

    Please see below for my event handler.

    Event_5F00_handler.JPG

    This is how it should work:

    Once I click in the date picker tool for the date field highlighted in yellow, I will select a date.

    Then, after I move my mouse away from the field the selected date should be saved and then my custom button below would become visible in the ribbon.

    pastedimage1664418549619v2.png

    In the solution I created for the custom button, it is not visible when deletion date field is blank.

    During testing. I am now getting this error: formContext.getAttribute is not a function

    Below is my js:

    function setDeletionDate(executionContext){

        var formContext=executionContext.getFormContext();
        var delDate = new Date();
       
        formContext.getAttribute("ava_gdprdeletiondate").setValue(delDate);
        formContext.data.entity.save().then(
            function () {
                // Success callback, you can do other logic here if needed.
            },
            function (error) {
                // Error callback
                Xrm.Utility.alertDialog(error);
            }
        )

    }

    Thank you very much!

    Best regards,

    Jolas365

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Show custom button based on the value of a field date in contact form

    Hello Jolas,

    Can you please provide screenshot of your onchange handler?

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,703 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,433 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans