I have an Invoice entity and it has child InvoiceLog entities. When I first create an Invoice and add its InvoiceLog entities and save, it works fine. However, if I then edit the Invoice and try to add additional InvoiceLog entities, it completely ignores the new InvoiceLog entities and doesn't save them at all. Any ideas what I'm doing wrong?
//POST: /Secure/Invoices/Save/
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(Invoice invoice)
{
invoice.UpdateDate = DateTime.Now;
invoice.DeveloperID = Developer.DeveloperID;
invoice.InvoiceStatusID = (int)Enums.InvoiceStatus.Open;
if (invoice.InvoiceID == 0)
{//inserting new invoice.
DataContext.InvoiceData.Insert(invoice);
}
else
{//attaching existing invoice.
DataContext.InvoiceData.Attach(invoice);
}
AddHours(invoice);
//save changes.
DataContext.SaveChanges();
//redirect to invoice list.
return RedirectToAction("Index");
}
private void AddHours(Invoice invoice)
{
//get existing logs.
IQueryable<InvoiceLog> existingLogs = null;
if(invoice.InvoiceID > 0)
{
existingLogs = DataContext.InvoiceData.GetLogs(invoice.InvoiceID);
}
//create new logs.
var numDays = invoice.EndDate.Subtract(invoice.StartDate).TotalDays;
for (int k = 0; k <= numDays; k++)
{
//check if log already exists.
var existingLog = existingLogs.ToList().FindIndex(l => l.LogDate == invoice.StartDate.AddDays(k));
if (existingLog == -1)
{
//add new log.
var log = new InvoiceLog();
log.CreateDate = DateTime.Now;
log.UpdateDate = DateTime.Now;
log.Hours = 0;
log.InvoiceID = invoice.InvoiceID;
log.LogDate = invoice.StartDate.AddDays(k);
invoice.InvoiceLogs.Add(log);
}
}
}
Thanks,
Justin
You can try to add InvoiceLogs items before attaching the invoice:
else
{//attaching existing invoice.
AddHours(invoice);
DataContext.InvoiceData.Attach(invoice);
}
Related
I am trying to update the "Modified By" field based on a text field called "Prepared By", which contains the name of a user. I've created a pre-operation plug-in to do this and believe I am close to done. However, the "Modified By" field is still not successfully getting updated. I am relatively new to coding and CRM, and could use some help modifying the code and figuring out how I can get this to work.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Linq;
namespace TimClassLibrary1.Plugins
{
public class CreateUpdateContact : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = factory.CreateOrganizationService(context.UserId);
tracingService.Trace("Start plugin");
tracingService.Trace("Validate Target");
if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))
return;
tracingService.Trace("Retrieve Target");
var target = (Entity)context.InputParameters["Target"];
String message = context.MessageName.ToLower();
SetCreatedByAndModifiedBy(tracingService, service, target, message);
}
private void SetCreatedByAndModifiedBy(ITracingService tracingService, IOrganizationService service, Entity target, string message)
{
tracingService.Trace("Start SetPriceList");
tracingService.Trace("Validate Message is Create or Update");
if (!message.Equals("create", StringComparison.OrdinalIgnoreCase) && !message.Equals("update", StringComparison.OrdinalIgnoreCase))
return;
tracingService.Trace("Retrieve Attributes");
var createdByReference = target.GetAttributeValue<EntityReference>("new_createdby");
var modifiedByReference = target.GetAttributeValue<EntityReference>("new_modifiedby");
tracingService.Trace("Retrieve And Set User for Created By");
RetrieveAndSetUser(tracingService, service, target, createdByReference, "createdby");
tracingService.Trace("Retrieve And Set User for Modified By");
RetrieveAndSetUser(tracingService, service, target, modifiedByReference, "modifiedby");
}
private void RetrieveAndSetUser(ITracingService tracingService, IOrganizationService service, Entity target, EntityReference reference, string targetAttribute)
{
tracingService.Trace("Validating Reference");
if (reference == null)
return;
tracingService.Trace("Retrieving and Validating User");
var user = RetrieveUserByName(service, reference.Name, new ColumnSet(false));
if (user == null)
return;
tracingService.Trace("Setting Target Attribute");
target[targetAttribute] = user.ToEntityReference();
}
private Entity RetrieveUserByName(IOrganizationService service, string name, ColumnSet columns)
{
var query = new QueryExpression
{
EntityName = "systemuser",
ColumnSet = columns,
Criteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "fullname",
Operator = ConditionOperator.Equal,
Values = { name }
}
}
}
};
var retrieveResponse = service.RetrieveMultiple(query);
if (retrieveResponse.Entities.Count == 1)
{
return retrieveResponse.Entities.FirstOrDefault();
}
else
{
return null;
}
}
}
}
If you do get use from method Retreiveusernyname then you have to use below code
target[“modifiedby”] = new EntityRefrence(user.logicalname,user.id);
I don't see anything obviously wrong with your update, however you are taking a complicated and unnecessary step with your RetrieveUserByName() method. You already have EntityReference objects from your new_createdby and new_modifiedby fields, you can simply assign those to the target:
if (message.Equals("create", StringComparison.OrdinalIgnoreCase))
{
target["createdby"] = target["new_createdby];
}
else if (message.Equals("update", StringComparison.OrdinalIgnoreCase))
{
target["modifiedby"] = target["new_modifiedby];
}
If new_createdby and new_modifiedby are not entity references, then that would explain why your existing code does not work, if they are, then use my approach.
while inserting records in a loop
The property "id" is part of the object's key information and cannot be modified
secProductionRepository.Add(tblSecProduction);
this.SaveChanges();
CODE
Controller : This is the Controller code from where i am calling method of Repository . Adding data into repository and calling function to insert. i think i have to initialize it every time with new keyword. But where should i do that.
SingleMaster objGetXMLData = _iSingleService.GetXMLData();
if (objGetXMLData._tblSecDoorXMLData != null)
{
for (int totalCount = 0; totalCount < objGetXMLData._tblSecDoorXMLData.Count; totalCount++)
{
_tblSecDoorsProduction.txtTongue = singleDoorModel.txtTongue;
_tblSecDoorsProduction.numFibMesh = Convert.ToInt32(singleDoorModel.chkBoxFibreMesh);
_tblSecDoorsProduction.dteDesDate = DateTime.Now;
_iSingleDoorService.UpdatetblSecDoorsProduction(_tblSecDoorsProduction, "Insert");
}
}
Repository : Here i am inserting new row into the table
public void UpdatetblSecDoorsProduction(tblSecDoorsProduction tblSecDoorsProduction, string Message)
{
var secDoorsProductionRepository = Nuow.Repository<tblSecDoorsProduction>();
tblSecDoorsProduction alreadyAttached = null;
if (Message == "Insert")
{
secDoorsProductionRepository.Add(tblSecDoorsProduction);
Nuow.SaveChanges();
}
}
Create new object each time in the loop. Updated code here:
for (int totalCount = 0; totalCount < objGetXMLData._tblSecDoorXMLData.Count; totalCount++)
{
tblSecDoorsProduction _tblSecDoorsProduction = new tblSecDoorsProduction();
_tblSecDoorsProduction.txtTongue = singleDoorModel.txtTongue;
_tblSecDoorsProduction.numFibMesh = Convert.ToInt32(singleDoorModel.chkBoxFibreMesh);
_tblSecDoorsProduction.dteDesDate = DateTime.Now;
_iSingleDoorService.UpdatetblSecDoorsProduction(_tblSecDoorsProduction, "Insert");
}
I have migrated my project from EF4 to EF6. I am facing trouble while updating the records. Below is my code
public int SaveApplicantData(Applicant objApplicant, bool isEdit)
{
DBEntities context = new DBEntities();
int applicantId = 0;
try
{
if (objApplicant.Id > 0)
{
var applicant = context.Applicants.Where(a => ((a.Id != objApplicant.Id) && (a.SSN == objApplicant.SSN))).FirstOrDefault();
if (applicant != null)
{
return -1;
}
else
{
applicant = context.Applicants.Find(objApplicant.Id);
}
if (applicant != null)
{
applicant.FirstName = "TEST NAME";
context.SaveChanges();
}
}
return objApplicant.Id;
}
}
I have also tried context.Entry(applicant).CurrentValues.SetValues(objApplicant) but does not work for me. Spending lot of hours in finding solution. Please assists with appropriate solution.
UPDATE
Also I tried
context.Applicants.Attach(objApplicant);
context.Entry(objApplicant).State = EntityState.Modified;
context.SaveChanges();
but no success!
Try the below code instead of adding in context.Applicants.Add(objApplicant);
context.Entry(objApplicant).State = EntityState.Modified;
context.SaveChanges();
Update: Try this code block.
if (applicant != null)
{
applicant.FirstName = "TEST NAME";
context.Entry(applicant).State = EntityState.Modified;
context.SaveChanges();
}
I tried to find an answer through the related questions I got but I didn't see the same situation I have now. I a beginner with this framework.
The thing is that in the DB the TopicFeedbackType is always referred but TopicNavigatedUrl and Product are always inserted event if I attach the entity. What am I doing wrong? I attach the found entity to the correct entity set name. The entity gets attached. I noticed that before the p_TopicQuickFb have one of its property updated by an attached entity, its EntityKey is null, but when the currentNavUrl(for example) is set, the EntityKey of p_TopicQuickFb is not null anymore. Its value is "EntitySet=TopicQuickFeedbacks" but there is no id.
I am really lost in there.
public void AddTopicQuickFeedback(TopicQuickFeedback p_TopicQuickFb, string p_SessionID)
{
TopicFeedbackType currentType = this.GetTopicFeedbackType(p_TopicQuickFb.TopicFeedbackType.FeedbackType);
bool currentTypeAttached = false;
TopicNavigatedUrl currentNavUrl = this.GetTopicNavigatedUrl(p_TopicQuickFb.TopicNavigatedUrl.Url);
bool currentNavUrlAttached = false;
Product currentProduct = this.GetProduct(p_TopicQuickFb.Product.Name, p_TopicQuickFb.Product.MajorVersion, p_TopicQuickFb.Product.MinorVersion);
bool currentProductAttached = false;
using (COHFeedbackEntities context = GetObjectContext())
{
TopicFeedback tf = GetTopicFeedback(p_SessionID, context);
if (tf != null)
{
if (currentType != null)
{
p_TopicQuickFb.TopicFeedbackType = null;
context.AttachToOrGet<TopicFeedbackType>("TopicFeedbackTypes", ref currentType);
currentTypeAttached = true;
p_TopicQuickFb.TopicFeedbackType = currentType;
}
if (currentNavUrl != null)
{
p_TopicQuickFb.TopicNavigatedUrl = null;
context.AttachToOrGet<TopicNavigatedUrl>("TopicNavigatedUrls", ref currentNavUrl);
currentNavUrlAttached = true;
p_TopicQuickFb.TopicNavigatedUrl = currentNavUrl;
}
if (currentProduct != null)
{
p_TopicQuickFb.Product = null;
context.AttachToOrGet<Product>("Products", ref currentProduct);
currentProductAttached = true;
p_TopicQuickFb.Product = currentProduct;
}
tf.TopicQuickFeedbacks.Add(p_TopicQuickFb);
context.SaveChanges();
context.Detach(tf);
if (currentNavUrlAttached)
{
context.TopicNavigatedUrls.Detach(currentNavUrl);
}
if (currentProductAttached)
{
context.Products.Detach(currentProduct);
}
if (currentTypeAttached)
{
context.TopicFeedbackTypes.Detach(currentType);
}
}
}
}
I found the method in this post : Is is possible to check if an object is already attached to a data context in Entity Framework?
public static void AttachToOrGet<T>(this System.Data.Objects.ObjectContext context, string entitySetName, ref T entity)
where T : IEntityWithKey
{
System.Data.Objects.ObjectStateEntry entry;
// Track whether we need to perform an attach
bool attach = false;
if (
context.ObjectStateManager.TryGetObjectStateEntry
(
context.CreateEntityKey(entitySetName, entity),
out entry
)
)
{
// Re-attach if necessary
attach = entry.State == EntityState.Detached;
// Get the discovered entity to the ref
entity = (T)entry.Entity;
}
else
{
// Attach for the first time
attach = true;
}
if (attach)
{
context.AttachTo(entitySetName, entity);
}
}
Test method:
User user = new User(true, false, false);
string commentStr = "This is my comment";
Product product = new Product("ProductName", 7, 0);
TopicFeedbackComment commFeedback = new TopicFeedbackComment(commentStr, new TopicNavigatedUrl("http://testurl.com/test0"), product);
TopicFeedback feedback = new TopicFeedback(sessionID, user, FeedbackState.New);
provider.AddTopicFeedback(feedback);
TopicFeedback addedFeedback = provider.RetrieveTopicFeedback(sessionID);
provider.AddTopicFeedbackComment(commFeedback, sessionID);
Running this again and again do just INSERT to the
Can't post images so I can provide schema it if necessary.
My answer is in my last comment. I found it by myself.
If someone would like to comment why it's working this way it would be nice! :)
i have a mission to make a plug-in in crm 4 which should 1. put in the subject field of an email the name of the account and then 2. put the contact list of account to the cc field of the email.
the first thing i did and it work, but the second... not so much...
i have seen some samples but none of them was close to halp me...
i would like to have help and explain of how to find the list of the contact that belong to the account and then put the list in the cc field.
here is the begining...:
namespace mail
{
public class Class1 : IPlugin
{
public void Execute(IPluginExecutionContext context)
{
DynamicEntity entity = null;
if (context.InputParameters.Properties.Contains("Target") &&
context.InputParameters.Properties["Target"] is DynamicEntity)
{
entity = (DynamicEntity)context.InputParameters.Properties["Target"];
if (entity.Name != EntityName.account.ToString())
{
return;
}
}
else
{
return;
}
try
{
// updating the subject of the email
ICrmService service = context.CreateCrmService(true);
account accountRecord = (account)service.Retrieve("account", ((Key)entity.Properties["accountid"]).Value, new ColumnSet(new string[] { "name" }));
String str = String.Empty;
str = accountRecord.name.ToString();
DynamicEntity followup = new DynamicEntity();
followup.Name = EntityName.email.ToString();
followup.Properties = new PropertyCollection();
followup.Properties.Add(new StringProperty("subject", str));
//updating the CC of the email
TargetCreateDynamic targetCreate = new TargetCreateDynamic();
targetCreate.Entity = followup;
CreateRequest create = new CreateRequest();
create.Target = targetCreate;
CreateResponse created = (CreateResponse)service.Execute(create);
}
catch
{
throw new InvalidPluginExecutionException(
"An error occurred in the AccountUpdateHandler plug-in.");
}
}
}
}
You can try something like this,
List<BusinessEntity> contactList = GetNeededContacts(crmService, sourceEntity);
email targetEmail = GetTargetEmail(crmService, emailid);
foreach (DynamicEntity contact in contactList)
{
activityparty contActParty = new activityparty() { partyid = new Lookup("contact", contact.contactid.Value };
List<activityparty> tmpList = targetEmail.cc.ToList();
tmpList.Add(contActParty);
targetEmail.cc = tmpList.ToArray();
}
crmService.Update(targetEmail);
You only have to develop the function to get the contact, user or account references.