Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

EntityState must be set to null, Created (for Create message) or Changed (for Update message)

Posted on by 80

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

  • Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: EntityState must be set to null, Created (for Create message) or Changed (for Update message)

    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;

  • Shubh_K Profile Picture
    Shubh_K 80 on at
    RE: EntityState must be set to null, Created (for Create message) or Changed (for Update message)

    Thanks for response Sreevalli,

    Tried this, as Kokulan already suggested in first comment. 

    Not working for me.

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: EntityState must be set to null, Created (for Create message) or Changed (for Update message)

    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;
    
                }
            }
  • Suggested answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: EntityState must be set to null, Created (for Create message) or Changed (for Update message)

    Remove childEntity.EntityState = EntityState.Changed; from your code.

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: EntityState must be set to null, Created (for Create message) or Changed (for Update message)

    Hi

    Please refer to this link and this should give you the options to help solve this issue

    community.dynamics.com/.../entitystate-must-be-set-to-null-created-for-create-message-or-changed-for-update-message

  • Shubh_K Profile Picture
    Shubh_K 80 on at
    RE: EntityState must be set to null, Created (for Create message) or Changed (for Update message)

    Hi Kokulan, I tried but still getting the same error, any other suggestion ?

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: EntityState must be set to null, Created (for Create message) or Changed (for Update message)

    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");

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans