Skip to main content

Notifications

Announcements

No record found.

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

  • Suggested answer
    Priyesh Profile Picture
    7,396 User Group Leader on at
    RE: Setting Field Value Based Upon Logged In User Metadata

    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.

  • Justin Palmer Profile Picture
    44 on at
    RE: Setting Field Value Based Upon Logged In User Metadata

    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
    Pawar Pravin  Profile Picture
    5,237 on at
    RE: Setting Field Value Based Upon Logged In User Metadata

    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;

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans