Execute Action with ColumnSet(ComplexType) type parameter Using Xrm.WebApi.online.execute in Dynamics 365 CRM V9.0

By | June 7, 2019

Introduction

Microsoft has introduced Xrm.WebApi.online.execute to execute an action in Dynamics CRM version 9.0. In our last blog we have seen how to Execute action using Xrm.WebApi.online.execute in Dynamics CRM. In this blog we will see how to execute action with “ColumnSet(ComplexType)” type property.

Here we are executing “GenerateQuoteFromOpportunity” action for sample purpose as this action contains the “ColumnSet(ComplexType)” type parameter.

Properties of ColumnSet(ComplexType) parameter:

xrm

For these properties we need to add another getMetadata method as shown in the below example.

Syntax:

Xrm.WebApi.online.execute(request).then(successCallback, errorCallback);

successCallback: This function call is for when is action executed successfully.

errorCallback: This function call is for when there is any error while executing action. It returns an error as result in string format.

Given below is the example of how to write the function to execute “GenerateQuoteFromOpportunity” action:

Generate Parameters:

/**

* This function generates quote from opportunity using GenerateQuoteFromOpportunity action

* @param formContext

* @param opportunityID

*/

function executeAction(executionContext) {

let functionName = “executeAction”;

var opportunityID = null;

var formContext = null;

try {

debugger;

//Get form context

formContext = executionContext.getFormContext();

//Get opportunity id

opportunityID = !isValid(executionContext.getFormContext().data)

|| !isValid(executionContext.getFormContext().data.entity)

|| !isValid(executionContext.getFormContext().data.entity.getId)

? null : executionContext.getFormContext().data.entity.getId();

//Set parameters

var GenerateQuoteFromOpportunityReq = function (OpportunityId, ColumnSetVal) {

this.OpportunityId = { “guid”: OpportunityId };

this.ColumnSet = {

“AllColumns”: ColumnSetVal,

“getMetadata”: function () {

return {

boundParameter: null,

operationType: 0,

operationName: “GenerateQuoteFromOpportunity”,

parameterTypes: {

“AllColumns”: {

“typeName”: “Edm.Boolean”, //Primitive Type

“structuralProperty”: 1

}

}

}

}

};

this.getMetadata = function () {

return {

boundParameter: null,

parameterTypes: {

“OpportunityId”: {

“typeName”: “Edm.Guid”, //Primitive Type

“structuralProperty”: 1

},

“ColumnSet”: {

“typeName”: “mscrm.ColumnSet”, //Complex Type

“structuralProperty”: 2

}

},

operationType: 0,

operationName: “GenerateQuoteFromOpportunity”

};

};

};

var generateQuoteFromOpportunityReq = new GenerateQuoteFromOpportunityReq(opportunityID, true);

}

Execute Action:

Xrm.WebApi.online.execute(generateQuoteFromOpportunityReq).then(function (result) {

//Validate result

if (!isValid(result) || !isValid(result[“responseText”])) {

return;

}

else {

//get result in JSON object

var resultJson = JSON.parse(result[“responseText”]);

//Validate JSON

if (isValid(resultJson)) {

//Validating the attributes

if (!isValid(Xrm) || !isValid(Xrm.Navigation)) { return; }

//openAlertDialog

Xrm.Navigation.openAlertDialog({ confirmButtonLabel: “Ok”, text: “Quote with ID: ” + resultJson.quoteid + ” is generated successfully. ” });

}

}

}, function (error) {

throwError(functionName, error);

});

} catch (error) {

throwError(functionName, error);

}

}

On success it returns a promise object with specific attributes.

In above example we have used ‘AllColums’ property of “ColumnSet(ComplexType)” parameter. In the same way we can use ‘Columns’ parameter. We just need to set “Edm.String” as typename in getMetadata method.

Conclusion

In this blog, we have seen how we can execute action with “ColumnSet(ComplexType)” type parameter using Xrm.WebApi.online.execute in Dynamics 365 CRM V9.0.

Click2Clone-One-Click-Productivity-App-to-Copy_clone-Dynamics-365_CRM-Records