Search code examples
c#pluginsdynamics-crmcrm

How to insert the value of custom field while creating the entity record?


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

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 am 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 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?


Solution

  • Please use the below code:

    var SSOId = " get from webapi";
    
    if (!string.IsNullOrEmpty(SSOId))
    {
        Entity entityToUpdate = new Entity("systemuser", new Guid(entity.Id));
        entityToUpdate["new_ssoid"] = SSOId;
        service.Update(entityToUpdate);
    }
    else
    {
        throw new InvalidPluginExecutionException("userID not exist");
    }
    

    You need to create a new instance of entity object to update after record creation (Post-create Asynchronous plugin).

    Also if you throw exception when there is no error, the transaction will rollback. Think about it.