1
votes

I'm writing a plugin for Dynamics CRM. My scenario: I have 2 entities. StudentDataRaw and Language

I'm trying to map StudentDataRaw entity to StudentData entity. StudentDataRaw entity has textbox for Language. How can I get GUID of 'Language' Entity based on 'StudentDataRaw' Entity's textbox value(i.e. English/Spanish).

studentObj.Language = rawContactImport.Contains("Language") ? ((EntityReference)rawStudent["Language"]) : null;

This wouldn't work because

((EntityReference)rawStudent["Language"])

contains textbox value(i.e English/Spanish)

I need to get GUID of English/Spanish record of Language Entity.

How can I get it?

1
Any follow up questions?Arun Vinoth - MVP

1 Answers

0
votes

If it’s plugin, you can query the Language entity using service.RetrieveMultiple by filtering with Name = “Spanish” ie Name = rawStudent["Language"] to get the GUID. You can use fetchxml or QueryExpression. Read more

    private static EntityCollection GetEntityCollection(IOrganizationService service, string entityName, string attributeName, string attributeValue, ColumnSet cols)
    {
        QueryExpression query = new QueryExpression
        {
            EntityName = entityName,
            ColumnSet = cols,
            Criteria = new FilterExpression
            {
                Conditions =
                {
                    new ConditionExpression
                    {
                        AttributeName = attributeName,
                        Operator = ConditionOperator.Equal,
                        Values = { attributeValue }
                    }
                }
                                }
                            };
                return service.RetrieveMultiple(query);
    }

Call the above reusable method by passing parameters: (make sure the schema name is modified per your need & parameterize the Spanish filter)

var language = GetEntityCollection(_orgService, "Language_entityschemaname", "new_name", "Spanish", new ColumnSet("languageid", "new_name"));
Guid languageid = (Guid)language[0].Id;

If it’s data import job, and the languages are limited data like two entries viz. English & Spanish, I would recommend you to maintain an enum, basically a key:value pair to switch it inline instead of querying for every row in batch operation.

In either case, code looks like:

studentObj.Language = new EntityReference("Language_entityschemaname", new Guid(languageid));