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 Opportunity Field based on condition

(0) ShareShare
ReportReport
Posted on by 365

This is my first time coding so bare with me.  Below is a part of the code that updates a field in the current Opportunity attached to an Account, I think this is where I need to make the changes to accomplish what I need.

Currently the field for all the opportunities attached to the account get updated but I need that only Opportunities with a status set to "In Progress" get updated.

I believe I need to add an if statement around "Service.Update(ChildToUpdate" but I cannot seem to find how to code: If Status == "In Progress" then do the update....

I would appreciate any feedback. Thank You.

List<Entity> children = (from child in serviceContext.CreateQuery(childEntity)

where ((((EntityReference)child[childParentField]) != null)

&& (((EntityReference)child[childParentField]).Id == triggerId))

select child).ToList();

foreach (Entity child in children)

{

Entity childToUpdate = service.Retrieve(childEntity, child.Id, new ColumnSet(new string[] { valueToUpdateChildDest }));

childToUpdate[valueToUpdateChildDest] = valueToUpdate;

service.Update(childToUpdate);

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Hasibur Rahman Profile Picture
    on at

    Hey GP,

    You can achieve this even without the code.

    I am guessing you are going to call this from a workflow, right? So,  you can Add step -> Check Condition

    4111.Capture2.JPG

    Then in the condition make status equals In Progress.

    4111.Capture2.JPG

    And then Inset Step to call your work flow activity

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I believe it is better to add that condition to select. So you can use something like following:

    List<Entity> children = (from child in serviceContext.CreateQuery(childEntity)
    where ((((EntityReference)child[childParentField]) != null)
    && (((EntityReference)child[childParentField]).Id == triggerId)
    && child.GetAttributeValue<OptionSet>("statuscode").Value == {int value of InProgress status})
    select child).ToList();


  • ChrisC Profile Picture
    630 on at

    I'm assuming you need to change a field on all opportunities for an account that are InProgress at the time of an event?

    In this case you have a 1:N relationship and cannot use a standard workflow to access the child records, but you can trigger one on the event and make a custom activity to iterate over the child records which is what I assume you are doing?

    As Andrii says, you can filter your child records with the InProgress condition, but in order to update each do you need first to retrieve and check the current value of the field before updating?

  • Pega Profile Picture
    365 on at

    Hi Hasibur,  yes this was my original plan but when i set up the condition the only related entity to opportunity is "Current Opportunity" and when i use it nothing gets updated.  So I'm not sure what "Current Opportunity" is even tough there is a subform oppoturnity in the account form.

  • Pega Profile Picture
    365 on at

    Hi Andrii,

    I'm trying to follow your recommendations.  I noticed that <OptionSet> is not available to me so I'm using <OptionSetValue> instead but i'm getting a bunch of errors.  Please take a look at my updated code.

     List<Entity> children = (from child in serviceContext.CreateQuery(childEntity)
                                                     where ((((EntityReference)child[childParentField]) != null)
                                                     && (((EntityReference)child[childParentField]).Id == triggerId)
                                                     && child.GetAttributeValue<OptionSetValue>("statuscode").Value == {int 1})
                                                     select child).ToList();


     

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Yeah, that's what I meant.

  • ChrisC Profile Picture
    630 on at

    Hi GP, in order for anyone to give further help I think you need to be specific about where your code is residing. If it's in a custom workflow activity I presume you are attaching a debugger to the sandbox worker thread and single stepping through the execution? The thread will timeout in 2 minutes so you do not have for ever but time enough...

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