I've made code to check if a record in CRM exists. Problem is that the IOrganisationService.Retrieve returns an error instead of a null when no record is found. I expect multiple records not to be found and I do not want to have to use a try catch then use the error from the catch.
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null))
{
IOrganizationService service = (IOrganizationService)serviceProxy;
//Get record
var record = service.Retrieve(entryId, guid, new ColumnSet(true)); //switch to var if no work
//Check if record is not null/empty
recordExists = !String.IsNullOrWhiteSpace(record.Id.ToString()); //<- does the record exist
}
Suggestions?
Method Retrieve
presumes that the record with the given ID actually exists in the system. In general it fails in only two scenarios:
When you need to query for data that may not exist or when a query can yield zero, one or more records, use QueryExpression
queries; you can issue these queries using the RetrieveMultiple
method. If you like, you can also use LINQ for CRM, which basically wraps the QueryExpression
functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With