Comparing date field with current date and updating - Not able to update field through plugin
-
Wednesday, June 20, 2012 4:24 PM
Trying to compare an existing date from an entity with current date. If entity field (testfield) of entity (testentity) date is equal to OR after current date, then add 1 year to the date in the field.
Issue - For some reason, its reading all the dates and comparing as well but not updating it in the field. I have used pre-operation (upon a fellow coder's suggesstion) step on the entity.
Any help would be deeply appreciated. Thanks!
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target entity from the input parmameters. Entity entity = (Entity)context.InputParameters["Target"]; // Verify that the target entity represents an account. // If not, this plug-in was not registered correctly. if (entity.LogicalName != "new_testentity") return; try { var k = entity["new_testfield"]; DateTime m = Convert.ToDateTime(k); DateTime d = DateTime.Now; int result = DateTime.Compare(m, d); var newvalue = DateTime.UtcNow.AddYears(1); // compare the dates if(entity.Contains("new_testfield")) { if (result <= 0) { entity["new_testfield"] = newvalue; } else return; } }
All Replies
-
Wednesday, June 20, 2012 7:33 PM
Test to update new_testfield by giving any date and check its updating or not.
If yes then there is problem with if condition(if(result <=0))
-
Thursday, June 21, 2012 4:32 PMI used a string value to populate a generic date. Tried it both on Pre and Post operation and it still won't work.
-
Friday, June 22, 2012 2:01 AM
Is this a Pre-Update plugin? If so, and the field in question was not updated as part of the original Update operation, it will not exist in the Entity's attributes collection. Try the following instead:
if (result <= 0) { if (entity.Contains("new_testfield")) { entity["new_testfield"] = newvalue; } else { entity.Attributes.Add("new_testfield", newvalue); } }
--pogo (pat) @ pogo69.wordpress.com
- Proposed As Answer by H.Desai Friday, June 22, 2012 5:23 AM