Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Fire a validation on form field change to check notes (annotation)

(0) ShareShare
ReportReport
Posted on by 262

Hi All,

I have a CRM Form in which there are multiple fields and notes entity is also enabled. Now what i want to achieve is if i change one of the field on the form and notes is not attached then I will have to alert and display a message and once i attach notes on the form it should not display any message.

My Implementation :

I have written a simple JS Script in onload event which I am getting the value of the required field like below :

citizenshipstatus is a Optionset Field

 

Function is :

checkCitizenshipStatusChangeMaritalStatusChange: function () {

var CSS = Xrm.Page.getAttribute("citizenshipstatus").getValue();
//var MS = Xrm.Page.getAttribute("maritalstatus").getValue();
var notesLength = document.getElementById("notescontrol").contentWindow.document.getElementById("NotesTab").children[1].children[4].children.length;

currently with the above highlighted code i am not getting the Notes attachment
if (CSS && !notesLength)
{
Xrm.Page.ui.setFormNotification('Please attach document(s) in Notes section if Citizenship Status is changed.', 'WARNING');
alert("Please attach document(s) in Notes section if Citizenship Status is changed.");
}

}

 

Can anyone please help me in this. I just want to know how is the notes fetched in Javascript?

*This post is locked for comments

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Fire a validation on form field change to check notes (annotation)

    Hello Dhawal,

    Yes, After adding your new library, Click on Add + button and when when you will go to field on change event handler it will show you drop down under Library field where you can select your newly added library and use your function name in below .

    Let us know if you are facing any issue.

  • dhawal Profile Picture
    dhawal 262 on at
    RE: Fire a validation on form field change to check notes (annotation)

    Hi Rawish,

    Thanks i understood what you trying to explain here...!!!!

    I am attaching a screenshot... As you can see i already have one library being called at onFormLoad... I understand that i can add a new library in the Form Libraries but i dont want to touch the exisiting setting. When i add the library and try to add Event Handler with my field Change event it doesnt shows the setting which i do it shows the old setting instead? Is it possible to use multiple event handlers?

    SS_5F00_FormProperties.png

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Fire a validation on form field change to check notes (annotation)

    Hi,

    You need to call this js method on change of the field where ever you want to throw validation. Let us know if you have implemented above code and facing any issue.

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Fire a validation on form field change to check notes (annotation)

    Since my colleague was doing similar to this. I have working script for you.

    function GetAttachments() {

    var id = Xrm.Page.data.entity.getId();
    var newid = id.slice(1, -1);

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/annotations?$filter=_objectid_value eq " + newid + "", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {

    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var results = JSON.parse(this.response);
    if (results.value.length <= 0)

    alert("Please add notes!")

    } else {

    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();

    }

    add this webresource to your library of the form and call the function on change of the field required:




  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Fire a validation on form field change to check notes (annotation)

    I think Mahender and I have provided you the approach. You have to call the function on change event of the optionset field -->retrieve attachments ---> if count is equals 0 --> put alert and ask for attachement to be added. End.

  • dhawal Profile Picture
    dhawal 262 on at
    RE: Fire a validation on form field change to check notes (annotation)

    Thanks Rawish, my requirement is if user comes and changes a value in optionset field i have to prompt user only if notes is not present and ask them to attach notes. Can you help me with this?

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Fire a validation on form field change to check notes (annotation)

    It is totally unsupported that you are doing. i.e using document.getelementbyid.

    you should below approach.

    1. create a function which you should call on change of the required field. crmbusiness.wordpress.com/.../crm-2013-javascript-to-get-id-of-record

    2. in the function get the current record id and retrieve its attachment using a web api request.  

    3. if count of attachment is more than 0 - perform your logic or operation.

    Please use rest builder to generate your code : github.com/.../CRMRESTBuilder

    sample code below :

    function GetAttachments()

    {

    //getid of the record and pass in the request below

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/CurrentEntityPluralSchemaNae?$filter=_objectid_value eq idoftherecord", true);

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var results = JSON.parse(this.response);

               for (var i = 0; i < results.value.length; i++) {

                   var annotationid = results.value[i]["annotationid"];

    /// your logic here.

               }

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    }

  • dhawal Profile Picture
    dhawal 262 on at
    RE: Fire a validation on form field change to check notes (annotation)

    Thanks for the Prompt response mahender, but i am writing the JS on formOnload event.... How can i use the code mentioned by you in the formOnload event. Can you please let me know?

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Fire a validation on form field change to check notes (annotation)

    Hi,

    You can write a Web API request where you can retrieve notes based on the objectid (your entity) and check if result length >0, have not tested it but it will be something like below

    var req = new XMLHttpRequest();

    var recordId = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/annotations?$select=annotationid&$filter=_objectid_value eq "+recordId , true);

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var results = JSON.parse(this.response);

               if(results.value.length>0)

                //notes is available

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,307 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans