Getting Following exception while trying to create a clone.
Edit EntityState must be set to null, Created (for Create message) or Changed (for Update message)
Following is the code snippet, please suggest I am missing here ?
public static Guid getParentSurveyClone(IOrganizationService service, acn_myEntity parentEntity) { try { //The bool parameter passed to Clone method is set to true by default. var childEntity = parentEntity.Clone(true); childEntity.SetAttributeValue("name", "Child-" + parentEntity.name + "-" + DateTime.Now.ToString()); //Remove all the attributes of type primaryid as all the cloned records will have their own primaryid childEntity.Attributes.Remove(childEntity.LogicalName + "id"); //create the cloned record childEntity.EntityState = EntityState.Changed; var childSurveyId = service.Create(childEntity); } catch (SaveChangesException ex) { throw ex; } }
*This post is locked for comments
Hi Shubh,
Yeah, I saw it later there are loading issues with community pages :D
Then as a tip, after removing try adding statecode and statuscode with desired optionsvalues
childEntity.statuscode = 1;
childEntity.statecode = 0;
Thanks for response Sreevalli,
Tried this, as Kokulan already suggested in first comment.
Not working for me.
Hi Shubh,
Try this,
public static Guid getParentSurveyClone(IOrganizationService service, acn_myEntity parentEntity) { try { //The bool parameter passed to Clone method is set to true by default. var childEntity = parentEntity.Clone(true); childEntity.SetAttributeValue("name", "Child-" + parentEntity.name + "-" + DateTime.Now.ToString()); //Remove all the attributes of type primaryid as all the cloned records will have their own primaryid childEntity.Attributes.Remove(childEntity.LogicalName + "id"); childEntity.Attributes.Remove("statecode"); childEntity.Attributes.Remove("statuscode"); //create the cloned record var childSurveyId = service.Create(childEntity); } catch (SaveChangesException ex) { throw ex; } }
Remove childEntity.EntityState = EntityState.Changed; from your code.
Hi
Please refer to this link and this should give you the options to help solve this issue
Hi Kokulan, I tried but still getting the same error, any other suggestion ?
Hi
When you are creating, you do not need to do this unless you are using DbContex
childEntity.EntityState = EntityState.Changed; - this can be removed
And in your remove attribute code it cannot have Status and Status reasons that are read only or inactive ones
Try removing status and status reason from the attributes to see if it still fails
childEntity.Attributes.Remove("statuscode");
childEntity.Attributes.Remove("statecode");
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156