Finding next available appointment date when creating an appointment in CRM Script

lock
push_pin
done
Answered
5

Hello friends!

I've been messing with the Netserver Appointment Agent:

https://docs.superoffice.com/automation/crmscript/reference/CRMScript.NetServer.NSAppointmentAgent.html

But I can't get it to do what I want to automate.

I'm trying to do this:

In the image, I get a selection of the next available time slots that fit the appointment without conflicts - so the functionality is there.

But how is it made available to either the REST API or CRM Script ?

I'd rather not implement a similar function myself, as I'm concerned with how much performance it will eat from the system.

I hope you can help.

8 Sep 2022 | 07:32 AM

All Replies (5)

Hi,

This is based on the 'NextAvailableTime' MDO provider, sample using CRMScript:

#setLanguageLevel 3;

// MDO provider: NextAvailableTime
// Format additional info [<DateTime start appointment>][<DateTime end appointment>]|<is all day event? 0/1>|<associate id>
// [DT:09/08/2022 11:30:00.0000000]|[DT:09/08/2022 11:45:00.0000000]|0|1993

DateTime start = DateTime(2022, 8, 9, 11, 30, 0);
DateTime end = DateTime(2022, 8, 9, 12, 30, 0);

NSMDOAgent mdoAgent;
Integer associateId = 1993;
Bool allDayEvent = false;
String additionalInfo = "[DT:" + start.toString("DD2/MM2/YY4 HH24:MI2:SS2") + "]|[DT:" + end.toString("DD2/MM2/YY4 HH24:MI2:SS2") + "]|" + allDayEvent.toInteger().toString() + "|" + associateId.toString();

foreach (NSMDOListItem listItem in mdoAgent.GetList("NextAvailableTime", true, additionalInfo, false))
{
  	printLine(listItem.GetName());
}

You can call this MDO list using the REST api

8 Sep 2022 | 09:59 AM

Brilliant!
Thank you so much!

8 Sep 2022 | 10:01 AM

Huh, interesting. If you provide it with a weekday, the function presumes that you want an open booking on a weekday. If you pick a saturday, it assumes you want an open booking on specifically a saturday - and the same for sunday.

I wonder if this is system specific or if it's a more general thing.

This functionality is worth knowing about if you're booking appointments during a cron job run on the weekend and you use the servers date as basis.

8 Sep 2022 | 02:19 PM
It also takes into account the users Preferences:
Section: DiaryView, Preferences: AllDayStartTime, AllDayEndTime, LunchStartTime, LunchEndTime

As you have noted, it also takes into account the following appointment types (not just workday/weekday...):
AllDaySaturday (All day on a Saturday.)
AllDayRedDay (All day on a Sunday/Red day)
AllDayWorking (All day on a working day)
MultiDayWorking (Lasting several days, but only working days)
MultiDayOffDay (Lasting several days, but only off days)
MultiDayFixedDays (Lasting several days, including both work and off days)
DayPartRedDay (A day part on a Sunday/Red day)
DayPartSaturday (A day part on a Saturday)
DayPartBeforeWork (Part of the day, but before the working day starts)
DayPartAfterWork (Part of the day, but after the working day ends)
DayPartStretchedBefore (Starts before working hours, but lasts into working hours. Considers lunch)
DayPartWithDinner (Starts during working hours, but stretched more than two hours after working day ends. Considers lunch)
DayPartStretchedAfter (Starts during working hours but ends after. Considers lunch)
DayPartWithLunch (Meeting during the working hours where lunch is included. Considers lunch)
DayPartMorning (Meeting starting after working hours starts and completes before lunch. Considers lunch)
DayPartAfternoon (Meeting starts after lunch and completes before end of the working day. Considers lunch)
DayPartLong (All other day meetings)

Hope this helps!
9 Sep 2022 | 08:52 AM

Oh, and additional info for other people:

The solution can throw a Netserver exception related to whether the date format is correct. It'll complain about an issue with UI culture, so make sure that the format matches the one used internally by the superoffice instance. It will simply say something like, 'expected DateTime, got xxxxxxx'.

9 Sep 2022 | 02:43 AM

Thanks for that Tony, that's great to know!

I wanted to also let you all know that I think I discovered a bug.

When I adapted David's code for my circumstances, I reused the 'foreach' loop. This worked fine both when the script was called from service and via a rest-api call - but when I then tried to run a trace on the script after I used the rest-api call, the trace refused to load; it effectively timed out. Which was quite curious, since the code actually did what it was supposed to.

When I replaced the foreach loop with a traditional for loop, that's all it took to get 'trace' to work again even when run via a rest api call.

I have a hunch it may be some kind of concurrency error, as the rest api allows concurrent script execution, and I imagine this could have some kind of effect on a loop accessing the list - but I'm not sure - I assume CRM Script is interpreted, and not cross compiled, as foreach loops, when compiled into an intermediate language 'just in time', are typically replaced with while loops, so it may be that there is some kind of functionality that differs between a traditional for loop and the foreach loop - and that this somehow comes into play when executed via a rest call.

Unless the content of the for-each loop also somehow relates to the bug, it should actually reproduce with Davids code, a rest api call to activate it, and a trace. Let me know if you need my version of the for-each loop.

14 Sep 2022 | 11:17 AM
Hi,
We have already fixed a bug related to tracing CRMScripts containing foreach loops in upcoming online release (10.1.6).
/Michel
14 Sep 2022 | 01:02 PM

Add reply