locked
CRM2013: Can't write to E-mail description field with javascript RRS feed

  • Question

  • What am I doing wrong here?

    Scenario: When creating a new e-mail I want to automatically add an e-mail signature to the e-mail description field. The e-mail signature comes from a text field on the user form. So I am trying to make a javascript that activates OnLoad on E-mail form:

    1. First it checks for Formstate = Create,
    2. then it fetches the e-mail signature tekst from the current user and
    3. pastes the text into the description field on the e-mail.

    I can get 1 and 2 to work, but no 3 is not working. Can you see what I am doing wrong?

    I have attached my javascript here:

    function fetchAutoSig()
    {
    var formType = Xrm.Page.ui.getFormType();
    //alert(formType);
    
    if (formType == 1) {
    		var Email = getCurrentUserEmail();
    		//alert(Email);
    		Xrm.Page.getAttribute("description").setValue(Email.toString());
    		Xrm.Page.getAttribute("description").setSubmitMode("always");
    }
    }
    
    function getCurrentUserEmail() {
    
        var serverUrl;
    
        if (Xrm.Page.context.getClientUrl !== undefined) {
    
            serverUrl = Xrm.Page.context.getClientUrl();
    
        } else {
    
            serverUrl = Xrm.Page.context.getServerUrl();
    
        }
    
        var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
    
        var userRequest = new XMLHttpRequest(); 
    
        userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false); 
    
        userRequest.setRequestHeader("Accept", "application/json"); 
    
        userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
    
        userRequest.send();
    
        if (userRequest.status === 200) {
    
            var retrievedUser = JSON.parse(userRequest.responseText).d; 
    
            var userEmail = retrievedUser.val_Emailsignatur;
    
            return userEmail;
    
        }
    
        else {
    
            return "error";
    
        }
    
    }

    Saturday, November 22, 2014 1:50 PM

All replies

  • Are you getting any error? Signature field name is correct? 

    Thanks!

    Sunday, November 23, 2014 9:08 AM
  • I'm getting no errors, it's not doing anything.

    When I active the alert "alert(Email);" it shows the autosig text in the alert box, so it seems that it's fetching the correct text, but it's just not inserting it into the description field of the e-mail.


    Thomas la Cour Søndergaard Valcore A/S | CRM Consultant Denmark

    Sunday, November 23, 2014 4:10 PM
  • Perhaps try removing .toString() function from the email??

    By the way, you should specify to retrieve only the email signature field data, instead of retrieving all fields.

    Monday, November 24, 2014 5:29 AM
  • Hi Thomas,

    Have a look at the below link. Not totally relavent but i guess, the description is an html field. so the format ur setting might not be correct.

    https://community.dynamics.com/crm/f/117/t/102808.aspx

    refer to the Javascript example in the above link.

    Thanks,

    Prasad

    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    Monday, November 24, 2014 7:18 AM
  • Hi

    Try saving the value to DB. 

    Below code should work:

    if (formType == 1) {
    var Email = getCurrentUserEmail();
    //alert(Email);
    Xrm.Page.getAttribute("description").setValue(Email.toString());
     
    Xrm.Page.getAttribute("description").setSubmitMode("always");

    Xrm.Page.data.save();
    }

    Monday, November 24, 2014 10:26 AM
  • Hello Thomas!

    I built and tested the code, try it yourself:

    function addSignature() {
    
        var formType = Xrm.Page.ui.getFormType();
    
        if (formType == 1) {
    
            var userSignature = getUserSignature();
    
            var descriptionFrame = document.getElementById('descriptionIFrame');
    
            //alert(descriptionFrame.readyState.toString());
    
            if (descriptionFrame.readyState !== 'complete') {
    
                descriptionFrame.onreadystatechange = function iFrameStatus() {
    
                    setValue(userSignature, descriptionFrame)
    
                }
    
            } else {
    
                setValue(userSignature, descriptionFrame);
    
            }
        }
    }
    
    function setValue(userSignature, descriptionFrame) {
    
        if (descriptionFrame.readyState == 'complete') {
    
            var htmlTag = userSignature;
    
            var descriptionWindow = document.frames['descriptionIFrame'];
    
            descriptionWindow.document.body.innerHTML = htmlTag;
    
        }
    
    }
    
    function getUserSignature() {
    
        var serverUrl;
    
        if (Xrm.Page.context.getClientUrl !== undefined) {
    
            serverUrl = Xrm.Page.context.getClientUrl();
    
        } else {
    
            serverUrl = Xrm.Page.context.getServerUrl();
    
        }
    
        var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    
        var userRequest = new XMLHttpRequest();
    
        userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false);
    
        userRequest.setRequestHeader("Accept", "application/json");
    
        userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    
        userRequest.send();
    
        if (userRequest.status === 200) {
    
            var retrievedUser = JSON.parse(userRequest.responseText).d;
    
            var userSignature = retrievedUser.new_signature;
    
            return userSignature;
    
        }
    
        else {
    
            return "error";
    
        }
    
    }
    

    I'm not sure if this is supported, but works.

    Elvis


    Wednesday, November 26, 2014 5:38 PM