0

I have the following code which is executing a duplicate detection request (successfully when viewed through CRM) but when I attempt to query the result of the detection job, I am getting a null reference (object not set to instance of an object) when running the last line of the code.

The _response.jobid is not null and the connection to CRM is still valid at this point, as I ran a separate Entity.Retrieve (not included in the code) at this stage to make sure. Can someone please tell me what me are missing? This code was largely taken from the sample code but adapted for our custom entity.

            // Create the BulkDetectDuplicatesRequest object
            Console.WriteLine("Creating the BulkDetectDuplicatesRequest object");
            BulkDetectDuplicatesResponse _response;

            BulkDetectDuplicatesRequest request = new BulkDetectDuplicatesRequest()
            {
                JobName = "Detect Duplicate Supplier Invoices",
                Query = new QueryExpression()
                {
                    EntityName = "custentityname",
                    ColumnSet = new ColumnSet(true)
                },
                RecurrencePattern = String.Empty,
                RecurrenceStartTime = DateTime.Now,
                ToRecipients = new Guid[0],
                CCRecipients = new Guid[0]
            };

            // Execute the request
            Console.WriteLine("Executing BulkDetectDuplicatesRequest");
            _response = (BulkDetectDuplicatesResponse)_serviceProxy.Execute(request);

            try
            {
                BulkImportHelper.WaitForAsyncJobCompletion(_serviceProxy, _response.JobId);

                QueryByAttribute query8 = new QueryByAttribute()
                {
                    ColumnSet = new ColumnSet(true),
                };

                query8.Attributes.Add("asyncoperationid"); //from sample code
                query8.Values.Add(_response.JobId); //has  the job id of the duplicate detection job
                EntityCollection results8 = _serviceProxy.RetrieveMultiple(query8); //errors due to null reference exception 

}

Thanks

user2965112
  • 87
  • 2
  • 12
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – eddie_cat Oct 23 '14 at 15:29

1 Answers1

1

QueryByAttribute requires to specify the entity name

example for account entity:

QueryByAttribute querybyattribute = new QueryByAttribute("account"); // account entity
querybyattribute.ColumnSet = new ColumnSet("name", "address1_city", "emailaddress1");
querybyattribute.Attributes.AddRange("address1_city");
querybyattribute.Values.AddRange("Redmond");
EntityCollection retrieved = _service.RetrieveMultiple(querybyattribute);
Guido Preite
  • 13,789
  • 3
  • 32
  • 63