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 CRM (Archived)

Setting Field Value Based Upon Logged In User Metadata

(0) ShareShare
ReportReport
Posted on by 44

What is the correct approach to set a field value on an entity (during creation) based upon metadata tied to the logged in user.  For example, we have added a Business Unit field to our Account entity and when a user creates a new account I would like to default that field to the Business Unit to which the user is associated.  Is this best done with Flow or another means?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Pawar Pravin Profile Picture
    5,237 on at

    Hi Justin,

    I would suggest you to create pre operation plugin on create of account record. Using context you can retrieve user business unit.

    Please refer below code for your reference:

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                   ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                   IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                   IOrganizationService service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);

                   Guid UserId = context.UserId;

                   Guid BUId = Guid.Empty;

                   string BUXmlString = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>

                   <entity name='businessunit'>

                   <attribute name='name' />

                   <attribute name='parentbusinessunitid' />

                   <attribute name='businessunitid' />

                   <order attribute='name' descending='false' />

                   <link-entity name='systemuser' from='businessunitid' to='businessunitid' link-type='inner' alias='aa'>

                   <filter type='and'>

                   <condition attribute='systemuserid' operator='eq' uitype='systemuser' value = '" + UserId + @"' />

                   </filter>

                   </link-entity>

                   </entity>

                   </fetch>";

                   FetchExpression BUFetchXmlQuery = new FetchExpression(BUXmlString);

                   EntityCollection BUFetchXmlResult = service.RetrieveMultiple(BUFetchXmlQuery);

                   if (BUFetchXmlResult.Entities.Count > 0)

                   {

                       BUId = BUFetchXmlResult.Entities[0].Id;

                   }

    Entity Account= (Entity)context.InputParameters["Target"];

    Account.Attributes["fieldlogicalnameofbusinessunitonaccount"] = // Pass here entityreference of Business Unit entity

    context.InputParameters["Target"] = Account;

  • Justin Palmer Profile Picture
    44 on at

    Pravin,

    Thank you for the reply.  As a newbie to D365 CRM a couple follow-ups:

    1) Would you have recommended guide for the steps to create a "pre operation plugin"

    2) Is there a reason you recommend a plugin instead of a Flow based solution?

  • Suggested answer
    Priyesh Profile Picture
    7,396 User Group Leader on at

    Hi Justin,

    I've answered the above in your other post. Can you please review the same and let me know if this works -

    community.dynamics.com/.../947603

    If correct, kindly mark both threads as verified.

    Hope this helps.

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 CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans