Skip to main content



No record found.

Microsoft Dynamics CRM forum

setValue not working

Posted on by Microsoft Employee
I have an on load javascript function that is called when a new record is created. It retrieve the territory for the User and I want to populate that territory to the form. I'm able to successfully retrieve the territory, using XHR, but when I try to call setValue using that territory it doesn't work... Code is below:
function weeklyReport_OnLoad() {
    //only on create
    if (Xrm.Page.ui.getFormType() == 1) {
        var id = Xrm.Page.context.getUserId();
        id = id.replace("{", "").replace("}", "");
        var req = new XMLHttpRequest();"GET", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/SystemUserSet(guid'" + id + "')?$select=am_DWTerritory", true);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function() {
            if (this.readyState === 4) {
                this.onreadystatechange = null;
                if (this.status === 200) {
                    var result = JSON.parse(this.responseText).d;
                    var am_DWTerritory = result.am_DWTerritory;
                    // alert("Setting DW Territory");
                    // console.log(am_DWTerritory);
                    // alert(am_DWTerritory.Name);
                } else {
  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: setValue not working
    I figured it out. I needed to unbox the object returned and populate a new lookup reference object to get it to work. Not really sure why I need to do this as its really tedious, you should be able to just take the object and pass that as a value...
    var array = new Array();
    array[0] = new Object();
    array[0].id = am_DWTerritory.Id;
    array[0].name = am_DWTerritory.Name;
    array[0].entityType = am_DWTerritory.LogicalName;
  • Suggested answer
    Abby Kong Profile Picture
    Abby Kong 2 on at
    RE: setValue not working

    Is there any error if you debug?


    Can you double check the field name? Maybe prefix is missed?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: setValue not working

    No errors are thrown.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: setValue not working

    Its a lookup field. The console.log returns an object which I try to use in setValue.

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,752 on at
    RE: setValue not working


    what error are you getting?

    I think you must try to debug - you will know where and why its failing.

  • Kenneth Leong Profile Picture
    Kenneth Leong 360 on at
    RE: setValue not working

    what's the data type of am_DWTerritory?

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .


André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,835 Super User

Martin Dráb Profile Picture

Martin Dráb 225,526 Super User

nmaenpaa Profile Picture

nmaenpaa 101,148


Featured topics

Product updates

Dynamics 365 release plans