Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Appointment date and time overlapping

Posted on by 5,227

Hi,

While creating appointment I don't want to overlap time in between date-time which is already scheduled.

I am using below code to restrict but its not working:

QueryExpression AppointmentQuery = new QueryExpression
{
EntityName = "appointment",
ColumnSet = new ColumnSet(true)
};

FilterExpression filter = new FilterExpression(LogicalOperator.Or);

FilterExpression filter1 = new FilterExpression(LogicalOperator.And);
filter1.Conditions.Add(new ConditionExpression("scheduledstart", ConditionOperator.Equal, _startDate));
filter1.Conditions.Add(new ConditionExpression("scheduledend", ConditionOperator.Equal, _endDate));
filter1.Conditions.Add(new ConditionExpression("cs_meetingroom", ConditionOperator.Equal, MeetingRoomId));
filter1.Conditions.Add(new ConditionExpression("activityid", ConditionOperator.NotEqual, AppointmentSchedule.Id));

FilterExpression filter2 = new FilterExpression(LogicalOperator.And);
filter2.Conditions.Add(new ConditionExpression("scheduledstart", ConditionOperator.LessEqual, _startDate));
filter2.Conditions.Add(new ConditionExpression("scheduledend", ConditionOperator.GreaterEqual, _endDate));
filter2.Conditions.Add(new ConditionExpression("cs_meetingroom", ConditionOperator.Equal, MeetingRoomId));
filter2.Conditions.Add(new ConditionExpression("activityid", ConditionOperator.NotEqual, AppointmentSchedule.Id));

FilterExpression filter3 = new FilterExpression(LogicalOperator.And);
filter3.Conditions.Add(new ConditionExpression("scheduledstart", ConditionOperator.LessThan, _startDate));
filter3.Conditions.Add(new ConditionExpression("scheduledend", ConditionOperator.GreaterThan, _endDate));
filter3.Conditions.Add(new ConditionExpression("cs_meetingroom", ConditionOperator.Equal, MeetingRoomId));
filter3.Conditions.Add(new ConditionExpression("activityid", ConditionOperator.NotEqual, AppointmentSchedule.Id));

FilterExpression filter4 = new FilterExpression(LogicalOperator.And);
filter4.Conditions.Add(new ConditionExpression("scheduledstart", ConditionOperator.LessEqual, _startDate));
filter4.Conditions.Add(new ConditionExpression("scheduledend", ConditionOperator.Equal, _endDate));
filter4.Conditions.Add(new ConditionExpression("cs_meetingroom", ConditionOperator.Equal, MeetingRoomId));
filter4.Conditions.Add(new ConditionExpression("activityid", ConditionOperator.NotEqual, AppointmentSchedule.Id));

FilterExpression filter5 = new FilterExpression(LogicalOperator.And);
filter5.Conditions.Add(new ConditionExpression("scheduledstart", ConditionOperator.Equal, _startDate));
filter5.Conditions.Add(new ConditionExpression("scheduledend", ConditionOperator.LessEqual, _endDate));
filter5.Conditions.Add(new ConditionExpression("cs_meetingroom", ConditionOperator.Equal, MeetingRoomId));
filter5.Conditions.Add(new ConditionExpression("activityid", ConditionOperator.NotEqual, AppointmentSchedule.Id));

FilterExpression filter6 = new FilterExpression(LogicalOperator.And);
filter6.Conditions.Add(new ConditionExpression("scheduledstart", ConditionOperator.LessThan, _startDate));
filter6.Conditions.Add(new ConditionExpression("scheduledstart", ConditionOperator.GreaterThan, _endDate));
filter6.Conditions.Add(new ConditionExpression("cs_meetingroom", ConditionOperator.Equal, MeetingRoomId));
filter6.Conditions.Add(new ConditionExpression("activityid", ConditionOperator.NotEqual, AppointmentSchedule.Id));

FilterExpression filter7 = new FilterExpression(LogicalOperator.And);
filter7.Conditions.Add(new ConditionExpression("scheduledstart", ConditionOperator.GreaterEqual, _startDate));
filter7.Conditions.Add(new ConditionExpression("scheduledend", ConditionOperator.LessEqual, _startDate));
filter7.Conditions.Add(new ConditionExpression("scheduledend", ConditionOperator.GreaterEqual, _endDate));
filter7.Conditions.Add(new ConditionExpression("cs_meetingroom", ConditionOperator.Equal, MeetingRoomId));
filter7.Conditions.Add(new ConditionExpression("activityid", ConditionOperator.NotEqual, AppointmentSchedule.Id));


filter.AddFilter(filter1);
filter.AddFilter(filter2);
filter.AddFilter(filter3);
filter.AddFilter(filter4);
filter.AddFilter(filter5);
filter.AddFilter(filter6);
filter.AddFilter(filter7);

AppointmentQuery.Criteria = filter;

EntityCollection Appointments = service.RetrieveMultiple(AppointmentQuery);

First Appointment:

4265.Capture1.PNG

Second Appointment:

8168.Capture2.PNG

Is there any way to restrict in plugin.

Thanks 

*This post is locked for comments

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans