web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

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

  • Shubh_K Profile Picture
    80 on at

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

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    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

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at

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

  • Suggested answer
    Sreevalli Profile Picture
    3,256 on at

    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;
    
                }
            }
  • Shubh_K Profile Picture
    80 on at

    Thanks for response Sreevalli,

    Tried this, as Kokulan already suggested in first comment. 

    Not working for me.

  • Sreevalli Profile Picture
    3,256 on at

    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;

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans