Wednesday, June 1, 2011

CRM4: inserting data field from custom field in related entity depending on the contact language

We faced a situation where we had some data fields in a phone call template, but we wanted to use custom field for this data field depending on the language of the contact, also we had some picklist that is always coming in the user context language, and we wanted it to depend on the contact language
To solve this, I have updated the template with some text pattern for each field, for the picklist and the custom fields, then I have created a plugin for the phone call, with the below steps
1- Get the contact language
2- Get the custom field depending on the language
3- Get the localized picklist label for the language
4- Search the phone call description for patterns, and replace with the localized value
5- Update the phone call with the new description
To get the localized lookup label I used the metadata service to get all pick list values, then compared each picklist option with the value and the language
                        Option[] deptOptions = RetrieveEntityPicklistValues(metaService, EntityName.incident.ToString(), _attCaseInternalStatus);
                        foreach (Option option in deptOptions)
                        {
                            if (option.Value.Value == (int)internalStatus)
                            {
                                foreach (LocLabel label in option.Label.LocLabels)
                                {
                                    if (label.LanguageCode.Value == languageCode)
                                    {
                                        newCaseInternalStatus = label.Label;
                                        break;
                                    }
                                }
                                break;

                            }
                        }


        public Option[] RetrieveEntityPicklistValues(IMetadataService metaService, string strEntityType, string pickListName)
        {
            try
            {
                RetrieveAttributeRequest request = new RetrieveAttributeRequest();
                request.EntityLogicalName = strEntityType;
                request.RetrieveAsIfPublished = true;
                request.LogicalName = pickListName;

                RetrieveAttributeResponse response = (RetrieveAttributeResponse)metaService.Execute(request);
                AttributeMetadata retreivedAttributeMetadata = response.AttributeMetadata;
                if (retreivedAttributeMetadata is PicklistAttributeMetadata)
                {
                    return ((PicklistAttributeMetadata)retreivedAttributeMetadata).Options;
                }
            }
            catch (Exception eGlobalException)
            {
                throw;
            }
            return null;
        }


Same concept can be applied with any template, a more generic solution could be developed by creating a plugin on template retrieval that replaces the values for any retrieved template