Skip to main content

Notifications

Dynamics 365 Community / Forums / Field Service forum / Booking rule - Not wor...
Field Service forum

Booking rule - Not working - Help me

Posted on by 140

My Following JS not working. Problem with the Highlighted part

function newBookingRule(sbContext) {
debugger;
var ruleResult = {
IsValid: true,
Message: '',
Type: 'success'
};
var resourceReqId = sbContext.newValues.ResourceRequirementId;
var resourceId = sbContext.newValues.ResourceId;

var workOrderId = getRecord(resourceReqId, "_msdyn_workorder_value", "msdyn_resourcerequirements");
var WorkTerritoryId = getRecord(workOrderId, "_msdyn_serviceterritory_value", "msdyn_workorders");

var string = "/api/data/v8.2/msdyn_resourceterritory?$select=_msdyn_territory_value,_msdyn_resource_value&$filter=_msdyn_territory_value eq" + WorkTerritoryId + "and _msdyn_resource_value eq" + resourceId;


if (string) {
ruleResult.IsValid = true;
ruleResult.Message = "Success";
ruleResult.Type = 'success';
}
return ruleResult;
}


function getRecord(RecordId, Value, Entity) {
var req = new XMLHttpRequest();
req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + Entity + "(" + RecordId + ")?$select=" + Value), false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
req.send();
if (req.readyState === 4) {
req.onreadystatechange = null;
if (req.status === 200) {
window.getResult = JSON.parse(req.response);
return getResult[Value];
}
else {
Xrm.Utility.alertDialog(req.statusText);
return null;
}
}
}

function getRecords(newQuery)
{
var req2 = new XMLHttpRequest();
req2.open("GET", encodeURI(Xrm.Page.context.getClientUrl() +newQuery), false);
req2.setRequestHeader("Accept", "application/json");
req2.setRequestHeader("Content-Type", "application/json;charset=utf-8");
req2.setRequestHeader("OData-MaxVersion", "4.0");
req2.setRequestHeader("OData-Version", "4.0");
req2.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
req2.send();
if (req2.readyState === 4) {
req2.onreadystatechange = null;
if (req2.status === 200) {
return true;
} else {
Xrm.Utility.alertDialog(req2.statusText);
return false;
}
}
}

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Booking rule - Not working - Help me

    I can see you have constructed the query string - var string = "/api/data/v8.2/msdyn_resourceterritory?$select=_msdyn_territory_value,_msdyn_resource_value&$filter=_msdyn_territory_value eq" + WorkTerritoryId + "and _msdyn_resource_value eq" + resourceId;

    But I cant see it being passed into a HttpRequest to fetch anything (I am assume maybe you are just testing to see if the code runs?)

    Currenty the string is always going to be true so it will always be successful and nothing will appear.

    if (string) {
    ruleResult.IsValid = true;
    ruleResult.Message = "Success";
    ruleResult.Type = 'success';
    }
    return ruleResult;
    }

    Have you tried debugging your code?

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

News and Announcements

Give Back to the Community this Month

Quick Links

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,807 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,135 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans