Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)
Under review by Community Managers

Under review

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

How to insert the value of custom field through create the entity

Posted on by 5

Hi Everyone,

I'm facing a problem about how to insert the custom field value through create a new systemuser.

Here is the details:

In Microsoft CRM system, there has systemuser table and i have a custom field named "SSOID", and the value of  "SSOID" I get it from webapi.

My requirement is when I in systemuser creation page in CRM.

I enter the domainname value to the domainname textbox, the other fields like firstname, lastname and businessID will auto populated base on domainname. they are come from active directory.

and meanwhile I also call the webapi to get the SSOID base on domainname.

What I want is after I click the save button, the new systemuser should be create and the SSOID also should be insert along with new systemuser.

But now when I click the button, some error happened "usersettings With Id = 5ab7eb74-511a-ea11-810b-005056ba5450 Does Not Exist"

Below is my code, it looks very simple, but some error happened;

IPluginExecutionContext context;
IOrganizationServiceFactory factory;
IOrganizationService service;
// 获取执行上下文
context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// 获取服务工厂
factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
// 获取服务
service = factory.CreateOrganizationService(context.UserId);

if (context.MessageName.ToLower() == "create"
&& context.InputParameters.Contains("Target")
&& context.InputParameters["Target"] is Entity)
{
try
{

var domainName = "";
Entity entity = (Entity)context.InputParameters["Target"];
if (context.Depth > 1)
{ return; }
if (entity.Attributes.Contains("domainname"))
{
domainName = entity["domainname"].ToString();
}

var SSOId =" get from webapi";

if (!string.IsNullOrEmpty(SSOId))
{
var businessEntityRef = (Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["businessunitid"]);
var businessunit = service.Retrieve(businessEntityRef.LogicalName, businessEntityRef.Id, new ColumnSet(true));
entity["businessunitid"] = businessEntityRef; // businessunit id is look up field comes from businessunit reference table
entity["domainname"] = entity["domainname"]; 
entity["lastname"] = entity["lastname"];
entity["firstname"] = entity["firstname"];
entity["new_ssoid"] = SSOId ;
service.Update(entity);  //i can't use create method, it shows "The specified Active Directory user already exists as a Dynamics 365 user"
}
else
{
throw new InvalidPluginExecutionException("userID not exist");
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}

I'm new to CRM plugin development, can anyone who guide me how to solve this problem, very thanks.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans