Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Fetch xml to get child cases of parent case

Posted on by 475

Hi,

I want to get all the cases which has child cases and after getting cases and looping parent cases each need to check any one of case child case has origin ="mail"  then update parent case title other wise no update to parent case. Please help getting data in fetch xml or query expression in easy way and how to validate them in loop.

Thanks,

Krishna

*This post is locked for comments

  • Verified answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Fetch xml to get child cases of parent case

    Hi,

    In the above code, there is a condition if any of the child case contain origin=email it will then only update parent.

    I will advice you to build a console application and debug the code, It will give you more knowledge about the code.

    Best regards,

    Shahbaaz

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Fetch xml to get child cases of parent case

    Hi krishna,

    You can make a filter after retrieve child case only origin = mail using linq from child case entity collection using service context.

    Here is reference you may get help.

    [View:https://msdn.microsoft.com/en-us/library/gg509026.aspx]

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: Fetch xml to get child cases of parent case

    If any one of the child case is having orgin="Mail" then only we need to update parent case cant do this in linq to check whether childcases cases contains origin .once we got all child cases of parent .? Pls suggest

  • Verified answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Fetch xml to get child cases of parent case

    Hi,

    Here using below code you can get the Parent cases where created date in 1 year older, and using for loop for each parent case you can get the related child case's where origin is "email", and if it gets any record then it will update parent case topic,

    Note : i have not tested it, you can create a console application and try to debug it if you get any issue.

    ************************************************************************

    using (service = new OrganizationServiceProxy(new Uri(_organizationURI), null, _credential, null))

    {

    // get all the case where createon date is 1 year old

    QueryExpression getcases = new QueryExpression("incident");

    getcases.NoLock = true;

    getcases.ColumnSet = new ColumnSet("incidentid","origin", "title", "ownerid", "createdon", "createdby", "parentcaseid", "description");

    getcases.AddOrder("createdon", OrderType.Descending);

    FilterExpression Filter1 = new FilterExpression(LogicalOperator.And);

    Filter1.AddCondition("createdon", ConditionOperator.LessEqual, oneyearolddate);

    getcases.Criteria = Filter1;

    var retrievecases = service.RetrieveMultiple(getcases);

    foreach (var case in retrievecases.Entities)

    {

    var parentIncidentId = case.Id;

    string oneyearolddate = Convert.ToString(DateTime.AddYears(-1));

    QueryExpression queryDemande = new QueryExpression("incident");

    queryDemande.NoLock = true;

    queryDemande.ColumnSet = new ColumnSet("incidentid","origin", "title", "ownerid", "createdon", "createdby", "parentcaseid", "description");

    queryDemande.AddOrder("createdon", OrderType.Descending);

    FilterExpression Filter1 = new FilterExpression(LogicalOperator.And);

    Filter1.AddCondition("parentcaseid", ConditionOperator.Equal, parentIncidentId);

    Filter1.AddCondition("createdon", ConditionOperator.LessEqual, oneyearolddate);

    queryDemande.Criteria = Filter1;

    var linkDemandeAssocie = new LinkEntity("incident", "incident", "parentcaseid","incidentid", JoinOperator.LeftOuter);

    linkDemandeAssocie.EntityAlias = "DemandeFille";

    linkDemandeAssocie.Columns = new ColumnSet("incidentid","origin", "title", "ownerid", "createdon", "createdby", "parentcaseid", "description");

    queryDemande.LinkEntities[0].LinkCriteria.Filters.Add(

    new FilterExpression()

    {

    FilterOperator = LogicalOperator.And,

    Conditions =

    {

    new ConditionExpression("origin", ConditionOperator.Equal, "Email")

    }

    }

    );

    queryDemande.LinkEntities.Add(linkDemandeAssocie);

    var results = service.RetrieveMultiple(queryDemande);

    if(results.Entities.Count>1)

    if(((AliasedValue)record["DemandeFille.origin"]).Value.ToString()=="Email")

    {

    record.attribute["title"] = "Update parent case title";

    service.Update(record);

    }

    }

    }

    }

    ************************************************************************

    If you find it helpful, Please mark as verified.

    Best Regards,

    Shahbaaz

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Fetch xml to get child cases of parent case

    Hi Krishna,

    Here is the code you just need to replace field as per your requirement.

     //Select the field as per your requirement 
                    var fetchCase = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                                <entity name='incident'>
                                                <attribute name='incidentid'/>                                        
                                                <attribute name='statecode'/>
                                                <attribute name='statuscode'/>
                                                <filter type='and'>
                                                <condition attribute='ticketnumber' value='" + xxxxxxx + "' operator='eq'/>  </filter> </entity>  </fetch>");
    
    
                    EntityCollection resultCase = organizationService.RetrieveMultiple(fetchCase);
                    EntityCollection resultChildCase = null;
    
                    if (resultCase.Entities.Count > 0)
                    {
                        if (notifyCaseStatusRequest.TicketStatusValue > 0 && notifyCaseStatusRequest.TicketStatusValue < 7)
                        {
                            Entity firstCase = resultCase.Entities.FirstOrDefault();
                            CaseID = new Guid(firstCase.Attributes["incidentid"].ToString());
    
                            var entity = new Entity(IncidentSchema.CaseEntityLogicalName) { Id = CaseID };
    
    
                            //Select the field as per your requirement 
    
                            string ChildCase = @"<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>"
                                                                 + "<entity name='incident'>"
                                                                 + "<attribute name='email'/>"
                                                                 + "<attribute name='xxxxxxx'/>"
                                                                 + "<attribute name='xxxxxxx'/>"
                                                                 + "<attribute name='incidentid'/>"
                                                                 + "<attribute name='statecode'/>"
                                                                  + "<attribute name='statuscode'/>"
                                                                 + "<filter type='and'>"
                                                                 + "<condition attribute = 'parentcaseid' value = '{" + CaseID + "}' uitype = 'incident' operator= 'eq' />"
                                                                 + "</filter></entity></fetch>";
    
                            resultChildCase = new EntityCollection();
                            resultChildCase = organizationService.RetrieveMultiple(ChildCase);
                        }
    
    
                        if (resultChildCase.Entities.Count > 0)
                        {
                            //  var entity = new Entity(IncidentSchema.CaseEntityLogicalName) { Id = CaseID };
                            for (int k = 0; k < resultChildCase.Entities.Count; k++)
                            {
    
                                var Childentity = new Entity(IncidentSchema.CaseEntityLogicalName) { Id = new Guid(resultChildCase.Entities[k].Attributes["incidentid"].ToString()) };
                                //Retrieve  the field as per your requirement  and set                                        
                                Childentity["email"] = "goutamdas@mail.com";
                                organizationService.UpdateRecord(Childentity);
    
                            }
                        }
                    }

    Hope this helps.

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: Fetch xml to get child cases of parent case

    One Parent case can have multiple child cases then how to check if any one of child case is having case origin= mail ?

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: Fetch xml to get child cases of parent case

    Thanks Shahbaaz for response .There can be multiple child cases for parent case how we can on this ? one more condition i need check while getting parent cases is where createdon is 1 year old records. pls suggest.

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Fetch xml to get child cases of parent case

    Hi Krishna,

    Please check below code,

    ***********************************************************

    using (service = new OrganizationServiceProxy(new Uri(_organizationURI), null, _credential, null))

               {

                   var parentIncidentId = new Guid("CC43939E-FF6C-E711-8177-E0071B7FD0F1");

                   QueryExpression queryDemande = new QueryExpression("incident");

                   queryDemande.NoLock = true;

                   queryDemande.ColumnSet = new ColumnSet("incidentid","origin", "title", "ownerid", "createdon", "createdby", "parentcaseid", "description");

                   queryDemande.AddOrder("createdon", OrderType.Descending);

                   queryDemande.Criteria.AddCondition("parentcaseid", ConditionOperator.Equal, parentIncidentId);

                   var linkDemandeAssocie = new LinkEntity("incident", "incident", "parentcaseid","incidentid", JoinOperator.LeftOuter);

                   linkDemandeAssocie.EntityAlias = "DemandeFille";

                   linkDemandeAssocie.Columns = new ColumnSet("incidentid","origin", "title", "ownerid", "createdon", "createdby", "parentcaseid", "description");                

                   queryDemande.LinkEntities.Add(linkDemandeAssocie);

                   var results = service.RetrieveMultiple(queryDemande);

                   foreach (var record in results.Entities)

                   {

                       Console.WriteLine("###############################");

    if(((AliasedValue)record["DemandeFille.origin"]).Value.ToString()=="Email")

    {

    record.attribute["title"] = "Update parent case title";

    service.Update(record);

    }

                   }

               }

    ***********************************************************

    Refer : [View:https://community.dynamics.com/crm/f/117/t/246736:750:50]

    If you find it helpful,Please mark as Verified.

    Best Regards,

    Shahbaaz

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans