locked
Updating Leads State programmatically. RRS feed

  • Question

  • please help me,  i want to update the lead state (status reason also) from a custom aspx page, using sdk.

    I'm adding a custom state code using entity customization (export/import)  with name "PreQualified" and value = 4 and added some status reason.
    when I'm tried out this new state code using simple on demand workflow, it works great. but when i tried to change it programmatically it throws an error.

    this is the error
    "  0x80040203
      An exception System.FormatException was thrown while trying to convert input value 'Pre-Qualified' to attribute 'lead.statecode'. Expected type of attribute value: System.Int32. Exception raised: Input string was not in a correct format.
      Platform"

    here is the some code:
                lead preQualifiedLead = new lead();

                preQualifiedLead.leadid = new CrmService.Key();
                preQualifiedLead.leadid.Value = new Guid(Label1.Text);
              
                preQualifiedLead.statecode = new LeadStateInfo();
                preQualifiedLead.statecode.Value = LeadState.PreQualified;

                Status statusCode = new Status();
                statusCode.Value = int.Parse(cmbStatusReason.SelectedValue);

                preQualifiedLead.statuscode = new Status();
                preQualifiedLead.statuscode = statusCode;

                crmService.Update(preQualifiedLead);

    fyi, the first time i'm customizing the lead, i use Pre-Qualified as the invariantname, but lately i'm changing it to PreQualified. did it get stuck somewhere?

    Thank you.
    Friday, February 5, 2010 3:03 AM

Answers

  • Yeah, i think may be u need to removed this and use the one which microsoft is providing you.
    I think for your case "Open" is good enough. may be u can add the status reason = "Pre-Qualified', against the "Open" status.
    Friday, February 5, 2010 6:47 AM

All replies

  • Hi regarding the statecode property it is not valid for the update operation, check this documentation from SDK.

    lead.statecode Property
    The properties statecode (Status) and statuscode (Status Reason) are linked. The statecode property is used internally to represent the status of the entity. The statuscode is used to display this value to the end-user. The set of valid state codes for an entity is not customizable. However, the status codes are customizable.

    The following table contains additional information about this property:

    Platform required No
    Application requirement level Read-only
    Valid for create No
    Valid for retrieve Yes
    Valid for update No


    http://msdn.microsoft.com/en-us/library/cc153210.aspx
    List of possible valid statecodes are.
    http://msdn.microsoft.com/en-us/library/bb959329.aspx

    Also if if you want to change the status of the lead, use SetStateLead Message
    http://msdn.microsoft.com/en-us/library/bb959541.aspx
    Friday, February 5, 2010 3:50 AM
  • That's what i'm looking for the SetStateLead Message.. thank you for pointing it out Ali.

    but it still throw an error.. -1 is not a valid Lead State. :(

    is there not a chance to add a custom state? if not can i execute a workflow first to change the state and then from the code i'm updating the status reason? because from workflow it's fine to change the state and i need to catch user input for the status reason..

    Thank you.
    Friday, February 5, 2010 4:05 AM
  • Yes -1 is not a valid state, you cannot use it. however in order to execute the workflow from the code, here is an example from the sdk.

    Example

    [C#]
    // Set up the CRM Service.
    CrmAuthenticationToken token = new CrmAuthenticationToken();
    // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
    token.AuthenticationType = 0; 
    token.OrganizationName = "AdventureWorksCycle";
     
    CrmService service = new CrmService();
    service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
    service.CrmAuthenticationTokenValue = token;
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
    // Create an ExecuteWorkflow request.
    ExecuteWorkflowRequest request = new ExecuteWorkflowRequest();
    
    //Assign the ID of the workflow you want to execute to the request.         
    request.WorkflowId = new Guid("b050f053-6968-dc11-bb3a-0003ffbad37a");
    
    //Assign the ID of the entity to execute the workflow on to the request.
    request.EntityId = new Guid("1DCDEE97-35BB-44BE-8353-58BC36592656");
    
    // Execute the workflow.
    ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)service.Execute(request);
    Friday, February 5, 2010 4:13 AM
  • Thank you for the sample Ali..it's work great.. but i still encounter an error when try to update the status of the lead.

    here is the code:
    ExecuteWorkflowRequest request = new ExecuteWorkflowRequest();
            
                request.WorkflowId = new Guid("8F89A245-9CD8-4E8F-802C-BBED5112B46C");
                request.EntityId = new Guid(Label1.Text);
                ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)crmService.Execute(request);
     //this workflow will set the state code to my custom state code.            
    
                lead preQualifiedLead = new lead();
    
                preQualifiedLead.leadid = new CrmService.Key();
                preQualifiedLead.leadid.Value = new Guid(Label1.Text);
                
                preQualifiedLead.statecode = new LeadStateInfo();
                preQualifiedLead.statecode.Value = LeadState.PreQualified;
    
                SetStateLeadRequest setState = new SetStateLeadRequest();
                setState.EntityId = new Guid(Label1.Text);
                //setState.LeadState = LeadState.PreQualified;
                setState.LeadStatus = int.Parse(cmbStatusReason.SelectedValue);
    //this will set the status reason to what the user select
                SetStateLeadResponse doneSetState = (SetStateLeadResponse)crmService.Execute(setState);
    it throw an error : Status reason 11 is not valid for state Open. 11 is my custom status reason for my custom state code.

    i suspect it's because the workflow is not yet finished. any advise how i should wait for the workflow to finished execute?

    i found ExecuteCompletedEventHandler and ExecuteCompletedEventArgs, but how do i use it? advise please..

    Thank you.
    Friday, February 5, 2010 5:49 AM
  • Hi,

    Why you are using this.
     
                preQualifiedLead.statecode = new LeadStateInfo();
                preQualifiedLead.statecode.Value = LeadState.PreQualified;

    as the documentation says this filed is not valid for update.

    The following table contains additional information about this property:

    Platform required No
    Application requirement level Read-only
    Valid for create No
    Valid for retrieve Yes
    Valid for update No


    Plus if you use the SetStateLead Message
    This message updates the lead entity properties statuscode and statecode.
    Can u replace setState.LeadStatus = int.Parse(cmbStatusReason.SelectedValue);
    with this and try if it work?(may be u don't need workflow then)
    setState.LeadState= LeadState.PreQualified;
    setState.LeadStatus = 11;

    i was wondering how you managed to add "PreQualified"? i am not sure that it's possible.? is it? I think StatusReason can be added but not sure whether you can add the "Status"?

    the leadStatus has following valid values.

    Members

    Name Value Description Status Reason Status Code Value
    Disqualified 2 Specifies that the lead is disqualified. Lost

    Cannot Contact

    No Longer Interested

    Canceled

    4

    5

    6

    7

    Open 0 Specifies that the lead is open. New

    Contacted

    1

    2

    Qualified 1 Specifies that the lead is qualified. Qualified 3


    http://msdn.microsoft.com/en-us/library/bb959329.aspx

    check this example for the account, on how to use it.
    state.AccountState = AccountState.Inactive
    state.AccountStatus = 2

    http://msdn.microsoft.com/en-us/library/bb959523.aspx

    Friday, February 5, 2010 6:10 AM
  • Hi Ali, i know it's Microsoft says the state is not customizable, but i just ignore it and add the new state from entity customization (the export/import). and it show up in the attributes in the customization. but it's look like it's not recognized by the crm when i execute the code.
    but it show up in the lead state enumeration..

    here is the code
                SetStateLeadRequest setState = new SetStateLeadRequest();
                setState.EntityId = new Guid(Label1.Text);
                setState.LeadState = LeadState.PreQualified;
    setState.LeadStatus = 11;//int.Parse(cmbStatusReason.SelectedValue); SetStateLeadResponse doneSetState = (SetStateLeadResponse)crmService.Execute(setState);
    it return error that say  "-1 is not a valid state code on lead". like i said in my previous post.

    why it looks like this line
    setState.LeadState = LeadState.PreQualified; //i'm not typing this code, it's there on the drop down completion.
    didn't work at all..

    i really lost here... :(

    thank you..
    Friday, February 5, 2010 6:40 AM
  • Yeah, i think may be u need to removed this and use the one which microsoft is providing you.
    I think for your case "Open" is good enough. may be u can add the status reason = "Pre-Qualified', against the "Open" status.
    Friday, February 5, 2010 6:47 AM
  • oouchh..  ooowww... it's my client request...  the Pre-Qualified need to be state code because it has it's own status reason (but the same as status reason for disqualified).

    *sigh*. okay...i'll try to propose then.. Thank you so much Ali, you're really helping me...
    Friday, February 5, 2010 7:22 AM