コード例 #1
0
        /// <summary>
        /// This method responsible for updating record in CRM
        /// Based on the Attribute type, it takes the attrbite and update the record in CRM
        /// </summary>
        /// <param name="entityToUpd"></param>
        /// <param name="crmHelper"></param>
        /// <param name="updateAttributeStructure"></param>
        /// <param name="recordNumber"></param>
        /// <param name="stringBuilder"></param>
        /// <returns></returns>
        private bool UpdateRecord(Entity entityToUpd, CrmHelper crmHelper, UpdateAttributeStructure updateAttributeStructure, int recordNumber, ref StringBuilder stringBuilder)
        {
            string attrType        = updateAttributeStructure.UpdateAttrDataType;
            var    fileRecord      = updateAttributeStructure.FileRecord;
            var    updAttrName     = updateAttributeStructure.UpdateAttrName;
            var    allowedRefTypes = (Enum.GetValues(typeof(AllowedRefAttrType))).OfType <object>().Select(o => o.ToString()).ToArray();
            var    allowedAllTypes = (Enum.GetValues(typeof(AllowedAttributeType))).OfType <object>().Select(o => o.ToString()).ToArray();

            var whetherToProcess = Array.Exists(allowedAllTypes, element => element.ToLower() == attrType.ToLower());

            if (!whetherToProcess)
            {
                stringBuilder.AppendLine($"Attribute selected for update is of type {attrType}, is currently unsupported.");
                return(true);
            }

            if (string.IsNullOrEmpty(fileRecord.UpdateAttrValue))
            {
                entityToUpd[updAttrName] = null;
            }
            if (attrType.Equals(AttributeType.String.ToString(), StringComparison.CurrentCultureIgnoreCase))
            {
                entityToUpd[updAttrName] = fileRecord.UpdateAttrValue.ToString();
            }
            else if (attrType.Equals(AttributeType.Integer.ToString(), StringComparison.CurrentCultureIgnoreCase))
            {
                entityToUpd[updAttrName] = Convert.ToInt32(fileRecord.UpdateAttrValue);
            }
            else if (attrType.Equals(AttributeType.Double.ToString(), StringComparison.CurrentCultureIgnoreCase))
            {
                entityToUpd[updAttrName] = Convert.ToDouble(fileRecord.UpdateAttrValue);
            }
            else if (attrType.Equals(AttributeType.Decimal.ToString(), StringComparison.CurrentCultureIgnoreCase))
            {
                entityToUpd[updAttrName] = Convert.ToDecimal(fileRecord.UpdateAttrValue);
            }
            else if (attrType.Equals(AttributeType.Boolean.ToString(), StringComparison.CurrentCultureIgnoreCase))
            {
                entityToUpd[updAttrName] = Convert.ToBoolean(bool.Parse(fileRecord.UpdateAttrValue));
            }
            else if (attrType.Equals(AttributeType.DateTime.ToString(), StringComparison.CurrentCultureIgnoreCase))
            {
                entityToUpd[updAttrName] = DateTime.ParseExact(fileRecord.UpdateAttrValue, "M/d/yyyy h:mm", null);
            }
            else if (attrType.Equals(AttributeType.Picklist.ToString(), StringComparison.CurrentCultureIgnoreCase) ||
                     attrType.Equals(AttributeType.State.ToString(), StringComparison.CurrentCultureIgnoreCase) ||
                     attrType.Equals(AttributeType.Status.ToString(), StringComparison.CurrentCultureIgnoreCase))
            {
                entityToUpd[updAttrName] = new OptionSetValue(Convert.ToInt32(fileRecord.UpdateAttrValue));
            }
            else if (attrType.Equals(AttributeType.Money.ToString(), StringComparison.CurrentCultureIgnoreCase))
            {
                entityToUpd[updAttrName] = new Money(Convert.ToDecimal(fileRecord.UpdateAttrValue));
            }
            else if (Array.Exists(allowedRefTypes, element => element.ToLower() == attrType.ToLower()))
            {
                var             tagetEntityName      = updateAttributeStructure.TargetEntityName;
                var             targetEntityAttrName = updateAttributeStructure.TargetEntityAttrName;
                QueryExpression qTargetEntity        = new QueryExpression(tagetEntityName);
                qTargetEntity.ColumnSet.AddColumns(targetEntityAttrName);
                qTargetEntity.Criteria.AddCondition(targetEntityAttrName, ConditionOperator.Equal, fileRecord.UpdateAttrValue);

                var ecTargetEntity = crmHelper.GetEntityCollection(qTargetEntity);

                if (ecTargetEntity.Entities.Count == 0)
                {
                    stringBuilder.AppendLine($"Record : {recordNumber + 1} , No record found for the entity {tagetEntityName} for Attribute {targetEntityAttrName} with value - {fileRecord.UpdateAttrValue} ");
                    return(false);
                }
                if (ecTargetEntity.Entities.Count > 1)
                {
                    stringBuilder.AppendLine($"Record: { recordNumber + 1} ,More than one ({ecTargetEntity.Entities.Count}) record found for the entity {tagetEntityName} for Attribute {targetEntityAttrName} with value - {fileRecord.UpdateAttrValue} ");
                    return(false);
                }
                if (ecTargetEntity.Entities.Count == 1)
                {
                    if (attrType.Equals(AttributeType.PartyList.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        Entity activityParty = new Entity("activityparty");
                        activityParty["partyid"] = ecTargetEntity.Entities[0].ToEntityReference();
                        var parties = (EntityCollection)entityToUpd[updAttrName];
                        parties.Entities.Add(activityParty);
                        entityToUpd[updAttrName] = parties;
                    }
                    else
                    {
                        entityToUpd[updAttrName] = ecTargetEntity.Entities[0].ToEntityReference();
                    }
                }
            }
            crmHelper.UpdateRecord(entityToUpd);
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Method responsible to update record in CRM
        /// It performs different validation before updaing the record and calls the update record method
        /// </summary>
        /// <param name="keyValuePairs"></param>
        /// <param name="stringBuilder"></param>
        private void ProcessLoadInCrm(List <FileUpdateModel> keyValuePairs, ref StringBuilder stringBuilder)
        {
            UpdateAttributeStructure updateAttributeStructure = new UpdateAttributeStructure();

            var entityName = selectEntitiesComboBox.SelectedItem != null ? ((EntityMetadata)selectEntitiesComboBox.SelectedItem).LogicalName : null;

            if (entityName == null)
            {
                stringBuilder.AppendLine("Entity Name not selected, can not proceed load");
                return;
            }
            updateAttributeStructure.EntityName = entityName;

            var selectedAttr = selectAttributeComboBox.SelectedItem != null ? (AttributeMetadata)selectAttributeComboBox.SelectedItem : null;

            if (entityName == null)
            {
                stringBuilder.AppendLine("Attribute to be updated not selected, can not procees load");
                return;
            }

            var selectedAttrType = ((AttributeMetadata)selectAttributeComboBox.SelectedItem).AttributeType;
            var selectedAttrName = ((AttributeMetadata)selectAttributeComboBox.SelectedItem).LogicalName;

            updateAttributeStructure.UpdateAttrName     = selectedAttrName;
            updateAttributeStructure.UpdateAttrDataType = selectedAttrType;

            var allowedRefTypes = (Enum.GetValues(typeof(AllowedRefAttrType))).OfType <object>().Select(o => o.ToString()).ToArray();

            var selectedAttrIsRef = Array.Exists(allowedRefTypes, element => element == selectedAttrType);

            string targetEntityName     = null;
            string targetEntityAttrName = null;

            if (selectedAttrIsRef == true)
            {
                targetEntityName = targetEntityComboBox.SelectedItem != null?targetEntityComboBox.SelectedItem.ToString() : null;

                if (targetEntityName == null)
                {
                    stringBuilder.AppendLine("Attribute to be updated is a reference attribute, target entity is not selected for it, can not proceed load");
                    return;
                }
                targetEntityAttrName = targetAttributecomboBox.SelectedItem != null ? ((AttributeMetadata)targetAttributecomboBox.SelectedItem).LogicalName : null;
                updateAttributeStructure.TargetEntityName = targetEntityName;
                if (targetEntityAttrName == null)
                {
                    stringBuilder.AppendLine("Attribute to be updated is a reference attribute, target entity Attrbite is not selected for it, can not proceed load");
                    return;
                }
                updateAttributeStructure.TargetEntityAttrName = targetEntityAttrName;
            }

            var uniqueAttributeName = ((AttributeMetadata)selectUniqueAttributeComboBox.SelectedItem).LogicalName;

            if (uniqueAttributeName == null)
            {
                stringBuilder.AppendLine("Attribute to be used as first column not selected, can not procees load");
                return;
            }
            updateAttributeStructure.UniqueAttrName = uniqueAttributeName;
            int succescCount = 0;

            for (int i = 0; i < keyValuePairs.Count; i++)
            {
                try
                {
                    var item = keyValuePairs[i];
                    updateAttributeStructure.FileRecord = item;
                    var crmHelper     = new CrmHelper(Service);
                    var activeRecords = activeRecordsCheckBox.Checked;

                    QueryExpression qEntity = new QueryExpression(entityName);
                    qEntity.ColumnSet.AddColumns(uniqueAttributeName, selectedAttrName);
                    qEntity.Criteria.AddCondition(uniqueAttributeName, ConditionOperator.Equal, item.UniqueAttrValue);
                    if (activeRecords)
                    {
                        qEntity.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
                    }

                    var ecEntity = crmHelper.GetEntityCollection(qEntity);
                    if (ecEntity.Entities.Count == 0)
                    {
                        stringBuilder.AppendLine($"Record : {i + 1} , No record or No Active found for the entity {entityName} for Attribute {uniqueAttributeName} with value - {item.UniqueAttrValue} ");
                    }
                    if (ecEntity.Entities.Count > 1)
                    {
                        stringBuilder.AppendLine($"Record: { i + 1} ,More than one ({ecEntity.Entities.Count}) record found for the entity {entityName} for Attribute {uniqueAttributeName} with value - {item.UniqueAttrValue} ");
                    }
                    if (ecEntity.Entities.Count == 1)
                    {
                        if (UpdateRecord(ecEntity.Entities[0], crmHelper, updateAttributeStructure, i, ref stringBuilder))
                        {
                            succescCount = succescCount + 1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    stringBuilder.AppendLine($"Record: { i + 1}, Failed, {ex.Message}");
                }
            }
            stringBuilder.AppendLine($"Total Records Processed : {succescCount}");
        }