Getting the localized field name based on a user preferred language in Dynamics 365

Zoe Sands, 05 May 2017

Using localized fields to display product records in Dynamics CRM is a good way to make your Dynamics 365 system language independent and user friendly. But what if you also want to use those localized names to set the name of another custom entity using a plugin that looks up to these products?

In a client Dynamics 365 implementation, we were going to have German and English users and we had custom entities with lookups to entities with localized fields. Unless you do some clever manipulation, the name of all new records no matter the user will have the same (default language) product name displayed in them.

In my situation, we wanted to record the products that a client ordered after a meeting (appointment) so decided that the name of our new appointment product record should be: “{Appointment.Subject} - {Product.Name}”. For example, we had a product called “Beef” in our system, the German translation is “Rindfleisch”. Getting the record name by doing a retrieve query on the product will result in all the records having the default language name (i.e. “Beef”) regardless of the owning users preferred language.

If German user created and an appointment product for beef, the generated name would be “Treffen - Beef” (meeting – beef). This is not the required result because their preferred language is German and we would want the record name to be “Treffen - Rindfleisch”. We decided that the name of our new appointment product record should be based on the language settings of the owner of the new record.

Fortunately, there are user settings that you can query to make this naming a bit more intuitive. I needed to get the product name in the owning users preferred language.

In the code sample below we pass in the localized entity reference, (in my case this was the product entity) the user whose language settings should be used to find the localized records name and the schema name of the field you want to retrieve the translation for.

clip_image001

Note that the Sdk variable is of type IOrganizationService and is used to perform common CRUD operations against the Dynamics CRM platform.