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);
        }
        private void VerifyRelationships(CustomisationImportRequest request)
        {
            var response = new CustomisationImportResponse();

            var relationShipmetadata =
                CustomisationImportService.ExtractRelationshipMetadataFromExcel(request.ExcelFile.FileName, Controller, response).Values;

            foreach (var metadata in relationShipmetadata)
            {
                Assert.IsTrue(XrmRecordService.GetManyToManyRelationships(metadata.RecordType1).Any(r => r.SchemaName == metadata.SchemaName));
                var relationshipMatch = XrmRecordService
                                        .GetManyToManyRelationships(metadata.RecordType1)
                                        .Where(r => r.SchemaName == metadata.SchemaName);
                Assert.IsTrue(relationshipMatch.Any());
                var relationship   = relationshipMatch.First();
                var loadedMetadata = XrmRecordService.GetManyRelationshipMetadata(relationship.SchemaName, relationship.RecordType1);
                Assert.AreEqual(loadedMetadata.RecordType1DisplayRelated, metadata.RecordType1DisplayRelated);
                Assert.AreEqual(loadedMetadata.RecordType2DisplayRelated, metadata.RecordType2DisplayRelated);
                Assert.AreEqual(loadedMetadata.RecordType1UseCustomLabel, metadata.RecordType1UseCustomLabel);
                Assert.AreEqual(loadedMetadata.RecordType2UseCustomLabel, metadata.RecordType2UseCustomLabel);
                if (metadata.RecordType1DisplayRelated)
                {
                    if (metadata.RecordType1UseCustomLabel)
                    {
                        Assert.AreEqual(loadedMetadata.RecordType1CustomLabel, metadata.RecordType1CustomLabel);
                    }
                    else
                    {
                        Assert.AreEqual(loadedMetadata.RecordType1CustomLabel, XrmRecordService.GetCollectionName(metadata.RecordType1));
                    }
                    Assert.AreEqual(loadedMetadata.RecordType1DisplayOrder, metadata.RecordType1DisplayOrder);
                }
                if (metadata.RecordType2DisplayRelated)
                {
                    if (metadata.RecordType2UseCustomLabel)
                    {
                        Assert.AreEqual(loadedMetadata.RecordType2CustomLabel, metadata.RecordType2CustomLabel);
                    }
                    else
                    {
                        Assert.AreEqual(loadedMetadata.RecordType2CustomLabel, XrmRecordService.GetCollectionName(metadata.RecordType2));
                    }
                    Assert.AreEqual(loadedMetadata.RecordType2DisplayOrder, metadata.RecordType2DisplayOrder);
                }
            }
        }