Hello swannylfc,
The commented out part of your function does accomplish what you are telling it to do. It sets the scheduleddurationminutes field on the service activity to 8 hours, but the service calendar reserves time based on the "scheduledstart" and "scheduledend" fields.
In other words, setting the scheduleddurationminutes field alone will not block out 8 hours in the service calendar for the selected resource.
May I ask how this is intended to be used? If the scheduled start and end times of the service activity are always the same (Say; 9am-5pm), this can be set in the same JavaScript function.
I took the liberty of altering your code to set scheduled start to TODAY at 9am, and scheduled end to TODAY at 7pm. If you want the date to be set to TOMORROW, simply add +1 to the date variable. (Note that I have added "if (Xrm.Page.ui.getFormType() == 1)" to make sure this code is only run the first time a service activity is opened to avoid the date being changed if it is opened and saved later on)
function SetLookup() {
if (Xrm.Page.ui.getFormType() == 1) {
var lookup = new Array();
lookup[0] = new Object();
lookup[0].id = '832AC107-6C5F-E411-B57A-6C3BE5BE4DE4';
lookup[0].name = 'testservice';
lookup[0].entityType = 'service';
Xrm.Page.getAttribute('serviceid').setValue(lookup);
Xrm.Page.getAttribute("statuscode").setValue(4);
Xrm.Page.getAttribute("scheduleddurationminutes").setValue(480);
//Retrieving the current date.
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth();
var date = today.getDate();
//Building date objects with the retrieved date variables
var scheduledstart = new Date(year, month, date, 09);
var scheduledend = new Date(year, month, date, 17);
//Setting sheduled start and end fields
Xrm.Page.getAttribute("scheduledstart").setValue(scheduledstart);
Xrm.Page.getAttribute("scheduledend").setValue(scheduledend);
}
}
All this being said, certain parts of this function can be done in a supported way through workflows. Setting the Service field to a specific service record can be done in a simple WF, but workflows have some shortcomings with regards to setting dates. (Dates can only be set to a fully static or a fully dynamic value e.g. "1 day, 6 hours AFTER execution time", meaning the scheduled start time would not always be at 9am of the current day.)
Good luck!
Regards,
Rasmus