C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Calling Entity Bound Actions Using Xrm.Web API With Entity Collection
WhatsApp
Mahender Pal
4y
14k
0
1
100
Article
Introduction
In this article, we are going to discuss calling the entity bound action which has entity collection as a parameter. We need to create this parameter from an array list, while sending entity collection we need to send customer lookup as well, so let’s see how we can do that.
Details
Earlier, we discussed calling out of the box actions and custom actions using web API, but today, we will be using the Xrm.WebApi.execute method. You can refer to parameter details
here
.
I have created the following sample demo action which will call through Xrm.WebApi
if
(
typeof
(HIMBAP) ==
"undefined"
) {
var
HIMBAP = {
__namespace:
true
};
}
HIMBAP.EvenMainLibrary = {
RetrieveMultiple: async
function
(entityName, query) {
var
result = await Xrm.WebApi.retrieveMultipleRecords(entityName, query)
.then(
function
success(result) {
return
result.entities;
},
function
(error) {
return
null
;
}
);
return
result;
},
CallEventAction: async
function
(executionContext) {
var
formContext = executionContext.getFormContext();
//check for customer
if
(formContext.getAttribute(
"him_customer"
) !=
null
&&
formContext.getAttribute(
"him_customer"
).getValue() !=
null
) {
var
customerId = formContext.getAttribute(
"him_customer"
).getValue()[0].id;
var
query =
"?$select=him_name,_him_customer_value&$filter=_him_customer_value eq "
+ customerId;
var
results = await HIMBAP.EvenMainLibrary.RetrieveMultiple(
"him_event"
, query);
var
parameters = {};
var
entity = {};
entity.id = formContext.data.entity.getId().substring(1, 37);
entity.entityType =
"him_event"
;
parameters.entity = entity;
var
Events = [];
if
(results!=
null
&& results.length > 0) {
for
(
var
indx = 0; indx < results.length; indx++) {
Events.push({
//setup current entity id
"him_eventid"
: results[indx].him_eventid,
//setup current entity name
"@odata.type"
:
"Microsoft.Dynamics.CRM.him_event"
,
//setup loopup, make sure to use schema name of the lookup field
"
[email protected]
"
:
"/accounts("
+ results[indx]._him_customer_value +
")"
});
}
if
(Events.length > 0) {
parameters.Events = Events;
var
CustomEntityBoundActionDemoRequest = {
entity: parameters.entity,
Events: parameters.Events,
getMetadata:
function
() {
return
{
boundParameter:
"entity"
,
parameterTypes: {
"entity"
: {
"typeName"
:
"mscrm.td_event"
,
"structuralProperty"
: 5
},
"Events"
: {
"typeName"
:
"Collection(mscrm.crmbaseentity)"
,
"structuralProperty"
: 4
}
},
operationType: 0,
operationName:
"new_CustomEntityBoundActionDemo"
};
}
};
Xrm.WebApi.online.execute(CustomEntityBoundActionDemoRequest).then(
function
success(result) {
if
(result.ok) {
console.write(
"Action call completed!"
);
}
},
function
(error) {
console.write(error.message);
}
);
}
}
}
}
};
In the above code, we are getting all the events where we have the same customer, once we have events available we are creating Events object array and adding all the events details to this object array with event details and customer lookup. Finally, we are forming a request to call our action and passing it to Xrm.WebApi.online.execute.
I hope it will help someone!!
Keep learning and keep sharing!!
calling action using Xrm.WebApi
Dynamics CRM
passing entity collection to action
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.5k people
Download Now!
Learn
View all
HIMBAP
We are expert in Microsoft Power Platform.
Membership not found