Dynamics CRM - Accessing Custom Product Option Value

前端 未结 2 969
一个人的身影
一个人的身影 2020-12-12 01:38

Is there a way to programmatically access the Label & Value fields that has been created as a custom Field in MS CRM Dynamics please?

I have added a custom field

2条回答
  •  独厮守ぢ
    2020-12-12 02:03

    This function retrieves a dictionary of possible values localised to the current user. Taken from: CRM 2011 Programatically Finding the Values of Picklists, Optionsets, Statecode, Statuscode and Boolean (Two Options).

    static Dictionary GetNumericValues(IOrganizationService service, String entity, String attribute)
    {
        RetrieveAttributeRequest request = new RetrieveAttributeRequest
        {
            EntityLogicalName = entity,
            LogicalName = attribute,
            RetrieveAsIfPublished = true
        };
    
        RetrieveAttributeResponse response = (RetrieveAttributeResponse)service.Execute(request);
    
        switch (response.AttributeMetadata.AttributeType)
        {
            case AttributeTypeCode.Picklist:
            case AttributeTypeCode.State:
            case AttributeTypeCode.Status:
                return ((EnumAttributeMetadata)response.AttributeMetadata).OptionSet.Options
                    .ToDictionary(key => key.Label.UserLocalizedLabel.Label, option => option.Value.Value);
    
            case AttributeTypeCode.Boolean:
                Dictionary values = new Dictionary();
    
                BooleanOptionSetMetadata metaData = ((BooleanAttributeMetadata)response.AttributeMetadata).OptionSet;
    
                values[metaData.TrueOption.Label.UserLocalizedLabel.Label] = metaData.TrueOption.Value.Value;
                values[metaData.FalseOption.Label.UserLocalizedLabel.Label] = metaData.FalseOption.Value.Value;
    
                return values;
    
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
    

    So you would then need to do something like:

    Dictionary values = GetNumericValues(proxy, "your_entity", "new_producttypesubcode");
    
    if(values.ContainsKey("Trophy"))
    {
        //Do something with the value
        OptionSetValue optionSetValue = values["Trophy"];
        int value = optionSetValue.Value;
    }
    

提交回复
热议问题