public void cleanupContacts()
        {
            DateTime             DateFilter = DateTime.Now.AddDays(-1);
            QueryExpression      query      = new QueryExpression("account");
            IOrganizationService service    = CRMHelper.ConnectToMSCRM1();

            Console.WriteLine("IOrganization Service Object Created");
            EntityCollection       EntityList   = CRMHelper.RetrieveMultiple(query, service);
            ExecuteMultipleRequest mulitRequest = null;

            int offset = 1;

            Console.WriteLine("Total Record to be Deleted are : " + EntityList.Entities.Count);
            mulitRequest = getNewObject();
            for (int count = 0; count < EntityList.Entities.Count; count++)
            {
                if (offset % 200 == 0)
                {
                    // Execute all the requests in the request collection using a single web method call.
                    ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(mulitRequest);
                    Console.WriteLine("Bunch of 200 Record Deleted  & Total record Proceed are : " + count);
                    mulitRequest = getNewObject();
                }
                Entity        Item          = EntityList.Entities[count];
                DeleteRequest deleteRequest = new DeleteRequest {
                    Target = Item.ToEntityReference()
                };
                mulitRequest.Requests.Add(deleteRequest);
                offset = count + 1;
            }
        }
        public void ZipCodeActions()
        {
            IOrganizationService service = CRMHelper.ConnectToMSCRM1();

            // Retrieval
            QueryExpression query = new QueryExpression("new_zipcode");

            query.TopCount  = 10;
            query.ColumnSet = new ColumnSet(new string[] { "new_state", "new_city" });

            EntityCollection coll = service.RetrieveMultiple(query);

            //Update
            Entity UpdateItem = new Entity("new_zipcode");

            UpdateItem.Id = coll.Entities[0].Id;
            UpdateItem.Attributes.Add("new_state", coll.Entities[0].Attributes["new_state"].ToString());
            service.Update(UpdateItem);

            //Create
            Entity AddItem = new Entity("new_zipcode");

            AddItem.Attributes.Add("new_state", "LA");
            service.Create(AddItem);
        }
Beispiel #3
0
        public void updateUSZipCode()
        {
            IOrganizationService service       = CRMHelper.ConnectToMSCRM1();
            EntityCollection     usZipCodeList = getAllUSZipCode(service);


            Console.WriteLine("Total Zip Code to Proceed are : " + usZipCodeList.Entities.Count);

            EntityCollection newEntityCollection = new EntityCollection();

            foreach (Entity item in usZipCodeList.Entities)
            {
                newEntityCollection.Entities.Add(item);
                //if(item.Attributes.Contains("new_country") && item.Attributes["new_country"] != null)
                //{

                //}
                //else
                //{
                //    newEntityCollection.Entities.Add(item);
                //}
            }

            Console.WriteLine("Total Records to Proceed : " + newEntityCollection.Entities.Count);
            foreach (Entity item in newEntityCollection.Entities)
            {
                Entity newEntity = new Entity(item.LogicalName);
                newEntity.Id = item.Id;
                newEntity.Attributes.Add("new_country", "CA");
                service.Update(newEntity);
            }

            //ExecuteMultipleRequest mulitRequest = null;
            //for (int count = 0; count < newEntityCollection.Entities.Count; count++)
            //{
            //    if(count == 0 || count % 50 == 0)
            //        mulitRequest = getNewObject();


            //    Entity Item = newEntityCollection.Entities[count];
            //    Entity newEntity = new Entity(Item.LogicalName);
            //    newEntity.Id = Item.Id;
            //    newEntity.Attributes.Add("new_country", "CA");
            //    UpdateRequest updateRequest = new UpdateRequest { Target = newEntity };
            //    mulitRequest.Requests.Add(updateRequest);

            //    if ((count + 1) % 50 == 0 || count == newEntityCollection.Entities.Count)
            //    {
            //        // Execute all the requests in the request collection using a single web method call.
            //        ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(mulitRequest);
            //        Console.WriteLine("Bunch of 1000 Record Deleted  & Total record Proceed are :" + count);
            //    }

            //}
        }
        public void updateContactOwner()
        {
            QueryExpression query = new QueryExpression("contact");

            query.ColumnSet = new ColumnSet(new string[] { "new_parentaccount", "ownerid", "fullname" });
            LinkEntity contactLink = new LinkEntity("contact", "account", "new_parentaccount", "accountid", JoinOperator.Inner);

            contactLink.EntityAlias = "Company";
            contactLink.Columns     = new ColumnSet(new string[] { "ownerid" });
            query.LinkEntities.Add(contactLink);
            IOrganizationService   service      = CRMHelper.ConnectToMSCRM1();
            EntityCollection       contactList  = CRMHelper.RetrieveMultiple(query, service);
            ExecuteMultipleRequest mulitRequest = null;
            int offset = 1;

            Console.WriteLine("Total Record to be Deleted are : " + contactList.Entities.Count);
            mulitRequest = getNewObject();
            for (int count = 0; count < contactList.Entities.Count; count++)
            {
                Guid   OwnerId     = Guid.Empty;
                string fullName    = string.Empty;
                string accountName = string.Empty;
                if (contactList.Entities[count].Attributes.Contains("fullname"))
                {
                    fullName = contactList.Entities[count].Attributes["fullname"].ToString();
                }

                AliasedValue    val        = (AliasedValue)contactList.Entities[count].Attributes["Company.ownerid"];
                EntityReference ownerIdRef = (EntityReference)val.Value;
                OwnerId = ownerIdRef.Id;

                accountName = ownerIdRef.Name;
                Guid   ContactOwner = contactList.Entities[count].GetAttributeValue <EntityReference>("ownerid").Id;
                Entity contact      = new Entity("contact");
                if (ContactOwner != OwnerId)
                {
                    Console.WriteLine("Miss-Match Owner of For Contact : " + fullName + " And Account is : " + accountName);
                    contact.Id = contactList.Entities[count].Id;
                    contact.Attributes.Add("ownerid", new EntityReference("systemuser", OwnerId));
                    UpdateRequest updateRequest = new UpdateRequest {
                        Target = contact
                    };
                    mulitRequest.Requests.Add(updateRequest);
                }
            }
            ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(mulitRequest);

            Console.ReadLine();
        }
        public void updatePhnAccountsPrimaryAddress()
        {
            QueryExpression query = new QueryExpression("account");

            query.ColumnSet = new ColumnSet(new string[] { "name", "new_addressstaging_county", "new_addressstaging_postalcode", "new_addressactual_county", "address1_postalcode" });
            query.Criteria.AddCondition("new_addressactual_county", ConditionOperator.Null);

            IOrganizationService service     = CRMHelper.ConnectToMSCRM1();
            EntityCollection     accountList = service.RetrieveMultiple(query);

            Console.WriteLine("Total Account Records : " + accountList.Entities.Count);
            for (int count = 0; count < accountList.Entities.Count; count++)
            {
                Entity singleAccount = accountList.Entities[count];
                Entity updateAccount = new Entity(singleAccount.LogicalName);
                updateAccount.Id = singleAccount.Id;
                bool isRequiredUpdate = false;

                if (singleAccount.Attributes.Contains("address1_postalcode") && singleAccount.Attributes["address1_postalcode"] != null)
                {
                    string          stagingzipCode = singleAccount.Attributes["address1_postalcode"].ToString();
                    EntityReference stagingCounty  = getCountyFromZipCodeEntity(stagingzipCode, service);
                    if (stagingCounty != null)
                    {
                        updateAccount.Attributes.Add("new_addressactual_county", new EntityReference(stagingCounty.LogicalName, stagingCounty.Id));
                        isRequiredUpdate = true;
                    }
                }

                //if (singleAccount.Attributes.Contains("address2_postalcode") && singleAccount.Attributes["address2_postalcode"] != null)
                //{
                //    string actualzipCode = singleAccount.Attributes["address2_postalcode"].ToString();
                //    EntityReference actualCounty = getCountyFromZipCodeEntity(actualzipCode, service);
                //    if (actualCounty != null)
                //    {
                //        updateAccount.Attributes.Add("new_address2actual_county", new EntityReference(actualCounty.LogicalName, actualCounty.Id));
                //        isRequiredUpdate = true;
                //    }
                //}


                if (isRequiredUpdate)
                {
                    service.Update(updateAccount);
                }

                Console.WriteLine("Current Record Count : " + count);
            }
        }
        public void deactiveContacts(string EntityName)
        {
            DateTime        DateFilter = DateTime.Now.AddDays(-1);
            QueryExpression query      = new QueryExpression(EntityName);

            query.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
            IOrganizationService service = CRMHelper.ConnectToMSCRM1();

            Console.WriteLine("IOrganization Service Object Created");
            EntityCollection       EntityList   = CRMHelper.RetrieveMultiple(query, service);
            ExecuteMultipleRequest mulitRequest = null;

            int offset = 1;

            Console.WriteLine("Total Record to be Deleted are : " + EntityList.Entities.Count);
            mulitRequest = getNewObject();
            for (int count = 0; count < EntityList.Entities.Count; count++)
            {
                if (offset % 200 == 0)
                {
                    // Execute all the requests in the request collection using a single web method call.
                    ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(mulitRequest);
                    Console.WriteLine("Bunch of 200 Record Deleted  & Total record Proceed are : " + count);
                    mulitRequest = getNewObject();
                }
                Entity Item = EntityList.Entities[count];
                //StateCode = 1 and StatusCode = 2 for deactivating Account or Contact
                SetStateRequest setStateRequest = new SetStateRequest()
                {
                    EntityMoniker = new EntityReference
                    {
                        Id          = Item.Id,
                        LogicalName = Item.LogicalName
                    },
                    State  = new OptionSetValue(1),
                    Status = new OptionSetValue(2)
                };
                mulitRequest.Requests.Add(setStateRequest);
                offset = count + 1;
            }
        }