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)

Update Status code on child record C#

(0) ShareShare
ReportReport
Posted on by 245

Hello,

I have created a code that will go through all child records of a specific type and update their status based on a field on the parent record.

The problem I am facing is that I am getting an exception when running the last execute statement:

SetStateResponse stateSet = (SetStateResponse)service.Execute(state);

The code is doing everything correctly up this stage:

SetStateRequest state = new SetStateRequest();

state.State = new OptionSetValue(0);

state.Status = new OptionSetValue(1);

EntityReference caseReference = new EntityReference("mit_keyasset", act.Id);

state.EntityMoniker = caseReference;

SetStateResponse stateSet = (SetStateResponse)service.Execute(state);

The exception I am receiving seem to be as following FaultException<OrganizationServiceFault>

Hope that anyone can give me a hand on this.

 

 

*This post is locked for comments

I have the same question (0)
  • Mahadeo Matre Profile Picture
    17,021 on at

    Chek you have coorect loop and also check act.Id is has value before creating request.

  • Aileen Gusni Profile Picture
    44,524 on at

    BRackee,

    Make sure this line is correct:

    new EntityReference("mit_keyasset", act.Id);

    The entity name of the child you want to update plus the ID..

    Thanks.

  • Brackee Profile Picture
    245 on at

    Hello Aileen,

    Thanks for the quick answer, I have made sure to check that mit_keyasset is the correct name of the custom entity, I have also verified that act.Id is not null and always do give me a an id (example: e2bcebb4-eef7-e411-8108-c4346bac2ebc ).

    I have also tested if I can create a new task for each run.

    foreach (Entity act in ec.Entities)

                       {

                           Guid regardingobjectid = new Guid(context.PrimaryEntityId.ToString());

                           string regardingobjectidType = "opportunity";

                           var id = act.Id;

                           if (id != null)

                           {

                               Entity followup1 = new Entity("task");

                               followup1["subject"] = "Test task1";

                               followup1["description"] = id;

                               followup1["category"] = context.PrimaryEntityName;

                               service.Create(followup1);

                           }

    }

    but if I add  the following it fails:

    SetStateRequest state = new SetStateRequest();

                           state.State = new OptionSetValue(0);

                           state.Status = new OptionSetValue(1);

                           EntityReference caseReference = new EntityReference("mit_keyasset", id);

                           state.EntityMoniker = caseReference;

                           SetStateResponse stateSet = (SetStateResponse)service.Execute(state);

  • Verified answer
    Mahadeo Matre Profile Picture
    17,021 on at

    Check / post your exception details.. mainly check message, inner exception and trace.

    Also check you are setting correct State and Status code, when executing SetStateRequest

    Generally State =0 and Status code = 1 is for State = Active and Status = Active.

    but if added any custom status codes, then this value might change. You need to check you are using correct state and status code.

    Also check

    msdn.microsoft.com/.../microsoft.crm.sdk.messages.setstaterequest.aspx

  • Verified answer
    Brackee Profile Picture
    245 on at

    Hello Machadeo,

    I did some more investigation and I realized that I thought that the value the optionsets have can be much larger than what I realized, and I did not check within the system what the real values where.

    I realized the values where "913,200,003", this resulted in the following code that executed without any problems:

                           SetStateRequest state = new SetStateRequest();

                           int value = 913200003;

                           state.State = new OptionSetValue(0);

                           state.Status = new OptionSetValue(value);

                           state.EntityMoniker = new EntityReference("mit_keyasset", id);

                           service.Execute(state);

    Thanks for your everyones contribution.

  • Royal King Profile Picture
    27,686 on at

    I would suggest using the workflow from the codeplex that executes the workflow on child records. this way you can use built in workflow step "Change status" on the child records to change the status of the record.  I used this workflow in many scenario and it works without any issue.

    Check the workflow and how to use in below post

    crm2011distributewf.codeplex.com

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