1

How can I access form field values using Javascript in Dynamics 365 online? This is what I've tried:

A script web resource with an "onload" event called on the form "load". This doesn't work. I get [Object object] but I expect a string. Debugging it shows no properties with the value of the field

function loadForm()
{
    var value1 =    Xrm.Page.data.entity.attributes.get("new_dealercode").getValue();
    alert(value1);
}

An HTML web resource that I include on the form, this does nothing, but I know it's being called because if I put an alert on it, it is alerted.

<html>
<head>
<script type="text/javascript">
    var value1 = window.parent.Xrm.Page.getAttribute("new_dealercoder").getValue();
    alert(value1);
</script><meta charset="utf-8">
</head><body><br></body>
</html>

What I'm trying to achieve: An Iframe with dynamic "src" based on a field value on the form.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Leon Gentle
  • 45
  • 1
  • 1
  • 5

1 Answers1

3

You can shorten your code to access Form field values to

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

If however you are addressing an OptionSet field (Dropdown Selection) you need to use

Xrm.Page.getAttribute("new_dealercode").getSelectedOption();

which returns an Option object with textand value property (see MSDN).
You will find that Lookup and DateTime fields are even more complicated to get and set.

If you need to retrieve properties of a Lookup attribute, you are dealing with an array of EntityReferences featuring the properties id, name and logicalname.

To obtain the id, access the first element in the array:

var dealerobj = Xrm.Page.getAttribute("new_dealercode").getValue();
var dealerid = dealerobj[0].id;

Caution! You may want to harden your code by checking for null values!


As for your second issue, you can access the source of iframeForm controls using

Xrm.Page.getControl("your_control_name_here").setSrc("$webresources\new_your.html")

Start at MSDN Client-side programming reference to find the full reference.

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • The shortened code didn't work either :-( . I still get [Object object] on the alert and I'm expecting a text value. I'm thinking that the Javascript gets called too soon anyway because the alert happens before the page is fully loaded (continues rendering after I click "ok"). This makes me think the javascript should be in an html web resource on the form in stead of the "Form Onload". Only problem is that if I put it on an HTML resource, nothing happens. – Leon Gentle Jan 23 '17 at 14:26
  • If you register your function for the CRM Form OnLoad event, it is guaranteed that all Form fields/attribute values are accessible. If you try to access the parent CRM Form from a web resource, this may not be the case. It's hard to tell what's going wrong in your case, but this is such basic stuff that's hard to believe it shouldn't work. – Filburt Jan 23 '17 at 15:21
  • I found out what's wrong. It's a lookup. My next question is how to get the value out of a lookup. I can get the "name" out, but I need the ID. Do you know how I can get that? – Leon Gentle Jan 24 '17 at 13:35
  • Updated my answer to get ID from Lookup/EntityReference. – Filburt Jan 24 '17 at 14:54
  • Thanks Filburt, but it's not quite the ID I was looking for. I don't want the GUID, I want the ID of the record (It's displayed on the screen). In other words, I have a dealer ID of "836" that is displayed on the screen. if I use Xrm.Page.getAttribute("new_dealercode").getValue()[0].name, I get "The Actual Dealer Name". If I use Xrm.Page.getAttribute("new_dealercode").getValue()[0].id, I get the GUID id, but I want to get "836", the dealer id that is displayed on the screen. Do you know how I can get that (the actual text that is displayed on the screen - not using the DOM). – Leon Gentle Jan 24 '17 at 16:23
  • In that case you are in for even more complex stuff: You'll have to retrieve the account record itself and the accounts field containing your dealer ID text (presumable the `accountnumber` attribute) because the Lookup to account does only contain the GUID and the primary field `name`. You may want to explore the [MSDN code samples](https://msdn.microsoft.com/en-us/library/gg309721.aspx) as it will take some before I can provide a code example. – Filburt Jan 24 '17 at 16:37
  • Thanks for this Fulburt. Although I didn't find the specific answer I wanted, I found the answer I needed. I've changed my approach to not try and call values from the related Lookup/Entity (there is probably a way, but in my case best avoided to keep it simple) and rather user a workflow / data import to set the values I need and then work from there. – Leon Gentle Jan 31 '17 at 16:00