public ActionResult CreateDisbursementLists(List <Entry> entries, string sessionId) { List <long> deptIds = new List <long>(); List <DisbursementList> disbursementLists = new List <DisbursementList>(); if (entries != null) { foreach (var entry in entries) { if (deptIds.Contains(entry.deptId)) { } else { deptIds.Add(entry.deptId); } } } List <EmailNotification> notices = new List <EmailNotification>(); foreach (var deptId in deptIds) { List <DisbursementListDetails> disbursementListDetails = new List <DisbursementListDetails>(); Department dept = new Department() { DeptId = deptId }; DisbursementList d = new DisbursementList() { Department = dept, DisbursementListDetails = disbursementListDetails, date = entries[0].collectionDate }; string repMail = RequisitionService.GetRep(d.Department.DeptId); //change to rep EmailNotification notice = new EmailNotification(); notice.ReceiverMailAddress = repMail; notice.CollectionDate = d.date.ToString("dd/MM/yyyy"); notices.Add(notice); foreach (var entry in entries) { if (entry.deptId == deptId) { Inventory i = new Inventory() { ItemId = entry.itemId, }; DisbursementListDetails dDets = new DisbursementListDetails() { Item = i, Quantity = entry.quantity }; d.DisbursementListDetails.Add(dDets); } } disbursementLists.Add(d); } DisbursementListService.CreateDisbursementLists(disbursementLists); Task.Run(() => { foreach (var notice in notices) { emailService.SendMail(notice, EmailTrigger.ON_DISBURSEMENT_CREATION); //need to send to multiple reps } }); return(Json(Url.Action("ViewAllDisbursements", "Disbursement", new { sessionId = sessionId }))); }