web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Universal resource scheduling: Stop duplicate bookings

(0) ShareShare
ReportReport
Posted on by 225

Hi All,

we have a requirement to stop duplicate bookings - booking same resource for a slot that is already taken. We tried pre create plugin on Booking record but it as well does not look stop bookings created in same second. Is there any feature in URS or Field service that enables to throw errors for duplicate bookings? 

Many Thanks

NPV

I have the same question (0)
  • Suggested answer
    Charan Raju C R Profile Picture
    7 Moderator on at
    RE: Universal resource scheduling: Stop duplicate bookings

    Hi,

    Please register the below two plug-in steps which checks if there is already a booking exist for the selected resource which falls within the provided start and end time.

    1. Pre Create of Bookable Resource Booking

    2. Post Update of Bookable Resource Booking (filter attributes: starttime, endtime and resource. Pre Image: PreImage)

    public void Execute(IServiceProvider serviceProvider) 
           {
               IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext));
               IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
               IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
               if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
               {
                   Entity entity = (Entity)context.InputParameters["Target"];
                   Entity preImage = (Entity)context.PreEntityImages["PreImage"];
                   EntityReference resource = entity.Contains("resource") ? entity.GetAttributeValue("resource") :
                            (preImage.Contains("resource") ? preImage.GetAttributeValue("resource") : null);
                   DateTime startTime = entity.Contains("starttime") ? entity.GetAttributeValue("starttime") :
                       (preImage.Contains("starttime") ? preImage.GetAttributeValue("starttime").ToLocalTime() : DateTime.MinValue);
                   DateTime endTime = entity.Contains("endtime") ? entity.GetAttributeValue("endtime") :
                       (preImage.Contains("endtime") ? preImage.GetAttributeValue("endtime").ToLocalTime() : DateTime.MinValue);
                   if (startTime != DateTime.MinValue && endTime != DateTime.MinValue)
                   {
                       if (IsDuplicateBoking(service, entity.Id, resource, startTime, endTime))
                           throw new InvalidPluginExecutionException("Duplicate Booking");
                   }
               }
           }
           public bool IsDuplicateBoking(IOrganizationService service, Guid newBookingId, EntityReference resource, DateTime startTime, DateTime endTime)
           {
               bool isDuplicate = false;
               QueryExpression query = new QueryExpression("bookableresourcebooking")
               {
                   ColumnSet = new ColumnSet("starttime", "endtime"),
                   Criteria = { Conditions = { new ConditionExpression("resource", ConditionOperator.Equal, resource.Id),
                   new ConditionExpression("bookableresourcebookingid", ConditionOperator.NotEqual, newBookingId),
                       new ConditionExpression("statecode", ConditionOperator.Equal, 0)} }
               };
               EntityCollection existingBookings = service.RetrieveMultiple(query);
               foreach (Entity booking in existingBookings.Entities)
               {
                   DateTime existingStartTime = booking.Contains("starttime") ? booking.GetAttributeValue("starttime") : DateTime.MinValue,
                       existingEndTime = booking.Contains("endtime") ? booking.GetAttributeValue("endtime") : DateTime.MinValue;
                   if (existingStartTime != DateTime.MinValue && existingEndTime != DateTime.MinValue)
                   {
                       isDuplicate = (existingStartTime >= startTime && existingStartTime < endTime) || (existingEndTime > startTime && existingEndTime <= endTime);
                       if (isDuplicate)
                           break;
                   }
               }
               return isDuplicate;
           }

    Below is the validation error message appears when user try to book a same resource with overlapping time slot.

      Resource Booking Slot Overlapping Errorr

  • NPV Profile Picture
    225 on at
    RE: Universal resource scheduling: Stop duplicate bookings

    This plugin on pre-create is a good idea and we implemented with similar logic. Having a backend integration to create these Bookings and duplicate booking managed to drip in - 2 created in same second by multiple users.

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 125

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 101 Super User 2025 Season 2

#3
Erin Lubben Profile Picture

Erin Lubben 57

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans