Skip to content

Commit 86d629a

Browse files
committed
Implemented retrieve by alternate key
1 parent ab5ae6c commit 86d629a

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/js/WebApiClient.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@
195195
if (params.entityId) {
196196
url += "(" + RemoveIdBrackets(params.entityId) + ")";
197197
}
198+
else if (params.alternateKey) {
199+
url += "(";
200+
201+
for (var i = 0; i < params.alternateKey.length; i++) {
202+
var key = params.alternateKey[i];
203+
204+
url += key.property + "='" + key.value + "'";
205+
206+
if (i + 1 === params.alternateKey.length) {
207+
url += ")";
208+
}
209+
else {
210+
url += ",";
211+
}
212+
}
213+
}
198214

199215
if (params.queryParams) {
200216
url += params.queryParams;

src/spec/WebApiClientSpec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
describe("WebApiClient", function() {
22
var fakeUrl = "http://unit-test.local";
33
var account;
4+
var contact;
45
var xhr;
56

67
Xrm = {};
@@ -18,6 +19,10 @@ describe("WebApiClient", function() {
1819
account = {
1920
Name: "Adventure Works"
2021
};
22+
23+
contact = {
24+
FirstName: "Joe"
25+
};
2126

2227
xhr = sinon.fakeServer.create();
2328

@@ -39,6 +44,12 @@ describe("WebApiClient", function() {
3944
[200, { "Content-Type": "application/json" }, JSON.stringify([account])]
4045
);
4146

47+
// Respond to Retrieve Request for contact with alternate key
48+
var retrieveByAlternateKeyUrl = RegExp.escape(fakeUrl + "/api/data/v8.0/contacts(firstname='Joe',emailaddress1='[email protected]')");
49+
xhr.respondWith("GET", new RegExp(retrieveByAlternateKeyUrl, "g"),
50+
[200, { "Content-Type": "application/json" }, JSON.stringify(contact)]
51+
);
52+
4253
// Respond to update Request for account
4354
var updateAccountUrl = RegExp.escape(fakeUrl + "/api/data/v8.0/accounts(00000000-0000-0000-0000-000000000001)");
4455
xhr.respondWith("PATCH", new RegExp(updateAccountUrl, "g"),
@@ -171,6 +182,28 @@ describe("WebApiClient", function() {
171182

172183
xhr.respond();
173184
});
185+
186+
it("should retrieve by alternative key", function(done){
187+
WebApiClient.Retrieve(
188+
{
189+
entityName: "contact",
190+
alternateKey:
191+
[
192+
{ property: "firstname", value: "Joe" },
193+
{ property: "emailaddress1", value: "[email protected]"}
194+
]
195+
})
196+
.then(function(response){
197+
expect(response).toEqual(contact);
198+
})
199+
.catch(function(error) {
200+
expect(error).toBeUndefined();
201+
})
202+
// Wait for promise
203+
.finally(done);
204+
205+
xhr.respond();
206+
});
174207
});
175208

176209
describe("Update", function() {

0 commit comments

Comments
 (0)