• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Blog
  • Power Platform
    • Power Apps
    • Power Automate
    • Power BI
    • Power Virtual Agents
  • Dynamics 365
  • Azure
  • About Me
Joe Gill Logo

Joe Gill

Microsoft MVP - Power Platform Consultant

Home » Power Platform » Dynamics 365 » Project Service Automation – Validate Hours for Time Entry

Project Service Automation – Validate Hours for Time Entry

11th May 2017 by Joe Gill Leave a Comment

This blog post covers how you can validate time entry in PSA, Project Service Automation, using a custom real time workflow to limit the amount of time a user can enter for any day.  The functionality of PSA is is pretty comprehensive however many users find it frustrating you cannot limit the total duration a user can book for a single day.

Project Service Automation -  Validate Hours for Time Entry - Joe Gill

To accomplish this I wrote a custom workflow activity that runs in realtime after a PSA time entry records is created or updated.

Project Service Automation -  Validate Hours for Time Entry - Joe Gill

The workflow takes four parameters which are self explanatory

Project Service Automation -  Validate Hours for Time Entry - Joe Gill The code is pretty simple and simply runs an aggegrate FetchXml query to get the total duration for the given day and resource and checks it does not exceed the maximum for a day. You can get the code and the solution from GitHub https://github.com/joegilldotcom/PsaUtils

public class ValidateTimeEntry : CodeActivity
{
[RequiredArgument]
[Input(“Resource”)]
[ReferenceTarget(“bookableresource”)]
public InArgument<EntityReference> Resource { get; set; }

[RequiredArgument]
[Input(“Entry Date”)]
public InArgument<DateTime> EntryDate { get; set; }

[RequiredArgument]
[Input(“Max Minutes per Day”)]
public InArgument<int> MaxMinutes { get; set; }

[RequiredArgument]
[Input(“Max Minutes Warning Message”)]
public InArgument<string> MaxMinutesWarning { get; set; }

protected override void Execute(CodeActivityContext executionContext)
{
ITracingService tracer = executionContext.GetExtension<ITracingService>();
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

EntityReference resource = this.Resource.Get(executionContext);
DateTime entryDate = this.EntryDate.Get(executionContext);
int maxMinutes = this.MaxMinutes.Get(executionContext);
string maxWarning = this.MaxMinutesWarning.Get(executionContext);

string fetchXml = @”<fetch version=’1.0′ distinct=’false’ mapping=’logical’ aggregate=’true’>
<entity name=’msdyn_timeentry’ >
<attribute name=’msdyn_duration’ aggregate=’sum’ alias =’durationtotal’/>
<filter type=’and’ >
<condition attribute=’msdyn_bookableresource’ operator=’eq’ value='{0}’ />
<condition attribute = ‘msdyn_date’ operator= ‘on’ value = ‘{1}’ />
</filter>
</entity>
</fetch>”;

string formatXml = string.Format(fetchXml, resource.Id.ToString(), entryDate.ToString(“yyyy-MM-dd”));
tracer.Trace(formatXml);

EntityCollection eColl = service.RetrieveMultiple(new FetchExpression(formatXml));

foreach (var c in eColl.Entities)
{
int? total = ((int?)((AliasedValue)c[“durationtotal”]).Value);
if (total != null & total > maxMinutes)
throw new InvalidPluginExecutionException(maxWarning);
}
}
}

 

 

 

 

Share This On Social:
  • Tweet
  • Project Service Automation -  Validate Hours for Time Entry - Joe Gill

Filed Under: Dynamics 365, Project Service Automation

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Popular Categories

  • Power Platform
    • Power Apps
    • Power Automate
    • Power Virtual Agents
  • Azure
    • Logic Apps
  • Dynamics 365
  • .NET
  • AI
  • SQL

More to See

Power Platform Requests – Base Request Capacity

17th December 2020 By Joe Gill

Power Automate Desktop

Power Automate Desktop – UI Flow

26th November 2020 By Joe Gill

Synapse Link for Dataverse – Metadata

5th July 2022 By Joe Gill

extract table from pdf and write as csv file using Power Automate Desktop

Extract Tables from a PDF using Power Automate Desktop

23rd May 2022 By Joe Gill

Tweets

Footer

Joe Gill

Microsoft Business Applications MVP – Power Platform, Dynamics 365 and Azure.

An architect with over twenty years experience designing and developing technology solutions. Specializing in the Microsoft technology stack including Power Platform, Dynamics 365 and Azure. Microsoft MVP Profile

Connect on Social

Useful Links

  • Home
  • Blog
  • About Joe Gill
  • Power Platform
  • Dynamics 365
  • Azure

Featured Posts

Power Platform Requests – Base Request Capacity

Power Automate Desktop – UI Flow

Power Fx Formula Columns in Dataverse

Synapse Link for Dataverse – Metadata

Extract Tables from a PDF using Power Automate Desktop

Dataverse Anonymization

© 2022 · Joe Gill