Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 55831

Set Lookup value

$
0
0

Hi all,

This has my mind spinning. I need to set the lookup value of a new record via a web API. I currently do this for an option set using the below code to return the value based on a text match. I believe that the value I have to set for the lookup is ? a GUID rather than value as below.

So if the record to be created is in the Canditate entity and the lookup is in the Course entity how would I go about setting it. I have played with using EntityReference however no success to date.

if (!string.IsNullOrWhiteSpace(formValues.Get("Standard")))
                {
                    string selectedValue = formValues.Get("Standard");
                    RetrieveAttributeRequest optionSetRequest = new RetrieveAttributeRequest();
                    optionSetRequest.EntityLogicalName = "Canditate";
                    optionSetRequest.LogicalName = "dev_standard";
                    optionSetRequest.RetrieveAsIfPublished = true;
                    RetrieveAttributeResponse optionSetResponse = (RetrieveAttributeResponse)service.Execute(optionSetRequest);
                    PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (PicklistAttributeMetadata)optionSetResponse.AttributeMetadata;
                    OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
                    int selectedOptionValue = 0;
                    foreach (OptionMetadata oMD in optionList)
                    {

                        if (string.Equals(oMD.Label.UserLocalizedLabel.Label, selectedValue, StringComparison.OrdinalIgnoreCase))
                        {
                            selectedOptionValue = oMD.Value.Value;
                            break;
                        }
                    }
                    if (selectedOptionValue == 0)
                    {
                        selectedOptionValue = 1;
                    }
                    target["dev_standard"] = new OptionSetValue(selectedOptionValue);
                }


Viewing all articles
Browse latest Browse all 55831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>