private IRecord GetRecordToDeployInto(string recordType, string nameToMatch, string containingFolderParentName) { nameToMatch = nameToMatch?.Replace("∕", "/"); var conditions = new List <Condition> { new Condition(Service.GetPrimaryField(recordType), ConditionType.Equal, nameToMatch) }; if (recordType == Entities.adx_webpage) { conditions.Add(new Condition(Fields.adx_webpage_.adx_rootwebpageid, ConditionType.NotNull)); } IRecord matchingRecord = null; var matchingRecords = Service.RetrieveAllAndClauses(recordType, conditions); if (matchingRecords.Count() == 0) { throw new NullReferenceException($"Could not find {Service.GetDisplayName(recordType)} named {nameToMatch} to update"); } else if (matchingRecords.Count() == 1) { matchingRecord = matchingRecords.First(); } else { if (containingFolderParentName == null) { throw new NullReferenceException($"There are multiple {Service.GetCollectionName(recordType)} named {nameToMatch} and the parents parent folder does not provide the name of a {Service.GetDisplayName(Entities.adx_website)} to deploy into"); } else { var websiteLookupFields = Service .GetFields(recordType) .Where(f => Service.GetLookupTargetType(f, recordType) == Entities.adx_website) .ToArray(); if (websiteLookupFields.Count() != 1) { throw new NullReferenceException($"There are multiple {Service.GetCollectionName(recordType)} named {nameToMatch} but the type does not contain 1 lookup field referencing the {Service.GetDisplayName(Entities.adx_website)} to deploy into"); } var websiteFieldName = websiteLookupFields.First(); matchingRecords = matchingRecords.Where(r => r.GetLookupName(websiteFieldName)?.ToLower() == containingFolderParentName.ToLower()); if (matchingRecords.Count() == 0) { throw new NullReferenceException($"There are multiple {Service.GetCollectionName(recordType)} named {nameToMatch} but none of them are linked to a {Service.GetDisplayName(Entities.adx_website)} named {containingFolderParentName}"); } if (matchingRecords.Count() > 1) { throw new NullReferenceException($"There are multiple {Service.GetCollectionName(recordType)} named {nameToMatch} linked to a {Service.GetDisplayName(Entities.adx_website)} named {containingFolderParentName}"); } matchingRecord = matchingRecords.First(); } } return(matchingRecord); }