Breaking news from around the world
Get the Bing + MSN extension
Now Available in Community - MBAS 2019 Presentation Videos
Catch the most popular sessions on demand and learn how Dynamics 365, Power BI, PowerApps, Microsoft Flow, and Excel are powering major transformations around the globe. | View Gallery
2019 release wave 2 Discover the latest updates to Dynamics 365Release overview guides and videos Release Plan | Early Access Availability
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 TechTalks | Customer Engagement TechTalks | Talent TechTalks | Upcoming 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