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 :
Dynamics 365 general forum

The context is not currently tracking the entity

(0) ShareShare
ReportReport
Posted on by 145

Hi,

I'm using LINQ to get list of case records using the service context from the generated XRM , when i try to resolve the first case in the list it works fine, but when i try to resolve a different case in the same list it doesn't work , an exception occurs : "The context is not currently tracking the 'incident' entity" . What might be the problem ? it feels like this is  a non sense issue.

This works :

svcContext serviceContext = new svcContext(organizationService);
Incident preImageCaseEntity = context.PreEntityImages["PreBusinessEntity"].ToEntity<Incident>();
List<Incident> cases = GetChangeEmailCases(organizationService, preImageCaseEntity, serviceContext);
if (cases.Any())
{
tracingService.Trace(cases.Count.ToString());
Incident lastChangeEmailCase = cases.First();
serviceContext.UpdateObject(lastChangeEmailCase);
serviceContext.SaveChanges();

}

but this doesn't work:

svcContext serviceContext = new svcContext(organizationService);
Incident preImageCaseEntity = context.PreEntityImages["PreBusinessEntity"].ToEntity<Incident>();
List<Incident> cases = GetChangeEmailCases(organizationService, preImageCaseEntity, serviceContext);
if (cases.Any())
{
tracingService.Trace(cases.Count.ToString());
Incident lastChangeEmailCase = cases[1];
serviceContext.UpdateObject(lastChangeEmailCase);
serviceContext.SaveChanges();

}

I have the same question (0)
  • Khaled Rimawi Profile Picture
    145 on at

    Hi, i am trying to update list of case entities, but an exception occurs : "the context is not currently tracking 'incident' entity" . if i only update one entity it works fine , but when i update multiple entities the exception happens.

    //This Works fine
    svcContext serviceContext = new svcContext(organizationService);
    Incident preImageCaseEntity = context.PreEntityImages["PreBusinessEntity"].ToEntity<Incident>();
    List<Incident> cases = GetChangeEmailCases(organizationService, preImageCaseEntity, serviceContext);
    
    Incident case = cases[0];
    case.new_emailapprove = true;
    serviceContext.UpdateObject(case);
    serviceContext.SaveChanges();
    
    //--------------------------------
    //this is not working
    svcContext serviceContext = new svcContext(organizationService);
    Incident preImageCaseEntity = context.PreEntityImages["PreBusinessEntity"].ToEntity<Incident>();
    List<Incident> cases = GetChangeEmailCases(organizationService, preImageCaseEntity, serviceContext);
    
    Incident case = cases[0];
    case.new_emailapprove = true;
    serviceContext.UpdateObject(case);
    serviceContext.SaveChanges();
    Incident case = cases[1];
    case.new_emailapprove = true;
    serviceContext.UpdateObject(case);
    serviceContext.SaveChanges();


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

    Please, don't create duplicated threads.

  • Suggested answer
    Community Member Profile Picture
    on at

    Are you sure the following line actually returns an incident record? It could very well be a case of the list containing only one record. 

    Incident lastChangeEmailCase = cases[1];
    


  • Khaled Rimawi Profile Picture
    145 on at

    Hi Sara,

    Yes, i'm pretty sure.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Khaled,

    Remove the save changes statement before the second case and try again.

    ===========

    Incident case = cases[0];

    case.new_emailapprove = true;

    serviceContext.UpdateObject(case);

    serviceContext.SaveChanges(); // Remove this line and try again.

    Incident case = cases[1];

    case.new_emailapprove = true;

    serviceContext.UpdateObject(case);

    serviceContext.SaveChanges();

    ===========

    Hope this helps.

  • Suggested answer
    Community Member Profile Picture
    on at

    You need to make sure that you attach the incident record to the serviceContext.

    Code snippet as below. 

    if(!serviceContext.IsAttached(lastChangeEmailCase))
    {
       serviceContext.Attach(lastChangeEmailCase);
    serviceContext.UpdateObject(lastChangeEmailCase);
    serviceContext.SaveChanges(); }


    - Sara

  • Verified answer
    Community Member Profile Picture
    on at

    Hi Khaled, 

    A cleaner way would be to have a function to save the changes and call them. 

    public void SaveChanges(OrganizationServiceContext serviceContext, Incident incidentToUpdate)
    {
        if (incidentToUpdate.recordId != Guid.Empty)
        {
            if (!serviceContext.IsAttached(incidentToUpdate))
            {
                serviceContext.Attach(incidentToUpdate);
            }
    
            serviceContext.UpdateObject(incidentToUpdate);
            serviceContext.SaveChanges();
        }
    }
    


    Essentially, you are checking the serviceContext has the incident record or not...if not, attach it and then update the record; else just update the record. 

    - Sara

    Code typed here without compiler verification / testing. Please modify typos accordingly. 

  • Community Member Profile Picture
    on at

    Hi Sara,

    I am also facing similar kind of issue and trying to use the code that you have suggested but the serviceContext is not having the IsAttached() method available.

    Kindly help me with this

    Thanks,

    Kiran

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Dynamics 365 general

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans