Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOrganisationService.Retrieve Record does not exist

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?

like image 998
Zain Avatar asked Oct 10 '16 08:10

Zain


1 Answers

Method Retrieve presumes that the record with the given ID actually exists in the system. In general it fails in only two scenarios:

  1. The record was deleted by another user or process.
  2. The user does not have sufficient read privileges.

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.

like image 156
Henk van Boeijen Avatar answered Oct 17 '22 04:10

Henk van Boeijen