Asked by:
Simple LINQ query to update record doesn't work, but retrieves the record

Question
-
Hello all, I have a query in LINQ that should retrieve a reference number from one entity and populate it in the current record. The problem is, I can retrieve the record and set the entityReference, but the line: currentRecord.new_reference = impSet.new_referenceTest; does nothing even though the record field is populated. Anyone know what's up with this?
Here is the query:
var impSet = (from c in orgContext.testSet
where c.Reference == Ref
select c).FirstOrDefault();
if (impSet != null)
{
currentRecord.new_reference = impSet.new_referenceTest;currentRecord.new_ImportFile = impSet.ToEntityReference();
service.Update(currentRecord);
}Friday, November 14, 2014 1:33 PM
All replies
-
Hi,
You have to set the id of entity that you want to update
if (impSet != null) { currentRecord.Id = EntityId;
currentRecord.new_reference = impSet.new_referenceTest; currentRecord.new_ImportFile = impSet.ToEntityReference(); service.Update(currentRecord); }
If you find this post helpful then please Vote as Helpful and Mark As Answer. Thanks and Regards, Polat Aydın My blog
Friday, November 14, 2014 1:50 PM -
Hi Polat, that didn't work either, but I am not sure that is necessary either.
The point is impSet.new_referenceTest is returned as null, even though I know data is in there.
- Edited by Sebd.DD Friday, November 14, 2014 2:03 PM
Friday, November 14, 2014 1:59 PM -
Hi,
You are running this code where ? If you are in the currentRecord entity update message you dont need to set the ID.
else
You have to set the Id of your entity . It is MUST.
Here is an example how you can update the entity.
Entity update = new Entity();
update.LogicalName = entitylogicalname;
update.Id = EntityId;
Service.Update(Entity);
Also if you set to it null, then it will be null
If you find this post helpful then please Vote as Helpful and Mark As Answer. Thanks and Regards, Polat Aydın My blog
- Edited by Polat Aydın[MCP] Friday, November 14, 2014 2:39 PM
Friday, November 14, 2014 2:20 PM -
I am in the current record, and this is where it is not working. I cannot retrieve related fields. It always resulted in null.Friday, November 14, 2014 2:43 PM
-
If you are in the plugin of the current record ,
Pre -Update --> you dont need to update record just add the context
if( currentRecord.Attributes.Contains("new_ImportFile")) { currentRecord.Attributes.Add("new_ImportFile",impSet.ToEntityReference(); } else { currentRecord["new_ImportFile"] = impSet.ToEntityReference(); }
if you are in Post Message after these code blocks you have to update the entity.
Service.Update( currentRecord);
If you find this post helpful then please Vote as Helpful and Mark As Answer. Thanks and Regards, Polat Aydın My blog
- Proposed as answer by Polat Aydın[MCP] Sunday, February 8, 2015 3:29 PM
Friday, November 14, 2014 5:10 PM -
Can you expand on the question with more information, are any attributes returned for impSet?Friday, November 14, 2014 5:29 PM