Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Retrieve statuscode

Posted on by

How to get status code of retrieved entity?

I used ColumnSet(true) for the retrieved entity but entity.statuscode = null.

*This post is locked for comments

  • Zibba360 Profile Picture
    Zibba360 on at
    RE: Retrieve statuscode

    Passing the organization service as a parameter worked, I now get the statuscode value

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve statuscode

    Hi Zibba,

    the problem is probably here:

    "I have CRM SDK references and serviceProxy (service) on both CRMOperations class and asmx web service"

    Create a single crm service class at the beninning of web service/function and pass it as parameter in every function.

    Please let me know.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Zibba360 Profile Picture
    Zibba360 on at
    RE: Retrieve statuscode

    Below is my full code:

    [WebMethod]

           [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

           public string GetEntityData(string json, string byAcc, string userId)

           {

               if (String.IsNullOrEmpty(json))

                   return JsonConvert.SerializeObject($"{fail} : No data passed from mobile.");

               dynamic data = JObject.Parse(json);

               string polNumber = data["new_pol"];

               bool acc = false;

               if (byAcc.Equals("Yes"))

                   acc = true;

               Entity recordToCreate = new Entity

               {

                   serId = userId,

                   new_ByAcct = acc,

               };

               try

               {

                   var recordId = service.Create(recordToCreate);

                   var createdRecord = crmOperations.GetRecordsById("new_entity", new ColumnSet("new_entityid", "new_name", "statuscode"), "new_entityid", recordId);

                   if (createdRecord.Entities.Count < 1)

                   {

                       for (int i = 0; i < 5; i++)

                       {

                           Thread.Sleep(TimeSpan.FromSeconds(10));

                           createdRecord = crmOperations.GetRecordsById("new_entity", new ColumnSet("new_entityid", "new_name", "statuscode"), "new_entityid", recordId);

                           if (createdRecord.Entities.Count > 0)

                               i = 5;

                       }

                   }

                   if (createdRecord.Entities.Count < 1)

                       return JsonConvert.SerializeObject($"{fail} : Could not retrieve record.");

                   Entity retrievedRecord = createdRecord.Entities[0];

                   var entityRef = (EntityReference)(retrievedRecord["new_pol"]);

                   var userClaim = new

                   {

                       statusreason = retrievedRecord["statuscode"],

                       new_poly = entityRef.Name,

                       new_name = retrievedRecord["new_name"],

                   };

                   return JsonConvert.SerializeObject(userClaim);

               }

               catch (Exception ex)

               {

                   throw new Exception($"{fail} : {ex.Message}");

               }

           }

    Below is the GetRecordsById method which is in class named CRMOperations:

    public EntityCollection GetRecordsById(string logicalName, ColumnSet columnSet, string attributeName, Guid value)

           {

               QueryExpression query = new QueryExpression

               {

                   EntityName = logicalName,

                   ColumnSet = columnSet,

                   Criteria = new FilterExpression

                   {

                       Conditions =

                       {

                           new ConditionExpression

                           {

                               AttributeName = attributeName,

                               Operator = ConditionOperator.Equal,

                               Values = { value }

                           }

                       }

                   }

               };

               var results = service.RetrieveMultiple(query);

               return results;

           }

    I have CRM SDK references and serviceProxy (service) on both CRMOperations class and asmx web service

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Retrieve statuscode

    How are you creating the query? (what do you have in your code before that "if")

  • Zibba360 Profile Picture
    Zibba360 on at
    RE: Retrieve statuscode

    I added statuscode in my ColumnSet like...ColumnSet("new_entityid", "new_name", "statuscode")

    Tried to get the value like...((OptionSetValue)entity["statuscode"]).Value

    but I get ==>

    ((OptionSetValue)entity["statuscode"]).Value' threw an exception of type 'System.Collections.Generic.KeyNotFoundException'

       Data: {System.Collections.ListDictionaryInternal}

    I get other attribute values except statuscode and statecode

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve statuscode

    Hi Zibba,

    it could be a cache issue (specially when you deal with linq queries).

    Please dispose any context and/or use local (not global) context.

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Suggested answer
    imayur Profile Picture
    imayur 630 on at
    RE: Retrieve statuscode

    Hi,

    Just add "statuscode" and "statuscode" field in your Columnset for entity inside your query Expression.

    Here is how you can retrieve the statecode and statuscode value

    var stateCode = ((OptionSetValue)entityCollecrion.Entities[0]["statecode"]).Value;
    var statusCode = ((OptionSetValue)entityCollecrion.Entities[0]["statuscode"]).Value;


  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Retrieve statuscode

    I think statuscode is there, but, possibly, the serialization does not work.. Statuscode is returned as an OptionSetValue if I'm not mistaken.

    This might work:

    var results = new

                  {

                      statusreason = ((OptionSetValue)entity["statuscode"]).Value,

                      new_ref = entityRef.Name,

                      new_name = entity["new_name"],

                  };

    Did you try to debug, though? Or just throw an exception to see what's in that statuscode?

  • Zibba360 Profile Picture
    Zibba360 on at
    RE: Retrieve statuscode

    Hi Alex

    Here is a portion of my code. I create a json object with entity attributes and return it.

    if (entityCollection.Entities.Count < 1)

                       return JsonConvert.SerializeObject($"{fail} : Could not retrieve entity.");

                   var entity = entityCollection.Entities[0];

                   var entityRef = (EntityReference)(entity["new_ref"]);

                   var results = new

                   {

                       statusreason = entity["statuscode"],

                       new_ref = entityRef.Name,

                       new_name = entity["new_name"],

                   };

                   return JsonConvert.SerializeObject(results);

               }

               catch (Exception ex)

               {

                   throw new Exception($"{fail} : {ex.Message}");

               }

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Retrieve statuscode

    You might try

    entity["statuscode"]

    Although, your code looks ok.. Maybe you could post the rest of it?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans