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
Passing the organization service as a parameter worked, I now get the statuscode value
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
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
How are you creating the query? (what do you have in your code before that "if")
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
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
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;
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?
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}");
}
You might try
entity["statuscode"]
Although, your code looks ok.. Maybe you could post the rest of it?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156