/// <summary> /// Returns the number of options for a picklist /// </summary> /// <param name="entityName">name of the entity</param> /// <param name="picklistName">name of the picklist</param> /// <param name="service">CRM service</param> /// <returns>integer count</returns> public static int GetPicklistOptionCount(string entityName, string picklistName, IOrganizationService service) { RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest { EntityLogicalName = entityName, LogicalName = picklistName, RetrieveAsIfPublished = true }; // Execute the request. RetrieveAttributeResponseWrapper retrieveAttributeResponse = (new RetrieveAttributeResponseWrapper(service.Execute(retrieveAttributeRequest))); //this is the only change from before // Access the retrieved attribute. PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata; OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray(); return(optionList.Length); }
public void GetPicklistOptionCountTest() { //ARRANGE - set up everything our test needs //first - set up a mock service to act like the CRM organization service var serviceMock = new Mock <IOrganizationService>(); IOrganizationService service = serviceMock.Object; PicklistAttributeMetadata retrievedPicklistAttributeMetadata = new PicklistAttributeMetadata(); OptionMetadata femaleOption = new OptionMetadata(new Label("Female", 1033), 43); femaleOption.Label.UserLocalizedLabel = new LocalizedLabel("Female", 1033); femaleOption.Label.UserLocalizedLabel.Label = "Female"; OptionMetadata maleOption = new OptionMetadata(new Label("Male", 1033), 400); maleOption.Label.UserLocalizedLabel = new LocalizedLabel("Male", 400); maleOption.Label.UserLocalizedLabel.Label = "Male"; OptionSetMetadata genderOptionSet = new OptionSetMetadata { Name = "gendercode", DisplayName = new Label("Gender", 1033), IsGlobal = true, OptionSetType = OptionSetType.Picklist, Options = { femaleOption, maleOption } }; retrievedPicklistAttributeMetadata.OptionSet = genderOptionSet; RetrieveAttributeResponseWrapper picklistWrapper = new RetrieveAttributeResponseWrapper(new RetrieveAttributeResponse()); picklistWrapper.AttributeMetadata = retrievedPicklistAttributeMetadata; serviceMock.Setup(t => t.Execute(It.Is <RetrieveAttributeRequest>(r => r.LogicalName == "gendercode"))).Returns(picklistWrapper); //ACT int returnedCount = MockDemo.GetPicklistOptionCount("ANYENTITYMATCHES", "gendercode", service); //ASSERT Assert.AreEqual(2, returnedCount); }