public void UpdateBaselineProjection(CustomBaselineProjection bp)
        {
            using (var ctx = new XrmServiceContext("Xrm"))
            {
                var bps = (from s in ctx.new_baselineprojectionsSet
                            where s.Id == bp.Id
                            select s).FirstOrDefault();
                bps.new_name = bp.Name;

                bps.new_Year = bp.Year;
                bps.new_CA_Q1Actual = (int)bp.CA_Q1_A;
                bps.new_Q1_ca = (decimal)bp.CA_Q1_P;
                bps.new_CA_Q2Actual = (int)bp.CA_Q2_A;
                bps.new_Q2_ca = (decimal)bp.CA_Q2_P;
                bps.new_CA_Q3Actual = (int)bp.CA_Q3_A;
                bps.new_Q3_ca = (decimal)bp.CA_Q3_P;
                bps.new_CA_Q4Actual = (int)bp.CA_Q4_A;
                bps.new_Q4_ca = (decimal)bp.CA_Q4_P;

                bps.new_DB_Q1Actual = (int)bp.DB_Q1_A;
                bps.new_Q1_db = (decimal)bp.DB_Q1_P;
                bps.new_DB_Q2Actual = (int)bp.DB_Q2_A;
                bps.new_Q2_db = (decimal)bp.DB_Q2_P;
                bps.new_DB_Q3Actual = (int)bp.DB_Q3_A;
                bps.new_Q3_db = (decimal)bp.DB_Q3_P;
                bps.new_DB_Q4Actual = (int)bp.DB_Q4_A;
                bps.new_Q4_db = (decimal)bp.DB_Q4_P;

                ctx.UpdateObject(bps);
                ctx.SaveChanges();
            }
        }
 private void IncreaseCurrentNumber(string entityName, int InitalValue)
 {
     using (XrmServiceContext context = new XrmServiceContext(OrganizationService))
     {
         var autoNumberDetail = context.dots_autonumberSet.FirstOrDefault(w => w.TargetEntityLogicalName == entityName);
         //autoNumberDetail.CurrentNumber = (Convert.ToInt32(autoNumberDetail.CurrentNumber) + 1).ToString();
         int currentNumber = ((autoNumberDetail.CurrentNumber == null) ? InitalValue : autoNumberDetail.CurrentNumber.Value);
         autoNumberDetail.CurrentNumber = ((currentNumber) + 1);
         context.UpdateObject(autoNumberDetail);
         context.SaveChanges();
     }
 }
        public void ResolveCase(Guid caseId)
        {
            try
            {
                using (var context = new XrmServiceContext(_service))
                {
                    _log.Info($"Executing resolve case for case with id {caseId}");

                    Incident selectedCase = (from incident in context.IncidentSet
                                             where incident.IncidentId.Equals(caseId)
                                             select incident)
                                            .FirstOrDefault();

                    if (selectedCase == null)
                    {
                        _log.Info($"No case found with id {caseId}");
                        return;
                    }

                    var statuses = (from status in context.New_requeststatusSet
                                    select status)
                                   .ToList();

                    //Set case status to "Completed"
                    selectedCase.new_caseid.Id = statuses[0].Id;

                    //Create Incident Resolution
                    var incidentResolution = new IncidentResolution
                    {
                        Subject    = "Case Resolved",
                        IncidentId = new EntityReference(Incident.EntityLogicalName, selectedCase.Id),
                        ActualEnd  = DateTime.Now
                    };

                    //Close Incident
                    var closeIncidenRequst = new CloseIncidentRequest
                    {
                        IncidentResolution = incidentResolution,
                        Status             = new OptionSetValue(5)
                    };

                    _service.Execute(closeIncidenRequst);

                    context.UpdateObject(selectedCase);
                    context.SaveChanges();
                    _log.Info($"Set case status to Completed for Case with ID {caseId}");
                }
            }
            catch (Exception ex)
            {
                _log.Error($"Exception caught while trying to resolve case with ID {caseId} - {ex.Message}");
            }
        }
        private static void UpdateContacts(OrganizationServiceProxy serviceProxy)
        {
            var context     = new XrmServiceContext(serviceProxy);
            var updateCount = 0;

            foreach (var contact in context.ContactSet)
            {
                Console.WriteLine(String.Format("{0} {1}|{2}|{3}|{4}", contact.FullName,
                                                contact.MobilePhone,
                                                contact.Telephone1,
                                                contact.Telephone2,
                                                contact.Telephone3));

                var newMobilePhone = SanitizeNumber(contact.MobilePhone);
                var newTelephone1  = SanitizeNumber(contact.Telephone1);
                var newTelephone2  = SanitizeNumber(contact.Telephone2);
                var newTelephone3  = SanitizeNumber(contact.Telephone3);

                if (newMobilePhone != contact.MobilePhone ||
                    newTelephone1 != contact.Telephone1 ||
                    newTelephone2 != contact.Telephone2 ||
                    newTelephone3 != contact.Telephone3)
                {
                    contact.MobilePhone = newMobilePhone;
                    contact.Telephone1  = newTelephone1;
                    contact.Telephone2  = newTelephone2;
                    contact.Telephone3  = newTelephone3;
                    context.UpdateObject(contact);
                    updateCount++;
                }

                if (updateCount == BatchSize)
                {
                    updateCount = 0;
                    context.SaveChanges();
                }
            }

            //final update
            if (updateCount > 0)
            {
                context.SaveChanges();
            }
        }
        private static void UpdateAccounts(OrganizationServiceProxy serviceProxy)
        {
            var context     = new XrmServiceContext(serviceProxy);
            var updateCount = 0;

            foreach (var account in context.AccountSet)
            {
                Console.WriteLine(String.Format("{0} {1}|{2}|{3}", account.Name,
                                                account.Telephone1,
                                                account.Telephone2,
                                                account.Telephone3));

                var newTelephone1 = SanitizeNumber(account.Telephone1);
                var newTelephone2 = SanitizeNumber(account.Telephone2);
                var newTelephone3 = SanitizeNumber(account.Telephone3);

                if (newTelephone1 != account.Telephone1 ||
                    newTelephone2 != account.Telephone2 ||
                    newTelephone3 != account.Telephone3)
                {
                    account.Telephone1 = newTelephone1;
                    account.Telephone2 = newTelephone2;
                    account.Telephone3 = newTelephone3;
                    context.UpdateObject(account);
                    updateCount++;
                }

                if (updateCount == BatchSize)
                {
                    updateCount = 0;
                    context.SaveChanges();
                }
            }

            //final update
            if (updateCount > 0)
            {
                context.SaveChanges();
            }
        }
        public Account UpdateStudentDetails(string studentId, DetailsViewModel model)
        {
            try
            {
                //Check if studentId is null
                if (string.IsNullOrWhiteSpace(studentId))
                {
                    _log.Error($"Trying to execute update student details for non existing student id");
                    return(null);
                }

                using (var context = new XrmServiceContext(_service))
                {
                    _log.Info($"Executing update student details for student with id {studentId}");
                    var studentObject = (from student in context.AccountSet
                                         where student.New_UserID.Equals(studentId)
                                         select student)
                                        .FirstOrDefault();

                    if (studentObject == null)
                    {
                        _log.Info($"No student found with id {studentId}");
                        return(null);
                    }

                    if (studentObject.New_FirstName != model.FirstName || studentObject.New_FamilyName != model.LastName ||
                        studentObject.New_StudentStatus.Value != (int)model.StudentStatus)
                    {
                        if (studentObject.New_FirstName != model.FirstName)
                        {
                            string oldFirstName = studentObject.New_FirstName;
                            studentObject.New_FirstName = model.FirstName;
                            _log.Info($"Updated the first name for student with ID {studentId} from {oldFirstName} to {model.FirstName}");
                        }

                        if (studentObject.New_FamilyName != model.LastName)
                        {
                            string oldLastName = studentObject.New_FamilyName;
                            studentObject.New_FamilyName = model.LastName;
                            _log.Info($"Updated the last name for student with ID {studentId} from {oldLastName} to {model.LastName} ");
                        }

                        if (studentObject.New_StudentStatus.Value != (int)model.StudentStatus)
                        {
                            int oldStudentStatus = studentObject.New_StudentStatus.Value;
                            studentObject.New_StudentStatus = new OptionSetValue((int)model.StudentStatus);
                            _log.Info($"Updated the student status for student with ID {studentId} from {(StudentStatus)oldStudentStatus} to {model.StudentStatus} ");
                        }

                        context.UpdateObject(studentObject);
                        context.SaveChanges();
                    }
                    else
                    {
                        _log.Info($"The new entered values are equal to the old ones");
                    }

                    return(studentObject);
                }
            }
            catch (Exception ex)
            {
                _log.Error($"Exception caught while trying to update Student Details for Student with ID {studentId} - {ex.Message}");
                return(null);
            }
        }