-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathxrm-webapi-samples.ts
More file actions
197 lines (168 loc) · 5.27 KB
/
xrm-webapi-samples.ts
File metadata and controls
197 lines (168 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// tslint:disable:no-console
// tslint:disable:no-empty
import {
associate,
batchOperation,
boundAction,
ChangeSet,
create,
createWithReturnData,
deleteProperty,
deleteRecord,
disassociate,
FunctionInput,
parseGuid,
retrieve,
retrieveMultiple,
retrieveMultipleNextPage,
unboundAction,
update,
updateProperty,
WebApiConfig
} from '../src/xrm-webapi';
const config: WebApiConfig = new WebApiConfig('8.1');
// demonstrate create
const account: any = {
name: 'Test Account'
};
create(config, 'accounts', account)
.then(() => {
console.log();
}, (error) => {
console.log(error);
});
// demonstrate create with returned odata
createWithReturnData(config, 'accounts', account, '$select=name,accountid')
.then((created: any) => {
console.log(created.name);
});
// demonstrate retrieve
retrieve(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), '$select=name')
.then((retrieved) => {
console.log(retrieved.data.name);
}, (error) => {
console.log(error);
});
// demonstrate retrieve multiple
const options: string = '$filter=name eq \'Test Account\'&$select=name,accountid';
retrieveMultiple(config, 'accounts', options)
.then(
(results) => {
const accounts: any[] = [];
for (const record of results.value) {
accounts.push(record);
}
// demonstrate getting next page from retreiveMultiple
retrieveMultipleNextPage(config, results['@odata.nextlink']).then(
(moreResults) => {
console.log(moreResults.value.length);
}
);
console.log(accounts.length);
},
(error) => {
console.log(error);
}
);
// demonstrate update. Update returns no content
update(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), account)
.then(() => {}, (error) => {
console.log(error);
});
// demonstrate update property. Update property returns no content
updateProperty(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'name', 'Updated Account')
.then(() => {}, (error) => {
console.log(error);
});
// demonstrate delete. Delete returns no content
deleteRecord(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'))
.then(() => {}, (error) => {
console.log(error);
});
// demonstrate delete property. Delete property returns no content
deleteProperty(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'address1_line1')
.then(() => {}, (error) => {
console.log(error);
});
// demonstrate delete navigation property. Delete property returns no content
deleteProperty(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'primarycontactid')
.then(() => {}, (error) => {
console.log(error);
});
// demonstrate associate. Associate returns no content
associate(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'),
'contact_customer_accounts', 'contacts', parseGuid('00000000-0000-0000-0000-000000000000'))
.then(() => {}, (error) => {
console.log(error);
});
// demonstrate disassociate. Disassociate returns no content
disassociate(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'contact_customer_accounts')
.then(() => {}, (error) => {
console.log(error);
});
// demonstrate bound action
const inputs: object = {
NumberInput: 100,
StringInput: 'Text',
};
boundAction(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'sample_BoundAction', inputs)
.then((result: any) => {
console.log(result.annotationid);
}, (error) => {
console.log(error);
});
unboundAction(config, 'sample_UnboundAction', inputs)
.then((result: any) => {
console.log(result.annotationid);
}, (error) => {
console.log(error);
});
// demonstrate bound function
const inputs3: FunctionInput[] = [];
inputs3.push({
name: 'Argument',
value: 'Value',
});
boundAction(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'sample_BoundFunction', inputs3)
.then((result: any) => {
console.log(result.annotationid);
}, (error) => {
console.log(error);
});
// demonstrate create. Custom action - Add note to account
const inputs4: FunctionInput[] = [];
inputs4.push({
alias: 'tid',
name: 'Target',
value: '{\'@odata.id\':\'accounts(87989176-0887-45D1-93DA-4D5F228C10E6)\'}',
});
unboundAction(config, 'sample_UnboundAction', inputs4)
.then((result: any) => {
console.log(result.annotationid);
}, (error) => {
console.log(error);
});
// demonstrate batch operation
const changeSets: ChangeSet[] = [
{
entity: {
name: 'Test 1'
},
queryString: 'accounts',
},
{
entity: {
name: 'Test 2'
},
queryString: 'accounts',
},
];
const gets: string[] = [
'accounts?$select=name',
];
batchOperation(config, 'BATCH123', 'CHANGESET123', changeSets, gets)
.then((result) => {
console.log(result);
}, (error) => {
console.log(error);
});