web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Service Activity form - Javascript is changing the service default duration

(0) ShareShare
ReportReport
Posted on by 510

Hi -

I have a Service Activity custom form which utilises some Javascript code (on form creation) to populate the Service field automatically. The Service which I am populating the field with has a default duration of 8 hours. After I implemented the Javascript code, it does automatically set the service to the correct one - however when the service activity is saved it now creates a booking on the service calendar with a duration of 30 minutes (previously when I manually selected the service and had no JS it would create a booking of 8 hrs). I checked out the audit history of the service activity and it has a scheduled duration of 30 minutes. So, when the JS is implemented it seems to discard the service's default duration of 8 hours and apply a scheduled duration of 30 minutes. The service also has a default status of reserved, but when I make a service activity with the JS enabled, the status is set to pending by default. The only function I am using is below.

function setLookup() {
var lookup = new Array();
lookup[0] = new Object();
lookup[0].id = '571B5287-DE54-E411-907D-00155D1D4803';
lookup[0].name = '8 hr vehicle booking';
lookup[0].entityType = 'service';
Xrm.Page.getAttribute('serviceid').setValue(lookup);
//Xrm.Page.getAttribute("statuscode").setValue(4);
//Xrm.Page.getAttribute("scheduleddurationminutes").setValue(480);
}

(as you can see from the comments I have been trying to set the scheduled duration to 8 hours (480 mins) but this doesn't work either. If I manually select 8 hours from the Scheduled Duration drop down list, it does work.)

If anyone knows what is going wrong, any advice or direction is greatly appreciated.

Thanks

*This post is locked for comments

I have the same question (0)
  • Royal King Profile Picture
    27,686 on at

    Try setting submit mode to always for the scheduleddurationminutes filed

    Xrm.Page.getAttribute("scheduleddurationminutes").setSubmitMode("always");
  • swannylfc Profile Picture
    510 on at

    Thanks Chitra. I will have to try your solution tomorrow! I am away from the CRM environment at the moment. Sounds like a good option to try.

  • Verified answer
    Rasmus R. Aardal Profile Picture
    on at

    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

  • swannylfc Profile Picture
    510 on at

    Hi Rasmus,

    Thank you for your help again. This was exactly what I was looking for. I didn't realise that you need to specify the scheduled start/scheduled end as well as the duration. It is working a treat now. It is intended to be used by a large number of staff - to book out our fleet vehicles - who will not necessarily receive CRM training and so this is why I wanted to make a custom form - so I can keep things as simple as possible for them. As it stands, all the staff have to do now is fill out a form like so:

    NAME (Subject field on the Service Activity form - so the name appears on the calendar)

    REGISTRATION (Facilities/Equipment resources - so it verifies that the registration is correct)

    VEHICLE TAKEN check box

    VEHICLE RETURNED check box

    If they tick vehicle taken (and it hasn't already been booked out) then it will create a booking of 8 hours duration on the Service Calendar. When they return, they double click their booking, tick Vehicle Returned and it ends the booking and makes the resource available for new bookings.

    A workflow may have been able to accomplish a large part of this, as you say, but not using one this time has really helped my knowledge of implementing JS within CRM. I'm more of an amateur than a professional developer.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans