Nahuel Prieto
Nahuel Prieto

Reputation: 371

How do I fix "The user Id(s) is invalid" when creating an application user?

I'm automating the creation of an application user in Dynamics 365. The user does get created, but when I try to assign a role to it later in the code, I get this message: The user Id(s) [a guid here] is invalid. When I try assigning the user a role from the Dynamics web UI, I get a variant of the same error: The user ID associated with the current record is not valid..

I am using a CrmServiceClient in C# to connect to the organization.

I am supplying the following fields when creating the user programmatically:

One thing I noticed is that when I look at the user via the web UI, the app id I assigned in the creation does not show up.

This is my current code:

    private void SetUpPermissions()
    {
        var roleId = GetRoleId();
        var userId = CreateApplicationUser();
        CrmSvc.Associate(
            "systemuser",
            userId,
            new Relationship("systemuserroles_association"),
            new EntityReferenceCollection {
                new EntityReference("role", roleId)
            });
    }

    private Guid CreateApplicationUser() => CrmSvc.CreateNewRecord("systemuser", new Dictionary<string, CrmDataTypeWrapper>
        {
            { "firstname", new CrmDataTypeWrapper(ProposalManagerApplicationName, CrmFieldType.String) },
            { "lastname", new CrmDataTypeWrapper("Application", CrmFieldType.String) },
            { "businessunitid", new CrmDataTypeWrapper(BusinessUnitId.Value, CrmFieldType.Lookup) },
            { "applicationid", new CrmDataTypeWrapper(ApplicationId, CrmFieldType.Key) },
            { "internalemailaddress", new CrmDataTypeWrapper("[email protected]", CrmFieldType.String) }
        });

I would expect this user to show up in the Web UI the same way an application user created via the UI would, but it does not. It does not show the application id I assigned to it in the code, and it throws the aforementioned error when trying to assign a role to it.

Am I doing something wrong? If the answer is backed up by existing documentation please point me to it as well so I can learn why I did not find it.

Upvotes: 2

Views: 1965

Answers (1)

Daryl
Daryl

Reputation: 18895

Your associate looks correct. I'd troubleshoot your code by attempting to associate a role to an already exiting user created normally. If that works, use the FetchXml Builder or some other method to compare the user you create and the user that is created via the UI for potential differences, and attempt to resolve those until you've determined what is causing your error.

Upvotes: 1

Related Questions