Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Service Activity 'There should be only one owner party for an activity' after upgrade from 2015 to 2016 (on premise)

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Last week we upgraded from CRM 2015 to CRM 2016 and also Installed SP1.


Since then on some random Service Activities we get a business process error when trying to delete or reassigning them.  In the Error log it shows some generic info and mentions a plugin we use.  Here is the error I get when trying to delete a Service Activity from an affected Case.

chrome_5F00_2017_2D00_04_2D00_11_5F00_17_2D00_19_2D00_05.png

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.Web.HttpUnhandledException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #A03B2C8FDetail: 
<OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
  <ErrorCode>-2147220970</ErrorCode>
  <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
  <Message>System.Web.HttpUnhandledException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #A03B2C8F</Message>
  <Timestamp>2017-04-11T21:20:45.0755066Z</Timestamp>
  <InnerFault>
    <ErrorCode>-2147220891</ErrorCode>
    <ErrorDetails xmlns:d3p1="schemas.datacontract.org/.../System.Collections.Generic" />
    <Message>An error occured while processing this request.</Message>
    <Timestamp>2017-04-11T21:20:45.0755066Z</Timestamp>
    <InnerFault i:nil="true" />
    <TraceText>

[MSCRM_Hours_Account_Plugin: MSCRM_Hours_Account_Plugin.Plugin]
[78b3e2af-e559-e311-80bd-001e6764dea8: MSCRM_Hours_Account_Plugin.Plugin: Delete of serviceappointment]


</TraceText>
  </InnerFault>
  <TraceText i:nil="true" />
</OrganizationServiceFault>

However in the Event Viewer I can see the "There should be only one owner party for an activity" error below.

The Web Service plug-in failed in OrganizationId: e66d88ca-93c6-44fc-8f1a-00c9595a3afc; SdkMessageProcessingStepId: 78b3e2af-e559-e311-80bd-001e6764dea8; EntityName: serviceappointment; Stage: 20; MessageName: Delete; AssemblyName: MSCRM_Hours_Account_Plugin.Plugin, MSCRM_Hours_Account_Plugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=797d3454956c826c; ClassName: MSCRM_Hours_Account_Plugin.Plugin; Exception: Unhandled Exception: Microsoft.Xrm.Sdk.SaveChangesException: An error occured while processing this request.
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChanges(SaveChangesOptions options)
   at MSCRM_Hours_Account_Plugin.Plugin.Execute(IServiceProvider serviceProvider)
   at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context)
   at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)
Inner Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: There should be only one owner party for an activityDetail: 
<OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
  <ErrorCode>-2147220989</ErrorCode>
  <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
  <Message>There should be only one owner party for an activity</Message>
  <Timestamp>2017-04-11T17:28:31.2752068Z</Timestamp>
  <InnerFault>
    <ErrorCode>-2147220989</ErrorCode>
    <ErrorDetails xmlns:d3p1="schemas.datacontract.org/.../System.Collections.Generic" />
    <Message>There should be only one owner party for an activity</Message>
    <Timestamp>2017-04-11T17:28:31.2752068Z</Timestamp>
    <InnerFault>
      <ErrorCode>-2147220989</ErrorCode>
      <ErrorDetails xmlns:d4p1="schemas.datacontract.org/.../System.Collections.Generic" />
      <Message>There should be only one owner party for an activity</Message>
      <Timestamp>2017-04-11T17:28:31.2752068Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText i:nil="true" />
    </InnerFault>
    <TraceText i:nil="true" />
  </InnerFault>
  <TraceText i:nil="true" />
</OrganizationServiceFault>
   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, ExecutionContext executionContext)
   at Microsoft.Crm.Extensibility.InprocessServiceProxy.ExecuteCore(OrganizationRequest request)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChange(OrganizationRequest request, IList`1 results)

This seems to happen to any Service Activity assigned to specific cases.  It doesn't happen with every case, very few in fact, but once it happens with a case any activity created will get the error if you attempt to delete it or assign it.

In trying to resolve this problem I found on Google that this can be related to activities having no owner, or having multiple owners.  I tried to the 2 SQL scripts here to see if I could identify those records, but both queries return 0 results.

I don't know if this is related to our plugin, even though the business process error does mention it.  It doesn't happen with the majority of activities or cases in our system and we go through a few hundred new ones per day.  And this all worked perfectly before the update from 2016 to 2015.  We didn't touch the plugins before or after the change.


Any ideas what else we could try?  I can give more info if necessary.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Service Activity 'There should be only one owner party for an activity' after upgrade from 2015 to 2016 (on premise)

    Alex Shega's answer fixed it for me.  Updating my plugins to avoid the use of SaveChanges seems to have resolved all business process errors.

    Thanks for all your help troubleshooting everyone!

  • ram r Profile Picture
    ram r on at
    RE: Service Activity 'There should be only one owner party for an activity' after upgrade from 2015 to 2016 (on premise)

    Nice find!

  • ashlega Profile Picture
    ashlega 34,477 on at
    RE: Service Activity 'There should be only one owner party for an activity' after upgrade from 2015 to 2016 (on premise)

    It's, actually, easy to reproduce with an on-delete workflow that re-assigns SA case to another user:

    saerror.PNG

    You need to fix that SaveChanges part of the plugin..

  • ram r Profile Picture
    ram r on at
    RE: Service Activity 'There should be only one owner party for an activity' after upgrade from 2015 to 2016 (on premise)

    Update the org name and guid of the service activity in the below odata url and run it in chrome. If it returns more than one record for the activity( which has error) then its a data issue. Let us know on the results.

    https://yourorg.crm.dynamics.com/XRMServices/2011/OrganizationData.svc/ActivityPartySet?$select=ActivityPartyId,ParticipationTypeMask,PartyId,&$filter=ParticipationTypeMask/Value eq 9 and ActivityId/Id eq (Guid'748fea2e-c39c-4d74-abce-9f12f05c5e97')

  • ashlega Profile Picture
    ashlega 34,477 on at
    RE: Service Activity 'There should be only one owner party for an activity' after upgrade from 2015 to 2016 (on premise)

    Just updated that post slightly.. Anyway, good luck

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Service Activity 'There should be only one owner party for an activity' after upgrade from 2015 to 2016 (on premise)

    Thanks, Ill have to give that a try next week.  Will report back!

  • Verified answer
    ashlega Profile Picture
    ashlega 34,477 on at
    RE: Service Activity 'There should be only one owner party for an activity' after upgrade from 2015 to 2016 (on premise)

    Might not answer your question yet, but might get you closer..

    - The error message is mentioning SaveChanges

    - In the "delete" portion of the plugin there are two calls to the SaveChanges

    The problem with those calls (I think) is that they are actually updating ALL the attributes on the account/case (even if, for the most part, they are just re-setting those attributes)

    Where I'm going is: those calls are also updating the "owner" field (again, just resetting it)

    According to Microsoft, such updates are now equivalent to AssignRequests. That could be the difference between your older CRM and current CRM

    Why it might matter? If an account/case are "re-assigned", CRM might try to propagate those changes to the service activity being deleted through the relationship. Not sure why it would fail.. but, maybe, it only fails where those activities are currently assigned to other owners?

    What I would try is the following update syntax instead:

    new_hours_account updatedAccount = new new_hours_account();

    updatedAccount.Id = curHoursAccount.Id;

    updatedAccount.new_Current_Balance = curBalance;

    service.Update(updatedAccount);

    That would be instead of

    xrm.UpdateObject(curHoursAccount);

    xrm.SaveChanges();

    Same thing with the incident

    (And you can optimize it more after that, but that's just a quick "test")

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Service Activity 'There should be only one owner party for an activity' after upgrade from 2015 to 2016 (on premise)

    It seems to be on an account by account basis.  So any account that is affected has every service activity and case also affected.

    New accounts are not picking up this issue as far as I know, but we don't necessarily have a lot of new accounts daily so it could happen still.

    Given that whats the best way to find the partylist?  Can accounts or other entities have more than 1 party?  I know activities have an activitypartybase table, but other entities don't seem to.

  • ram r Profile Picture
    ram r on at
    RE: Service Activity 'There should be only one owner party for an activity' after upgrade from 2015 to 2016 (on premise)

    For a record that errors out, can you retrieve the partylist associated to it and analyze the data. Also hope this issue occurs with the existing records and not with the new one's!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Service Activity 'There should be only one owner party for an activity' after upgrade from 2015 to 2016 (on premise)

    I updated the plugin to the latest SDK and replaced all the references, generated a new XRM.cs file, and still no luck.

    Here is the plugin source if anyone has any ideas.  The filen below is Plugin.cs, the only other file in the plugin is the XRM.cs.  Let me know if you have any ideas.  Otherwise I plan to make a MS support call after Easter to see if we can narrow down if the issue is data related or plugin related.

    namespace COMPANY_Hours_Account_Plugin
    {
        using System;
        using System.Collections.ObjectModel;
        using System.Globalization;
        using System.Linq;
        using System.ServiceModel;
        using Microsoft.Xrm.Sdk;
        using Xrm;
        using Microsoft.Crm.Sdk.Messages;
        using Microsoft.Xrm.Sdk.Query;
    
        /// <summary>
        /// Base class for all Plugins.
        /// </summary>    
        public class Plugin : IPlugin
        {
           
            public void Execute(IServiceProvider serviceProvider)
            {
                //THIS PLUGIN UPDATES THE BALANCE OF THE HOURS ACCOUNT AND THE RELATED CASE WHEN A SERVICE ACTIVITY IS MODIFIED OR DELETED
                if (serviceProvider == null)
                {
                    throw new ArgumentNullException("serviceProvider");
                }
    
                // Construct the Local plug-in context.
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                Entity entity;
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity && ((Entity)context.InputParameters["Target"]).LogicalName == "serviceappointment")
                {
                    try
                    {
                       entity = (Entity)context.InputParameters["Target"];
                       IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                        using (var xrm = new XrmServiceContext(service))
                        {
                            ServiceAppointment curService = xrm.ServiceAppointmentSet.Where(a => a.Id == entity.Id).FirstOrDefault();
                            if (curService!=null && curService.new_HoursAccountId != null)
                            {
                                decimal curBalance = 0M;
                                Guid hoursAccountId = curService.new_HoursAccountId.Id;
                                new_hours_account curHoursAccount = xrm.new_hours_accountSet.Where(a => a.Id == hoursAccountId && (int)a.statecode == 0).FirstOrDefault();
                                if (curHoursAccount != null)
                                {
                                    new_hours_account_statement lastStatement = (new_hours_account_statement)xrm.new_hours_account_statementSet.Where(a => a.new_HoursAccountId.Id == hoursAccountId && a.statecode.Value == 0).OrderByDescending(b => b.new_DateTo).FirstOrDefault();
                                    if (lastStatement != null)
                                        curBalance = lastStatement.new_EndingHoursBalance != null ? (decimal)lastStatement.new_EndingHoursBalance : 0M;
                                    var serviceAppointments = xrm.ServiceAppointmentSet.Where(a => a.new_HoursAccountId.Id == hoursAccountId && a.new_new_hours_account_statement_serviceappointment_HoursAccountStatement == null && (int)a.StateCode.Value != 2);
                                    foreach (ServiceAppointment aService in serviceAppointments)
                                        curBalance -= aService.new_BilledDuration != null ? (decimal)aService.new_BilledDuration : 0M;
                                    curHoursAccount.new_Current_Balance = curBalance;
                                    xrm.UpdateObject(curHoursAccount);
                                    xrm.SaveChanges();
                                }
                            }
    
                            //UPDATE THE TOTAL NUMBER OF MINUTES FOR THE CASE
                            int curActualDuration = 0;
                            decimal curCaseBilled = 0M;
                            decimal curCaseCOMPANY = 0M;
                            if (curService != null &&  curService.RegardingObjectId != null && curService.RegardingObjectId.LogicalName == "incident")
                            {
                                Incident curCase = xrm.IncidentSet.Where(a => a.Id == curService.RegardingObjectId.Id && (int)a.StateCode != 2).FirstOrDefault();
                                if (curCase != null)
                                {
                                    int curCaseState = (int)curCase.StateCode.Value;
                                    int curCaseStatus=curCase.StatusCode.Value;
                                    IncidentResolution aResolution=null;
                                    bool bNeedToReopen = false;
                                    if (curCaseState ==1)
                                    {
                                        //the case is canceled or resolved and need to be reactivated before updating it
                                        OpenIncident(curCase, service);
                                        Guid aCaseId = curCase.Id;
                                        if(curCase.Incident_IncidentResolutions!=null)
                                            aResolution = curCase.Incident_IncidentResolutions.LastOrDefault();
                                        xrm.Detach(curCase);
                                        curCase = xrm.IncidentSet.Where(a => a.Id == curService.RegardingObjectId.Id && (int)a.StateCode != 2).FirstOrDefault();
                                        //curCase = service.Retrieve(Incident.EntityLogicalName, aCaseId, new ColumnSet(allColumns: true)).ToEntity<Incident>();
                                        bNeedToReopen = true;
                                    }
                                    if (curCase != null)
                                    {
                                        // get the services associated with the case
                                        var serviceAppointments2 = xrm.ServiceAppointmentSet.Where(a => a.RegardingObjectId != null && a.RegardingObjectId.Id == curCase.Id && (int)a.StateCode != 2);
                                        foreach (ServiceAppointment aService in serviceAppointments2)
                                        {
                                            curActualDuration += aService.ActualDurationMinutes != null ? (int)aService.ActualDurationMinutes : 0;
                                            curCaseBilled += aService.new_BilledDuration != null ? (decimal)aService.new_BilledDuration : 0M;
                                            curCaseCOMPANY += aService.new_COMPANYTime != null ? (decimal)aService.new_COMPANYTime : 0M;
                                        }
                                        curCase.ActualServiceUnits = curActualDuration;
                                        curCase.new_BilledTime = curCaseBilled;
                                        curCase.new_COMPANYTime = curCaseCOMPANY;
                                        xrm.UpdateObject(curCase);
                                        xrm.SaveChanges();
                                        if (bNeedToReopen)
                                        {
                                            CloseIncident(curCase, service, aResolution, curCaseStatus);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (FaultException<OrganizationServiceFault> ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred updating Hours Account Balance - ", ex.InnerException);
                    }
                }
                else if(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
                {
                    //THIS SECTION RUN WHEN THE SERVICE ACTIVITY IS BEING DELETED
                    try
                    {
                        var entityref = (EntityReference)context.InputParameters["Target"];
                        if (entityref != null && entityref.LogicalName!=null && entityref.LogicalName.ToLower() != "serviceappointment")
                            return;
                        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
    
                        using (var xrm = new XrmServiceContext(service))
                        {
    
                            ServiceAppointment curService = xrm.ServiceAppointmentSet.Where(a => a.Id == entityref.Id).FirstOrDefault();
                            //If service is posted to a statement abort the delete operation
                            if (curService != null)
                            {
                                if (curService.new_HoursAccountStatementId != null)
                                {
                                    throw new Exception("This service was posted and cannot be deleted");
                                }
    
                                //if the case is resolved, the service cannot be deleted
                                if (curService.RegardingObjectId != null && curService.RegardingObjectId.LogicalName == "incident")
                                {
                                    Incident curCase = xrm.IncidentSet.Where(a => a.Id == curService.RegardingObjectId.Id && (int)a.StateCode != 2).FirstOrDefault();
                                    if (curCase != null)
                                    {
                                        int curCaseState = (int)curCase.StateCode.Value;
                                        if (curCaseState == 1)
                                        {
                                            throw new Exception("This service cannot be deleted because it has a refrence to a resolved case. The case needs to be reactivated first.");
                                        }
                                        else
                                        {
                                            int curActualDuration = 0;
                                            decimal curCaseBilled = 0M;
                                            decimal curCaseCOMPANY = 0M;
                                            var serviceAppointments = xrm.ServiceAppointmentSet.Where(a => a.Id != curService.Id && a.RegardingObjectId != null && a.RegardingObjectId.Id == curCase.Id && (int)a.StateCode != 2);
                                            foreach (ServiceAppointment aService in serviceAppointments)
                                            {
                                                curActualDuration += aService.ActualDurationMinutes != null ? (int)aService.ActualDurationMinutes : 0;
                                                curCaseBilled += aService.new_BilledDuration != null ? (decimal)aService.new_BilledDuration : 0M;
                                                curCaseCOMPANY += aService.new_COMPANYTime != null ? (decimal)aService.new_COMPANYTime : 0M;
                                            }
                                            curCase.ActualServiceUnits = curActualDuration;
                                            curCase.new_BilledTime = curCaseBilled;
                                            curCase.new_COMPANYTime = curCaseCOMPANY;
                                            xrm.UpdateObject(curCase);
                                            xrm.SaveChanges();
                                        }
                                    }
                                }
                                decimal curBalance = 0M;
                                if (curService.new_HoursAccountId != null)
                                {
                                    Guid hoursAccountId = curService.new_HoursAccountId.Id;
                                    new_hours_account curHoursAccount = xrm.new_hours_accountSet.Where(a => a.Id == hoursAccountId && (int)a.statecode == 0).FirstOrDefault();
                                    if (curHoursAccount != null)
                                    {
                                        new_hours_account_statement lastStatement = (new_hours_account_statement)xrm.new_hours_account_statementSet.Where(a => a.new_HoursAccountId.Id == hoursAccountId && a.statecode.Value == 0).OrderByDescending(b => b.new_DateTo).FirstOrDefault();
                                        if (lastStatement != null)
                                            curBalance = lastStatement.new_EndingHoursBalance != null ? (decimal)lastStatement.new_EndingHoursBalance : 0M;
                                        var serviceAppointments = xrm.ServiceAppointmentSet.Where(a => a.Id != curService.Id && a.new_HoursAccountId.Id == hoursAccountId && a.new_new_hours_account_statement_serviceappointment_HoursAccountStatement == null && (int)a.StateCode != 2);
                                        foreach (ServiceAppointment aService in serviceAppointments)
                                            curBalance -= aService.new_BilledDuration != null ? (decimal)aService.new_BilledDuration : 0M;
                                        curHoursAccount.new_Current_Balance = curBalance;
                                        xrm.UpdateObject(curHoursAccount);
                                        xrm.SaveChanges();
                                    }
                                }
                            }
                        }
                    }
                    catch (FaultException<OrganizationServiceFault> ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred: - ", ex);
                    }
                }
            }
    
            public void OpenIncident(Incident entity, IOrganizationService aService)
            {
                SetStateRequest setStateReq = new SetStateRequest();
                //Pass GUID of the record to be activated or Deactivated
    
                setStateReq.EntityMoniker = new EntityReference(entity.LogicalName, entity.Id);
                setStateReq.State = new OptionSetValue(0);
                setStateReq.Status = new OptionSetValue(1);
                SetStateResponse setStateResponse = (SetStateResponse)aService.Execute(setStateReq);
            }
    
            public void CloseIncident(Incident entity, IOrganizationService aService,IncidentResolution aResolution, int aNewStatus)
            {
                CloseIncidentRequest closeRequest = new CloseIncidentRequest();
                if (aResolution == null)
                {
                    aResolution = new IncidentResolution();
                    aResolution.Subject = "Case Closed";
                    aResolution.IncidentId = new EntityReference(Incident.EntityLogicalName,entity.Id);
                }
                closeRequest.IncidentResolution = aResolution;
                closeRequest.Status = new OptionSetValue(aNewStatus);
                CloseIncidentResponse closeResponse = (CloseIncidentResponse)aService.Execute(closeRequest);
            }
    
            public static void Test(XrmServiceContext xrm, string theServiceGuid)
            {
                if (xrm == null ||string.IsNullOrEmpty(theServiceGuid))
                {
                    throw new ArgumentNullException("serviceProvider");
                }
    
                // Construct the Local plug-in context.
                try
                {
                    Guid id = new Guid(theServiceGuid);
                    var curService = (ServiceAppointment)xrm.ServiceAppointmentSet.Where(a=> a.Id==id).FirstOrDefault();
    
                   decimal curBalance = 0M;
                    new_hours_account_statement lastStatement = (new_hours_account_statement)xrm.new_hours_account_statementSet.Where(a => a.new_HoursAccountId.Id == curService.new_HoursAccountId.Id).OrderByDescending(b => b.new_DateTo).FirstOrDefault();
                    if (lastStatement != null)
                        curBalance = lastStatement.new_EndingHoursBalance != null ? (decimal)lastStatement.new_EndingHoursBalance : 0M;
                    var serviceAppointments = xrm.ServiceAppointmentSet.Where(a => a.new_HoursAccountId.Id == curService.new_HoursAccountId.Id && a.new_new_hours_account_statement_serviceappointment_HoursAccountStatement == null && (int) a.StateCode !=2);
                    foreach (ServiceAppointment aService in serviceAppointments)
                        curBalance -= aService.new_BilledDuration != null ? (decimal)aService.new_BilledDuration : 0M;
                    new_hours_account curHoursAccount = xrm.new_hours_accountSet.Where(a => a.Id == curService.new_HoursAccountId.Id).FirstOrDefault();
                    curHoursAccount.new_Current_Balance = curBalance;
                    xrm.UpdateObject(curHoursAccount);
                    xrm.SaveChanges();
    
                    //UPDATE THE TOTAL NUMBER OF MINUTES FOR THE CASE
                    int curActualDuration = 0;
                    decimal curCaseBilled = 0M;
                    decimal curCaseCOMPANY = 0M;
                    if (curService.RegardingObjectId.LogicalName == "incident")
                    {
                        Incident curCase = xrm.IncidentSet.Where(a => a.Id == curService.RegardingObjectId.Id && (int)a.StateCode != 2).FirstOrDefault();
                        if (curCase != null)
                        {
                            // get the services associated with the case
                            serviceAppointments = xrm.ServiceAppointmentSet.Where(a => a.RegardingObjectId!=null &&  a.RegardingObjectId.Id == curCase.Id &&(int)a.StateCode != 2);
                            foreach (ServiceAppointment aService in serviceAppointments)
                            {
                                curActualDuration += aService.ActualDurationMinutes != null ? (int)aService.ActualDurationMinutes : 0;
                                curCaseBilled += aService.new_BilledDuration != null ? (decimal)aService.new_BilledDuration : 0M;
                                curCaseCOMPANY += aService.new_COMPANYTime != null ? (decimal)aService.new_COMPANYTime : 0M;
                            }
                            curCase.ActualServiceUnits = curActualDuration;
                            curCase.new_BilledTime = curCaseBilled;
                            curCase.new_COMPANYTime = curCaseCOMPANY;
                            xrm.UpdateObject(curCase);
                            xrm.SaveChanges();
                        }
                    }
    
    
                   }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred updating Hours Account Balance - ", ex);
                }
            }
            
        }
    }


    Appreciate the help!

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans