简体   繁体   中英

Dynamics CRM Plugin Lookup

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(ie English/Spanish).

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

This wouldn't work because

((EntityReference)rawStudent["Language"])

contains textbox value(ie English/Spanish)

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

How can I get it?

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM