locked
Adding text to field on load RRS feed

  • Question

  • Hi,

    I've been trying to automatically add a signature to the description field when hitting "Reply" on an e-mail.

    What I thought about doing is using JavaScript to do this, by reading the description into a variable and adding the signature to it, then replace the current description with the new one. But since the description loads after the javascript function loads, the variable is empty.


    My code:

    function test() {

    var desc = Xrm.Page.getAttribute("description").getValue();

    alert(desc); //Nothing, since description loads after I get the value

    }

    Currently this function is added to the form's on load event.

    Question is: How can I wait until the description is loaded before the function loads?

    If this isn't the best way to do this, I'm open for alternatives.


    Thanks for your time :)


    • Edited by Whew32 Tuesday, February 10, 2015 9:53 PM
    Tuesday, February 10, 2015 9:32 PM

Answers

  • Hi,

    I solved the problem by checking if the value is nothing, and if it is, wait 1 second and retry.

    What I ended up with:

    function test() {
        if (Xrm.Page.getAttribute("description").getValue() == "") {
            console.log("Description empty, rerunning in 1 second");
            setTimeout(function() {
                test();
            }, 1000);
        } else {
            console.log(Xrm.Page.getAttribute("description").getValue());
        }
    }


    • Marked as answer by Whew32 Wednesday, February 11, 2015 4:12 PM
    Wednesday, February 11, 2015 4:12 PM

All replies

  • Hi,

         Try using

    var description = Xrm.Page.data.entity.attributes.get("description").getValue();

    alert(description);

         This is because the form is not loaded hence page will not return data. You need to use Xrm.Page.data for this.

    Hope this helps.
     
     -----------------------------------------------------------------------
     Minal Dahiya
     blog : http://minaldahiya.blogspot.com.au/

     If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

     

    • Proposed as answer by Minal Dahiya Tuesday, February 10, 2015 10:04 PM
    • Unproposed as answer by Whew32 Wednesday, February 11, 2015 3:34 PM
    Tuesday, February 10, 2015 10:04 PM
  • Hi,

    Unfortunately this doesn't work either. I tried this as well, and the variable is still empty for some reason.

    Tuesday, February 10, 2015 10:31 PM
  • Are you sure the value is present in description field? As there is nothing wrong with the code!!!
    Tuesday, February 10, 2015 11:22 PM
  • Hi,

    The value seems to load after my JavaScript loads.

    The code:

    function test() {

    console.log("Debug: " + Xrm.Page.data.entity.attributes.get("description").getValue());

    }

    Returns:

    Not entirely sure why this is happening. Thanks for your help.

    Wednesday, February 11, 2015 9:51 AM
  • I would suggest you use the Console in the browser debugger to test your getValue() calls.  Take a look at this CRM Tip Of The Day that explains how to do this.  Just make sure you change the "Frame" to the Main.aspx or it won't work (the article explains this).

    It's possible the field you are looking for doesn't match exactly...although if you are using the OOB Description field, it then your first try should work in the Console.

    Xrm.Page.getAttribute("description").getValue();

    Wednesday, February 11, 2015 2:14 PM
  • Hi,

    I solved the problem by checking if the value is nothing, and if it is, wait 1 second and retry.

    What I ended up with:

    function test() {
        if (Xrm.Page.getAttribute("description").getValue() == "") {
            console.log("Description empty, rerunning in 1 second");
            setTimeout(function() {
                test();
            }, 1000);
        } else {
            console.log(Xrm.Page.getAttribute("description").getValue());
        }
    }


    • Marked as answer by Whew32 Wednesday, February 11, 2015 4:12 PM
    Wednesday, February 11, 2015 4:12 PM