Code to retrieve status value based on status label in CRM 3

This code is for retrieving status value based on status text for salesorder entity.

 

public int RetrieveStatusValue(EntityName Entity, string StatusValue)
   {

       int intStatus = 0;

       MetadataService MetaService = new MetadataService();
       MetaService.Credentials = System.Net.CredentialCache.DefaultCredentials;

       AttributeMetadata am = MetaService.RetrieveAttributeMetadata(Entity.ToString(), "statuscode");
       StatusAttributeMetadata sm = (StatusAttributeMetadata)am;

       foreach (StatusOption so in sm.Options)
       {
           if (so.State == 0)
           {
               if (so.Description.Trim().ToLower() == StatusValue.ToLower())
               {
                   intStatus = so.OptionValue;
                   break;
               }
           }
       }

       am = null;
       sm = null;

       MetaService.Dispose();

       return intStatus;
   }

 

To call the function

RetrieveStatusValue(EntityName.salesorder, "Draft");

 

Bye.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s