2016-10-10 3 views
2

CRM 레코드가 있는지 확인하는 코드를 만들었습니다. 문제는 IOrganisationService.Retrieve가 레코드가 발견되지 않을 때 null 대신 오류를 반환한다는 것입니다. 여러 레코드를 찾을 수 없기 때문에 try catch를 사용하지 않고 catch에서 오류를 사용하려고합니다.IOrganisationService.Retrieve 레코드가 없습니다.

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 
      } 

제안 사항? Retrieve 지정된 ID를 가진 레코드가 실제로 시스템에 존재한다는 것을 가정

// Create and cache the plugin context 
this.context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
this.service = serviceFactory.CreateOrganizationService(this.context.UserId); 

// Retrieve users with certain ID 
Guid userId = Guid.NewGuid(); 
var query = new QueryExpression("systemuser"); 
query.Criteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, userId)); 

EntityCollection users; 
try 
{ 
    users = this.service.RetrieveMultiple(query); 
} 
catch (FaultException<OrganizationServiceFault> faultException) 
{ 
    throw new InvalidPluginExecutionException($"Failed to retrieve system users that have an ID of '{userId}'", faultException); 
} 

if (users.Entities.Length == 0) // (or !users.Entities.Any() using Linq) 
{ 
    // Do something 
} 
+0

왜 try/catch를 사용하지 않으시겠습니까? 내 대답을 참조하십시오. – Phil

답변

1

사용 RetrieveMultiple 당신이 관심있는 ID와 사용자를 검색 할 수 있습니다. 일반적으로 두 시나리오에서만 실패합니다.

  1. 레코드가 다른 사용자 또는 프로세스에 의해 삭제되었습니다.
  2. 사용자에게 충분한 읽기 권한이 없습니다.

존재하지 않을 수도 있고 쿼리가 0 개 이상의 레코드를 생성 할 수있는 경우에는 QueryExpression 쿼리를 사용하십시오. RetrieveMultiple 메소드를 사용하여이 u 리를 발행 할 수 있습니다. 원하는 경우 기본적으로 QueryExpression 기능을 래핑하는 CRM 용 LINQ를 사용할 수도 있습니다.

+0

코드가 작동합니다. 아마도 내 설명이 약간 나빴습니다. 결과를 찾을 수없는 경우 Retrieve 메소드 오류. 레코드가 존재하지 않는다고 말하기 위해 catch를 사용하지 않고도 어떤 CRM 레코드가 존재하지 않는지 알고 싶습니다. – Zain

+0

'Retrieve' 메서드를 사용해서는 가능하지 않다고 생각합니다. CRM 서비스는 레코드가 존재하지 않으면'FaultException '유형의 예외를 throw합니다. 그러나 당신이 할 수있는 것은'RetrieveMultiple'을 사용하여 관심있는 ID를 가진 레코드의 목록을 검색하는 것입니다. 길이가 0이면 예외가 발생하지 않습니다. 내 대답을 업데이트했습니다. – Phil

+0

'Retrieve' 메쏘드는 지정된 레코드를 반환해야하고, 드문 경우에만 호출이 실패합니다. 따라서 오류 처리는 가능한 가장 높은 수준에서 처리해야합니다. –

2

방법 :