If you are working in a scenario where you have to open
Quick Create Form through JavaScript and save/refresh the calling record
after the quick create form has been saved, you have few choices.
In this article I will show you how you can leverage the async and await
operators in JavaScript to solve this scenario.
First thing let’s try to define async
and await
Async: - pretty much a function
that returns a promise
First thing
let’s define the function that is going to Open
Quick Create Form
function QuickCreate() {
var entityFormOptions = {};
entityFormOptions["entityName"] = "contact";
entityFormOptions["useQuickCreateForm"] = true;
// Set default
values for the Contact form
var formParameters = {};
formParameters["firstname"] = "Sample";
formParameters["lastname"] = "Contact";
formParameters["fullname"] = "Sample Contact";
formParameters["emailaddress1"] = "contact@adventure-works.com";
formParameters["jobtitle"] = "Sr. Marketing Manager";
formParameters["donotemail"] = "1";
formParameters["description"] = "Default values for this record were set
programmatically.";
// Open the
form.
return Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
function (success) {
console.log(success);
return success
},
function (error) {
console.log(error);
return null;
});}
Next we
are going to define the function that is going to consume the QuickCreate() and see how we can use the JavaScript operators
async function ConsumeQuickCreate (formContext) {
//here you can
define which fields you want to update after the quick create has saved
formContext.getAttribute("statuscode").setValue("set your value here");
var success = await
QuickCreate();
if (success) formContext.data.refresh(save);
}
As you have noticed in
the code above formContext.data.refresh(save); is not going to be executed until
the QuickCreate() function has return a value.
Thanks For reading
I have to comment on my own post just to say the above code want work on IE 11 for the simple reason that IE 11 does not support ECMAScript 6.
ReplyDeleteIn case of IE 11 you have to use callback.