Schedule a Resource

This sample code shows how to schedule a resource with the following scenario. A plumber and van need to be scheduled to investigate and fix a leak at a customer site. The earliest available appointment needs to be made. In order for this to be accomplished, all preliminary records must be created. The default 24-hour calendars will be used. No sites are created for this sample.




using System;
using CrmSdk;
using Microsoft.Crm.Sdk.Utility;

namespace Microsoft.Crm.Sdk.HowTo
{
///
/// This sample shows how to schedule a resource.
///

public class ServiceManagement
{
public static bool Run(string crmServerUrl, string orgName)
{
// Set up the CRM Service.
CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);

#region Setup Data Required for this Sample
bool success = false;

#endregion

try
{
// Get the current user's information.
WhoAmIRequest userRequest = new WhoAmIRequest();
WhoAmIResponse user = (WhoAmIResponse) service.Execute(userRequest);

// Create the van resource.
equipment van = new equipment();
van.name = "Van 1";
van.timezonecode = new CrmNumber();
van.timezonecode.Value = 1;
van.businessunitid = new Lookup();
van.businessunitid.type = EntityName.businessunit.ToString();
van.businessunitid.Value = user.BusinessUnitId;

// Create the van object.
Guid vanId = service.Create(van);

// Create the plumber resource group.
constraintbasedgroup group = new constraintbasedgroup();
group.businessunitid = new Lookup();
group.businessunitid.type = EntityName.businessunit.ToString();
group.businessunitid.Value = user.BusinessUnitId;
group.name = "Plumber with Van 1";
System.Text.StringBuilder builder = new System.Text.StringBuilder("");
builder.Append("");
builder.Append("");
builder.Append("resource[\"Id\"] == ");
builder.Append(user.UserId.ToString("B"));
builder.Append(" || resource[\"Id\"] == ");
builder.Append(vanId.ToString("B"));
builder.Append("");
builder.Append("");
builder.Append("");
builder.Append("
");
builder.Append("
");
builder.Append("
");
builder.Append("
");
group.constraints = builder.ToString();
group.grouptypecode = new Picklist();
group.grouptypecode.Value = 0;

Guid groupId = service.Create(group);

// Create the resource specification.
resourcespec spec = new resourcespec();
spec.businessunitid = new Lookup();
spec.businessunitid.type = EntityName.businessunit.ToString();
spec.businessunitid.Value = user.BusinessUnitId;
spec.objectiveexpression = @"

udf ""Random""(factory,resource,appointment,request,leftoffset,rightoffset)









";
spec.requiredcount = new CrmNumber();
spec.requiredcount.Value = 1;
spec.name = "Test Spec";
spec.groupobjectid = new UniqueIdentifier();
spec.groupobjectid.Value = groupId;

Guid specId = service.Create(spec);

// Create the plumber required resource object.
RequiredResource plumberReq = new RequiredResource();
plumberReq.ResourceId = user.UserId;// assume current user is the plumber
plumberReq.ResourceSpecId = specId;

// Create the van required resource object.
RequiredResource vanReq = new RequiredResource();
vanReq.ResourceId = vanId;
vanReq.ResourceSpecId = specId;

// Create the service.
service plumberService = new service();
plumberService.name = "Plumber1";
plumberService.duration = new CrmNumber();
plumberService.duration.Value = 60;
plumberService.initialstatuscode = new Status();
plumberService.initialstatuscode.Value = 1;
plumberService.granularity = "FREQ=MINUTELY;INTERVAL=15;";
plumberService.resourcespecid = new Lookup();
plumberService.resourcespecid.type = EntityName.resourcespec.ToString();
plumberService.resourcespecid.Value = specId;
plumberService.strategyid = new Lookup();
// This is a known GUID for the default strategy.
plumberService.strategyid.Value = new Guid("07F7DC72-1671-452D-812C-7172D3CA881F");

Guid plumberServiceId = service.Create(plumberService);

// Create the appointment request.
AppointmentRequest appointmentReq = new AppointmentRequest();
appointmentReq.RequiredResources = new RequiredResource[] {vanReq};
appointmentReq.Direction = SearchDirection.Forward;
appointmentReq.Duration = 60;
appointmentReq.NumberOfResults = 10;
appointmentReq.NumberOfResults = 1;
appointmentReq.ServiceId = plumberServiceId;

// The search window describes the time when the resouce can be scheduled.
// It must be set.
appointmentReq.SearchWindowStart = new CrmDateTime();
appointmentReq.SearchWindowStart.Value = DateTime.Now.ToUniversalTime().ToString();
appointmentReq.SearchWindowEnd = new CrmDateTime();
appointmentReq.SearchWindowEnd.Value = DateTime.Now.AddDays(7).ToUniversalTime().ToString();
appointmentReq.UserTimeZoneCode = 1;

// Create the request object.
SearchRequest search = new SearchRequest();

// Set the properties of the request object.
search.AppointmentRequest = appointmentReq;

// Execute the request.
SearchResponse searched = (SearchResponse)service.Execute(search);

#region check success

if (searched.SearchResults.Proposals.Length > 0)
{
success = true;
}

#endregion

#region Remove Data Required for this Sample

service.Delete(EntityName.service.ToString(), plumberServiceId);
service.Delete(EntityName.equipment.ToString(), vanId);

#endregion
}
catch (System.Web.Services.Protocols.SoapException ex)
{
// Add your error handling code here.
Console.WriteLine(ex.Detail.InnerText);
success = false;
}

return success;
}
}
}