CRMBuzz.net

Abe Saldana on Microsoft Dynamics CRM Development
posts - 23, comments - 0, trackbacks - 0

How to's - Getting your Lead Status and State

Working on the previous blog post I have to get some information on the Lead's status and state, just to validate that the lead was not already qualified or disqualified and then make the conversion to an Account and to a Contact, so I started to create a SDK generic library type of assembly with the most common things I will be using on a daily basis.

Working on a lead the first thing I will be doing is check if the lead remains Open [Status = 1 and State = 0 ] or that is not Qualified [ Status = 3  and State = 1 ] and if the lead is not Disqualified [Status = 4 (Most Common - Lost) and State = 2], here is some code to get the independent values:

Getting the Lead Status Information:
 
   1: public int getStatus(Guid leadID)
   2: {
   3:     int returnValue;
   4:  
   5:     try
   6:     {
   7:         lead leadInformation = (lead)service.Retrieve(EntityName.lead.ToString(), leadID, new AllColumns());
   8:  
   9:         returnValue = leadInformation.statuscode.Value;
  10:  
  11:     }
  12:     catch( Exception)
  13:     {
  14:         returnValue = -1;
  15:     }
  16:  
  17:     return returnValue;
  18:  
  19: }
 
Getting the Lead State Information:
 
   1: public int getState(Guid leadID)
   2: {
   3:     int returnValue;
   4:     
   5:     try
   6:     {
   7:         lead leadInformation = (lead)service.Retrieve(EntityName.lead.ToString(), leadID, new AllColumns());
   8:  
   9:         returnValue = ((int)leadInformation.statecode.Value);
  10:  
  11:     }
  12:     catch (Exception)
  13:     {
  14:         returnValue = -1;
  15:     }
  16:     return returnValue;
  17: }

Note: the Lead State value is returned un-boxing the object value from the enum CRMSDK.LeadState

 

Well that is good to pull the value from the lead, but as part of the conversion process I needed to change the status from Open to Qualified, so I needed to have the Update Method and handle the validation for the update, the lead status is handle not from the Service.Update but the state is managed by the SetStateLeadRequest object, here is the code for updating the Status

   1: public bool UpdateStatus(Guid leadID, int Stateparam, int Statusparam)
   2: {
   3:     bool returnValue = false;
   4:  
   5:     try
   6:     {
   7:         SetStateLeadRequest leadUpdateRequest = new SetStateLeadRequest(); 
   8:  
   9:         leadUpdateRequest.EntityId = leadID ;
  10:         
  11:         leadUpdateRequest.LeadStatus = Statusparam;
  12:  
  13:         LeadState valueState = (LeadState)Enum.ToObject(typeof(LeadState),Stateparam);
  14:         leadUpdateRequest.LeadState = valueState;
  15:         
  16:         SetStateLeadResponse stateSet = (SetStateLeadResponse)service.Execute(leadUpdateRequest);
  17:  
  18:         int resultStateCode = getState(leadID);
  19:  
  20:         if (resultStateCode == Stateparam)
  21:         {
  22:             returnValue = true;
  23:         }
  24:  
  25:     }
  26:     catch(Exception)
  27:     {
  28:         returnValue = false;
  29:     }
  30:  
  31:     return returnValue;
  32:  
  33: }

 

After all this the conversion code was so simple to put together and easy to read, I hope the following code snipped's will work for you, keep in mind that I just put the core of the code, CRM connection objects and class definition are not included you need to create those and then test the code 

 

Abe Saldaña

http://crmbuzz.net 
Everything here, though, is my personal opinion and is not read nor approved before being posted.

No warranties or other guarantees will be offered as to the quality of the opinions or anything else offered here.

Print | posted on Wednesday, June 11, 2008 6:07 PM | Filed Under [ Callouts Code Level 300 MS Dynamics CRM 3.0 SDK code ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 6 and type the answer here:

Powered by: