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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Javascript to display an alert on data change

(0) ShareShare
ReportReport
Posted on by 112

Hi, 

I need some help in correcting an error which is appearing when the postcode is being altered in CRM2013. There is javascript which is suppose to display an alert to advise this has been changed and to check correct. 

To be honest I dont really know java but on reading a bit online about I would of expected to see the  Xrm.Utility.alertDialog function on the field’s onChange but this is not there.

The code at present is 


function PostCode_OnChange() {
var postCodeField = 'address1_postalcode';
var postCodeValue = Xrm.Page.getAttribute(postCodeField).getValue();
if (postCodeValue) {
alert("The post code has changed, please ensure correct");

The error message advised 

Error:Unable to get property 'getValue' of undefined or null reference

Anyone able to point me in the direction of where the code is going wrong and how to fix?

Thanks

Iain

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hello,

    Most probably field name is not correct or some has removed this field from entity form, so make sure your field name is correct and placed over form, you can change your code like below

    var postCodeField = 'address1_postalcode';

    if(Xrm.Page.getAttribute(postCodeField)!=null)

    {

    var postCodeValue = Xrm.Page.getAttribute(postCodeField).getValue();

    if (postCodeValue) {

    alert("The post code has changed, please ensure correct");

    }

    }

    Thanks

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

    Hmm Lain,

    I think you should change the if here:

    if (postCodeValue)  -->because this is will not return true anyway, since this is field value, a string, not boolean

    to if (postCodeValue != null)

    So:

    function PostCode_OnChange() {

    var postCodeField = 'address1_postalcode';

    var postCodeValue = Xrm.Page.getAttribute(postCodeField).getValue();

    if (postCodeValue !=null) {

    alert("The post code has changed, please ensure correct");

    }

    }

    Hope this helps.

    Thanks

  • Mahendar Pal Profile Picture
    45,095 on at

    Hi Aileen,

    It will work

    if (postCodeValue) // this will check if this field has some value or not.

    Thanks

  • Suggested answer
    Mohsin Ali Profile Picture
    3,634 on at

    Iain, you cannot fire the onChange event for any of the field added in composite. As in this case 'address1_postalcode' is actually not in the form but in the composite field of address.

    A better way to handle onchange events on individual fields of composite field, is to add a hidden section to your form, and add each of the individual address fields into this section. You can then add JavaScript event handlers to the individual fields which will also be applied to the fields in the composite fly-out menu.

  • Iain_C_54 Profile Picture
    112 on at

    HI HIMBAP,

    Thanks.

    The field name was incorrect in the code, however putting in the correct name 'a_postcode'

    I am now getting another error message advising of an error with the fields customized event

    Field: a_postcode

    Event :onchange

    Error : 'PostCode_Onchnage' is undefined.

    The fun of unwrapping someones code!

  • Aileen Gusni Profile Picture
    44,524 on at

    Iain

    Seems you missed some bracket

  • Iain_C_54 Profile Picture
    112 on at

    Hi Moshin Ali,

    The way the form is set up the data is not entered in a composite. The address fields are all individually shown on the form.

    but i understand what your saying if it were to be part of a composite

  • Mahendar Pal Profile Picture
    45,095 on at

    Please check if all brackets are there, otherwise share you code here for inquiry

  • Iain_C_54 Profile Picture
    112 on at

    As far as i can see all the brackets are there.

    function PostCode_OnChange() {

       var postCodeField = 'a_postcode';

       var postCodeValue = Xrm.Page.getAttribute(postCodeField).getValue();

       if (postCodeValue) {

           alert("The post code has changed, please ensure correct");

       }

    }

  • Verified answer
    Mohsin Ali Profile Picture
    3,634 on at

    Iain, as the postal code is not  bit field so you must change the 'if'

    function PostCode_OnChange() {

      var postCodeField = 'a_postcode';

      var postCodeValue = Xrm.Page.getAttribute(postCodeField).getValue();

      if (postCodeValue != null) {

          alert("The post code has changed, please ensure correct");

      }

    }

    Also, put valid function name into onChange event.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans