locked
How to set a value to PickList() type field RRS feed

  • Question

  • How to set a value to PickList() type field programmatically?
    I'm doing the following, which is raising an error while creating a user.
    new_Division is a custom attribute of systemuser Entity. If I need to set the "value", how can I get the associated value for a picklist?

    list = new Picklist();
    newUser.new_division =
    new Picklist
    ();
    newUser.new_division.name = Division;
    //Division

    Thursday, March 18, 2010 3:27 AM

Answers

  • Following is the example code to set the case type..here "problem" is display text of the relevant picklist value..

    following code retrives the value based on display text and then sets the picklist value against relevant field..

    hope this helps..

     //Case Type
     int picklistValueCaseType = 999;
     picklistValueCaseType = GetValueFromPicklist(myMetaDataService, "incident", "casetypecode", "problem");
     if (picklistValueCaseType != 999)
     {
       myCrmObject.casetypecode = CrmTypes.CreatePicklist(picklistValueCaseType);
     }
    
    
    
    
    
            /// <summary>
            /// Gets an integer value from a picklist if it exists
            /// </summary>
            /// <param name="myMetaData">The MetaData Service Object</param>
            /// <param name="strEntity">The Entity schema name</param>
            /// <param name="pickListName">The Picklist schema name</param>
            /// <param name="strSearchString">The Text being searched for</param>
            /// <returns>The Integer value from the Picklist Text or 999 if it is not found</returns>
            private int GetValueFromPicklist(MetadataService myMetaData, string strEntity, string pickListName, string strSearchString)
            {
    
    
                try
                {
                    // Create the request
                    RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest();
                    attributeRequest.EntityLogicalName = strEntity;
                    attributeRequest.LogicalName = pickListName;
                    attributeRequest.RetrieveAsIfPublished = true;
    
    
                    RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)myMetaData.Execute(attributeRequest);
    
                    //AttributeMetadata attMetaData = myMetaData.RetrieveAttributeMetadata(strEntity, pickListName);
                    PicklistAttributeMetadata picklist = (PicklistAttributeMetadata)attributeResponse.AttributeMetadata;
                    int retValue = 999;
    
                    foreach (Option o in picklist.Options)
                    {
                        if (strSearchString.ToLower() == o.Label.UserLocLabel.Label.ToString().ToLower())
                        {
                            retValue = o.Value.Value;
                            break; 
                        }
                    }
    
                    return retValue;
                }
                catch (Exception ex)
                {
                    // Log Entry to Event Viewer              
                    throw (ex);
                }
    
            }



    • Marked as answer by shiil Friday, March 19, 2010 2:20 AM
    Thursday, March 18, 2010 9:21 AM

All replies

  • Picklist list = new Picklist();
    Picklist.value=1;

    and

    newUser.new_division = list


    Mariusz
    Thursday, March 18, 2010 4:32 AM
  • Following is the example code to set the case type..here "problem" is display text of the relevant picklist value..

    following code retrives the value based on display text and then sets the picklist value against relevant field..

    hope this helps..

     //Case Type
     int picklistValueCaseType = 999;
     picklistValueCaseType = GetValueFromPicklist(myMetaDataService, "incident", "casetypecode", "problem");
     if (picklistValueCaseType != 999)
     {
       myCrmObject.casetypecode = CrmTypes.CreatePicklist(picklistValueCaseType);
     }
    
    
    
    
    
            /// <summary>
            /// Gets an integer value from a picklist if it exists
            /// </summary>
            /// <param name="myMetaData">The MetaData Service Object</param>
            /// <param name="strEntity">The Entity schema name</param>
            /// <param name="pickListName">The Picklist schema name</param>
            /// <param name="strSearchString">The Text being searched for</param>
            /// <returns>The Integer value from the Picklist Text or 999 if it is not found</returns>
            private int GetValueFromPicklist(MetadataService myMetaData, string strEntity, string pickListName, string strSearchString)
            {
    
    
                try
                {
                    // Create the request
                    RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest();
                    attributeRequest.EntityLogicalName = strEntity;
                    attributeRequest.LogicalName = pickListName;
                    attributeRequest.RetrieveAsIfPublished = true;
    
    
                    RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)myMetaData.Execute(attributeRequest);
    
                    //AttributeMetadata attMetaData = myMetaData.RetrieveAttributeMetadata(strEntity, pickListName);
                    PicklistAttributeMetadata picklist = (PicklistAttributeMetadata)attributeResponse.AttributeMetadata;
                    int retValue = 999;
    
                    foreach (Option o in picklist.Options)
                    {
                        if (strSearchString.ToLower() == o.Label.UserLocLabel.Label.ToString().ToLower())
                        {
                            retValue = o.Value.Value;
                            break; 
                        }
                    }
    
                    return retValue;
                }
                catch (Exception ex)
                {
                    // Log Entry to Event Viewer              
                    throw (ex);
                }
    
            }



    • Marked as answer by shiil Friday, March 19, 2010 2:20 AM
    Thursday, March 18, 2010 9:21 AM
  • Hi MayankP, I also encounter this problem, I want to specify a default value for a picklist field in CRM2011,

    I did't try your method coded by C#, Can I use a same function method like yours writed by Javascript?

    Thank you

    Monday, September 9, 2013 9:47 AM