Skip to main content

Notifications

Announcements

No record found.

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

  • NPV Profile Picture
    NPV 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.

  • Suggested answer
    Charan Raju C R Profile Picture
    Charan Raju C R 7 Super User 2024 Season 1 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

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans