Esempio n. 1
0
        private Guid CreateIfNotExist_new_mydata(string name, Guid new_datacircularId, Guid new_myrefdataId, Guid buId, List <Guid> refDatas)
        {
            IOrganizationService service = SourceService;

            var  repo     = new EntityRepository(service, new ServiceRetryExecutor());
            Guid retValue = Guid.Empty;

            var records = service.GetEntitiesByColumn("new_mydata", "new_name", name);

            if (records.Entities.Count == 0)
            {
                var ent = new Entity("new_mydata");
                ent.Attributes["new_name"]       = name;
                ent.Attributes["new_bulookupid"] = new EntityReference("businessunit", buId);
                ent.Attributes["new_parentbuid"] = new EntityReference("businessunit", repo.GetParentBuId());

                if (new_datacircularId != Guid.Empty)
                {
                    ent.Attributes["new_datacircular"] = new EntityReference("new_mydata", new_datacircularId);
                }

                if (new_myrefdataId != Guid.Empty)
                {
                    ent.Attributes["new_myrefdataid"] = new EntityReference("new_myrefdata", new_myrefdataId);
                }

                retValue = service.Create(ent);
            }

            if (refDatas != null && retValue != Guid.Empty)
            {
                List <EntityWrapper> ents = new List <EntityWrapper>();
                foreach (var refDataId in refDatas)
                {
                    var manyToManyEnt = new Entity("new_new_mydata_new_myrefdata");
                    manyToManyEnt.Attributes["new_mydataid"]    = retValue;
                    manyToManyEnt.Attributes["new_myrefdataid"] = refDataId;
                    var wrap = new EntityWrapper(manyToManyEnt, true);
                    ents.Add(wrap);
                }

                if (ents.Any())
                {
                    repo.AssociateManyToManyEntity(ents);
                }
            }

            return(retValue);
        }
        public void AssociateManyToManyEntity()
        {
            testOrgService.ExecutionResponse = new RetrieveEntityResponse();

            MockRetryExecutor.Setup(a => a.Execute(It.IsAny <Func <ExecuteMultipleResponse> >()))
            .Returns(requestResult);
            ManyToManyDetails details = new ManyToManyDetails
            {
                Entity1IntersectAttribute = "contactid",
                Entity2IntersectAttribute = "accountid",
                IsManyToMany = true,
                SchemaName   = "accountcontact"
            };

            MockEntityMetadataCache.Setup(a => a.GetManyToManyEntityDetails(It.IsAny <string>()))
            .Returns(details);

            FluentActions.Invoking(() => systemUnderTest.AssociateManyToManyEntity(entities))
            .Should()
            .NotThrow();

            MockRetryExecutor.VerifyAll();
            MockEntityMetadataCache.VerifyAll();
        }