Check out the latest features available in Dynamics 365 for Customer Engagement, including LinkedIn Connect, Voice of the Customer and Universal Resource Scheduling.
Dynamics 365 2019 release wave 2 plan Discover the latest updates to Dynamics 365.Release Plan | Weekly Deployment Notes
Ace your Dynamics 365 deployment with packaged services delivered by expert consultants.Explore service offerings
Connect with the ISV success team on the latest roadmap, developer tool for AppSource certification, and ISV community engagements.
ISV self-service portal
The FastTrack program is designed to help you accelerate your Dynamics 365 deployment with confidence.
FastTrack Program | Finance and Operations TechTalks | Customer Engagement TechTalks | Talent TechTalks
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?
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'>
<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'>
<condition attribute='systemuserid' operator='eq' uitype='systemuser' value = '" + UserId + @"' />
FetchExpression BUFetchXmlQuery = new FetchExpression(BUXmlString);
EntityCollection BUFetchXmlResult = service.RetrieveMultiple(BUFetchXmlQuery);
if (BUFetchXmlResult.Entities.Count > 0)
BUId = BUFetchXmlResult.Entities.Id;
Entity Account= (Entity)context.InputParameters["Target"];
Account.Attributes["fieldlogicalnameofbusinessunitonaccount"] = // Pass here entityreference of Business Unit entity
context.InputParameters["Target"] = Account;
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?
I've answered the above in your other post. Can you please review the same and let me know if this works -
If correct, kindly mark both threads as verified.
Hope this helps.
Business Applications communities