3
votes

I need to be able to display a case field (ticketnumber) on a case form such that

  1. The value cannot be changed by the user
  2. The value can be copied from the field (CTRL-C + right click/copy)
  3. Solution to work on all crm supported browsers
  4. Solution to work with outlook as client
  5. Solution must use supported methods

I've tried the obvious making the field readonly which makes it un-copyable in IE8/2013 and probably other client combinations.

Just wondering if anyone has figured out the best way to do before I spend time on it. I'm tempted to use a html resource but it seems overkill...

2

2 Answers

0
votes

Add the field to the form's header or footer. You can now copy the field by just selecting it since it is just text but it cannot be edited.

Be sure this business rule is being enforced in a plugin or a user could accidentally (or intentionally) make the change via Bulk Edit, Excel import, OData, API, etc.

EDITED

Since IE9 won't let you copy from header/footer you can do the following:

  1. Write a JavaScript OnLoad event to get the existing value of the field.
  2. Write an Javascript OnChange event that changes to the field to the initial value if the user changes the value. (I would also add a pop-up telling the user the field cannot be edited - to avoid questions.)

That is the supported method.

The unsupported, do at your own risk is to cancel the KeyPress event by adding an OnLoad event handler to your form which then adds a OnKeyDown event handler to the appropriate field:

function OnLoad(){
     document.getElementById("new_field").onkeydown = 
          function(evt) { return false; };
}

Just update the "new_field" to the appropriate field name. Any attempt to edit the field will be blocked.

0
votes

Answering myself here. I ended up using a web resource with a single button. If a new form, I disable the button with button text = "no info until save". If the field I am interested in has a value, I enable the button with button text = field value.

Pressing the button uses the handy "copy to clipboard" mechanism here.

Works great in every client. Web resources don't show up in the outlook reading pane but I have the field of interest visible by default on the form so that copy of the field shows up in the reading pane. On load of the form, I make that field invisible so the user only sees the web resource.

Nicknow's javascript handler solution would work too.

update

I ended up using a readonly text element and auto-selecting the text onclick. Here's the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="/WebResources/ClientGlobalContext.js.aspx"></script>
    <meta charset="utf-8">
</head>
<body style="margin: 0px; padding: 0px;" onload="onload()">
<input id="fieldElement" onclick="onClickElement(this)" readOnly="true" value="save case for number" type="text">

<script>
    var FORM_TYPE_UPDATE = 2;
    var FORM_TYPE_READ_ONLY = 3;
    var FORM_TYPE_DISABLED = 4;

    function onload()
    {
        if (parent.Xrm.Page.ui.getFormType() == FORM_TYPE_UPDATE ||
            parent.Xrm.Page.ui.getFormType() == FORM_TYPE_READ_ONLY ||
            parent.Xrm.Page.ui.getFormType() == FORM_TYPE_DISABLED)
        {
            var value = parent.Xrm.Page.getAttribute('ticketnumber').getValue();
            document.getElementById("fieldElement").value = value;
        }
        else
        {
            // must be a create. handled by default setup
        }

        return;
    }
    function onClickElement(element)
    {
        element.select();
    }

</script>
</body>
</html>