web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

setValue not working

(0) ShareShare
ReportReport
Posted on by
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();
        req.open("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);
                    Xrm.Page.getAttribute("dwterritory").setValue(am_DWTerritory);
                } else {
                    alert(this.statusText);
                }
            }
        };
        req.send();
    }
}

*This post is locked for comments

I have the same question (0)
  • Kenneth Leong Profile Picture
    360 on at
    RE: setValue not working

    what's the data type of am_DWTerritory?

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

    Hey,

    what error are you getting?

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

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

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

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

    No errors are thrown.

  • Suggested answer
    Abby Kong Profile Picture
    6 on at
    RE: setValue not working

    Is there any error if you debug?

    Xrm.Page.getAttribute("dwterritory").setValue(am_DWTerritory);

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

  • Verified answer
    Community Member Profile Picture
    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;
    Xrm.Page.getAttribute("dwterritory").setValue(array);

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
UllrSki Profile Picture

UllrSki 2

#1
Community Member Profile Picture

Community Member 2

#3
SC-08081331-0 Profile Picture

SC-08081331-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans