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
}
왜 try/catch를 사용하지 않으시겠습니까? 내 대답을 참조하십시오. – Phil