/// <summary>
 /// Initializes a new instance of the <see cref="UPObjectivesGroup"/> class.
 /// </summary>
 /// <param name="groupKey">The group key.</param>
 /// <param name="objectives">The objectives.</param>
 /// <param name="configuration">The configuration.</param>
 public UPObjectivesGroup(string groupKey, UPObjectives objectives, UPObjectivesConfiguration configuration)
 {
     this.GroupKey       = groupKey;
     this.Objectives     = objectives;
     this.Configuration  = configuration;
     this.Items          = new List <UPObjectivesItem>();
     this.ItemDictionary = new Dictionary <string, UPObjectivesItem>();
 }
        /// <summary>
        /// Changeds the records.
        /// </summary>
        /// <returns></returns>
        public List <UPCRMRecord> ChangedRecords()
        {
            UPCRMRecord changedRecord = null;

            if (this.Deleted && this.Record != null)
            {
                changedRecord = new UPCRMRecord(this.Record.RecordIdentification, "Delete");
            }
            else
            {
                if (this.Created && this.Record == null)
                {
                    UPObjectives objectives = this.Group.Objectives;
                    changedRecord = new UPCRMRecord(this.Group.Configuration.DestinationFieldControl.InfoAreaId);
                    changedRecord.AddLink(new UPCRMLink(objectives.RecordIdentification));
                    for (int i = 0; i < this.AdditionalFields.Count; i++)
                    {
                        string newValue = this.values[i];
                        if (!string.IsNullOrEmpty(newValue))
                        {
                            UPConfigFieldControlField field = this.AdditionalFields[i];
                            changedRecord.NewValueFieldId(newValue, field.FieldId);
                        }
                    }
                }
                else if (this.Changed && this.Record != null)
                {
                    changedRecord = new UPCRMRecord(this.Record.RecordIdentification);
                    changedRecord.AddLink(new UPCRMLink(this.Group.Objectives.RecordIdentification));
                    for (int i = 0; i < this.AdditionalFields.Count; i++)
                    {
                        string originalValue = this.OriginalValues[i];
                        string newValue      = this.values[i];
                        if (originalValue != newValue)
                        {
                            UPConfigFieldControlField field = this.AdditionalFields[i];
                            changedRecord.NewValueFromValueFieldId(newValue, originalValue, field.FieldId);
                        }
                    }

                    if (this.completed != this.originalCompleted)
                    {
                        string sCompleted         = StringExtensions.CrmValueFromBool(this.completed);
                        string sOriginalCompleted = StringExtensions.CrmValueFromBool(this.originalCompleted);
                        if (this.Group.Configuration.FieldForFunction(Constants.FieldCompletedFunction) != null)
                        {
                            changedRecord.NewValueFromValueFieldId(sCompleted, sOriginalCompleted, this.Group.Configuration.FieldForFunction(Constants.FieldCompletedFunction).FieldId);
                        }

                        if (this.completed)
                        {
                            if (this.Group.Configuration.FieldForFunction(Constants.FieldCompletedOnFunction) != null)
                            {
                                changedRecord.NewValueFromValueFieldId(StringExtensions.CrmValueFromDate(DateTime.UtcNow), string.Empty, this.Group.Configuration.FieldForFunction(Constants.FieldCompletedOnFunction).FieldId);
                            }

                            if (this.Group.Configuration.FieldForFunction(Constants.FieldCompletedByFunction) != null)
                            {
                                changedRecord.NewValueFieldId(ServerSession.CurrentSession.CurRep, this.Group.Configuration.FieldForFunction(Constants.FieldCompletedByFunction).FieldId);
                            }
                        }
                    }
                }
            }

            return(changedRecord != null ? new List <UPCRMRecord> {
                changedRecord
            } : null);
        }