F1: Creating a Work Order Schedule in code in FieldOne

Posted on Posted in Microsoft, MS Dynamics, MS Dynamics CRM

There might be a reason when you need to create a work order schedule in code and assign it to a resource automatically or on certain requirements.

In below example I will explain how to create a Work Order Schedule in Plugin or Workflow activity and what is required.

Below are the required fields for creation of Work Order Schedule otherwise you will get an error

Resource (Look up)
Work Order (Look up)
Start time (Date time)
End Time (Date time)
Duration (Int)

Let’s say you already have

Resource (look up)
Work Order (look up)

You can get duration from the Primary Incident Type on Work Order by querying it

You can pass start time as a parameter in Workflow if you are creating a workflow activity such as..

[Input("Start time (hours)")]
public InArgument StartTime { get; set; }

End time will be Start Time + duration of the work order

Date can be set to any you want to, in this example lets suppose in next 5 days.

Once you have all required values just use code below to create a record

var _wos = new Entity("f1_workorderresource");
_wos["f1_starttime"] = _dateForSchedule;
_wos["f1_endtime"] = _dateForSchedule.AddMinutes(_duration);
_wos["f1_resource"] = resource;
_wos["f1_workorder"] = new EntityReference("f1_workorder", workorderid);
// _wos["f1_allowoverlapping"] = false;

var _id =service.Create(_wos);

If you need to skip weekends and/or bank holidays you can tweak your code and get Calendar rules for resource