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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

JavaScript not working to set manager in lookup

(0) ShareShare
ReportReport
Posted on by 387

Hi all,

I have a form that has three lookup fields on it. All three point to the users table. This form is created through a subgrid of a user record.

One field I null out so the person can choose what they want. But two fields I want to set.

User and Manager.

So today when the record is created through the subgrid both of these two lookups are populated with the users lookup info from the relationship. I dont see a way to tell it to auto populate the manager lookup. Seems like it just puts the user it is coming from into all user lookups. So javascript is what I guess I need to use to auto populate this. I've tried a million variations of this, but this is where I am at now. but when I run it the first alert comes back with "undefined". Not sure why though.

If anyone has some ideas for me on how to get this working I would appreciate it.

function setManager(executionContext) {
var formContext = executionContext.getFormContext();
var lookupValue = [{
id: "",
name: "",
entityType: "systemuser"
}];

var userId = formContext.getAttribute("new_user").getValue()[0].id.slice(1, -1);

if (userId !== null) {
Xrm.WebApi.retrieveRecord("systemuser", userId, "?$select=parentsystemuserid").then(
function success(result) {
var managerId = result.parentsystemuserid;
alert(managerId);

Xrm.WebApi.retrieveRecord("systemuser", managerId, "?$select=fullname").then(
function success(result) {
var managerName = result.fullname;
alert(managerName);

lookupValue[0].id = managerId;
lookupValue[0].name = managerName;

formContext.getAttribute("new_reportsto").setValue(lookupValue);
}
);
}
);
}
}

I have the same question (0)
  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    Hi,

    parentsystemuserid is a navigation property when _parentsystemuserid_value is the associated field value.

    Could you try with (_parentsystemuserid_value):

     Xrm.WebApi.retrieveRecord("systemuser", userId, "?$select=_parentsystemuserid_value")

    Instead of parentsystemuserid:

     Xrm.WebApi.retrieveRecord("systemuser", userId, "?$select=parentsystemuserid")

    You can also make only one request to get the fullname and id of manager with $expand

    const result = await Xrm.WebApi.retrieveRecord("systemuser", userId, "?$select=systemuserid&$expand=parentsystemuserid($select=fullname)");
    
    formContext.getAttribute("new_reportsto").setValue(
        {
            id: result.parentsystemuserid.systemuserid,
            name: result.parentsystemuserid.fullname,
            entityType: "systemuser"
        }
    );

  • Leah Ju Profile Picture
    Microsoft Employee on at

    Hi Mattw112b,

    I would suggest that you can use 'Dataverse Rest Builder' tool to build web api code easily.

    https://github.com/GuidoPreite/DRB 

    pastedimage1682581005645v1.png

    pastedimage1682581057355v2.png

    Then you can copy the code and change guid to variable in your js code.

  • Suggested answer
    Mattw112b Profile Picture
    387 on at

    Thanks, so when you told me about the _value, I was like "duh"... lol.  I should've seen that, thanks!

    I like the cleaner, simpler code you sent too with expand..., this kind of worked.  For some reason I couldn't get "await" to work in Dynamics?  So had to change to a ".then(" instead then that worked.

    Thanks!

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    This is because async must be placed on a function using await.

    Full code with async/await:

    async function setManager(execCtx) {
        const formCtx = execCtx.getFormContext();
    
        const userLookupValue = formCtx.getAttribute("new_user").getValue();
    
        if (userLookupValue != null) {
            const userId = userLookupValue[0].id.replace(/[{}]/g, "");
            const result = await Xrm.WebApi.retrieveRecord("systemuser", userId, "?$select=systemuserid&$expand=parentsystemuserid($select=fullname)");
    
            formCtx.getAttribute("new_reportsto").setValue(
                {
                    id: result.parentsystemuserid.systemuserid,
                    name: result.parentsystemuserid.fullname,
                    entityType: "systemuser"
                }
            );
        }
    }

    If my answer helped you, please click Yes on my answer.

  • Mattw112b Profile Picture
    387 on at

    Thanks, good to know.  But they way I rewrote it with .then... seems to work fine?

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    Yes, .then work fine.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Subra Profile Picture

Subra 142

#2
11manish Profile Picture

11manish 112

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 86 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans