web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

Issue with Action execution from JavaScript and Custom Workflow Activity

(0) ShareShare
ReportReport
Posted on by 5

Hi, dear community.

Our team faced with the next issue:

We need to build a custom automation process for Creating a Quote with Quote Lines from the Opportunity form by a custom button on the web form. At the next step on each Quote Line, the functionality of Import from Project Estimation need to be executed.

Solved Steps:
1. Button was successfully created with JavaScript functionality for Quote creation. Placed on form and work as it should. Code below

function createQuote() {
            Xrm.Utility.showProgressIndicator("Wait please until Quote creating.");
            var opportunityId = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');
            var serverURL = Xrm.Page.context.getClientUrl();
            var query = "opportunities("   opportunityId   ")/Microsoft.Dynamics.CRM.msdyn_CreateQuoteFromOpportunity";
            var req = new XMLHttpRequest();
            req.open("POST", serverURL   "/api/data/v9.1/"   query, true);
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.setRequestHeader("OData-MaxVersion", "4.0");
            req.setRequestHeader("OData-Version", "4.0");
            req.onreadystatechange = function () {
                if (this.readyState == 4 /* complete */) {
                    req.onreadystatechange = null;
                    if (req.status >= 200 && req.status <= 300) {

                        var data = JSON.parse(this.response);

                        Xrm.Utility.closeProgressIndicator();

                        var entityFormOptions = {};
                        entityFormOptions["entityName"] = "quote";
                        entityFormOptions["entityId"] = data.quoteid;
                        importQuoteLineDetails(data.quoteid);

                        // Open the form.
                        Xrm.Navigation.openForm(entityFormOptions).then(
                            function (success) {
                                console.log(success);
                                
                            },
                            function (error) {
                                console.log(error);
                                alert(error.message);
                            });

                        console.log(data);
                    } else {
                        Xrm.Utility.closeProgressIndicator();
                        var error = JSON.parse(this.response).error;
                        console.log(error);
                        alert(error.message);
                    }
                }
            };
            req.send();
        }

As everybody can see we call an Action "msdyn_CreateQuoteFromOpportunity" (Project Service - Create Quote From Opportunity).

2. After form opens we can also find a Project-Based and Product-Based Quote Lines under Quote, automatically created based on Opportunity Lines from parent Opportunity

2020_2D00_06_2D00_25_5F00_17_2D00_05_2D00_04.png

3. The next point - Import Quote Line Details foreach Quote Line. Can be completed with manual clocking button on Quote Line form

2020_2D00_06_2D00_25_5F00_17_2D00_16_2D00_17.png

But we have a requirements for automate that step.

ISSUE DESCRIPTION:

For solving the last (3) step from the previous paragraph I created a Workflow with optionally ability to run it on-demand and inserted Custom Workflow Activity for executing Action "msdyn_CreateQuoteLineDetailsFromEstimate" (Project Service - Create Quote Line Details From Estimate), code below:

List quoteLines = service.RetrieveMultiple(quoteLinesQuery).Entities.ToList();
                    QuoteLines.Set(activityContext, quoteLines.Count);
                    quoteLines.ForEach(ql =>
                    {
                        OrganizationRequest req = new OrganizationRequest("msdyn_CreateQuoteLineDetailsFromEstimate");
                        req["groupByRole"] = 0;
                        req["groupByTransactionType"] = 0;
                        req["groupByTransactionCategory"] = 0;
                        req["groupByWbsLevel"] = 4;
                        req["Target"] = new EntityReference("quotedetail", ql.Id);
                        //execute the request
                        OrganizationResponse response = service.Execute(req);
                    });

Workflow description below (Custom Workflow Activity marked with red square):

2020_2D00_06_2D00_25_5F00_17_2D00_29_2D00_44.png

When I performed Unit Testing it work as it should - Quote Line Description was imported, as shown below:

2020_2D00_06_2D00_25_5F00_17_2D00_33_2D00_58.png

But when I tried to do the same from executing Workflow manually - it executed, completed successfully, but has no effect. Nothing imported.

Tried few times, - the same.

2020_2D00_06_2D00_25_5F00_17_2D00_36_2D00_48.png

As an option I also tried to implement the same functionality by JavaScript, code below:

function importQuoteLineDetails(quoteId) {
            Xrm.Utility.showProgressIndicator("Wait please until Quote Line Details importing.");
            Xrm.WebApi.retrieveMultipleRecords("quotedetail", "?$select=quotedetailid&$filter=producttypecode eq 5 and _quoteid_value eq "   quoteId).then(
                function success(result) {
                    for (var i = 0; i < result.entities.length; i  ) {
                        console.log(result.entities[i]);
                        var parameters = {
                            "groupByRole": 0,
                            "groupByTransactionType": 0,
                            "groupByTransactionCategory": 0,
                            "groupByWbsLevel": 4
                        };
                        var query = "quotedetails("   result.entities[i].quotedetailid   ")/Microsoft.Dynamics.CRM.msdyn_CreateQuoteLineDetailsFromEstimate";
                        console.log(query);
                        var req = new XMLHttpRequest();
                        req.open("POST", serverURL   "/api/data/v9.1/"   query, true);
                        req.setRequestHeader("Accept", "application/json");
                        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                        req.setRequestHeader("OData-MaxVersion", "4.0");
                        req.setRequestHeader("OData-Version", "4.0");
                        req.onreadystatechange = function () {
                            if (this.readyState == 4 /* complete */) {
                                req.onreadystatechange = null;
                                if (req.status >= 200 && req.status <= 300) {
                                    Xrm.Utility.closeProgressIndicator();
                                    var data = JSON.parse(this.response);                            

                                   

                                    console.log(data);
                                } else {
                                    Xrm.Utility.closeProgressIndicator();
                                    var error = JSON.parse(this.response).error;
                                    console.log(error);
                                    alert(error.message);
                                }
                            }
                        };
                        req.send(parameters);
                    }
                    // perform additional operations on retrieved records

                },
                function (error) {
                    console.log(error.message);
                    // handle error conditions
                }
            );
        }

It executes a long time and then fails.

I will be appreciated for any help and suggestions related to our problem.

Best Regards.

I have the same question (0)
  • Suggested answer
    SynchronicityAlex Profile Picture
    20 on at
    RE: Issue with Action execution from JavaScript and Custom Workflow Activity

    Hi Ruslan,

    not sure if you're still having this issue, but I ran into the same problem and have addressed it in code.  Following a review of how the custom action works natively (with Project Service automation), I found that the plugin was omitting the key operation of creating the lines.  The only reason I could see for this was that there must be a check for depth in the pipeline.

    To fix this, I added the request to an ExecuteMultipleRequest (even though there's only one call), and this seems to have 'wiped' the context and depth from the pipeline and allowed it to be created.

    I hope this helps!

    Alex.

  • Suggested answer
    Matthew Lazowski Profile Picture
    3,163 on at
    RE: Issue with Action execution from JavaScript and Custom Workflow Activity

    And this post described a scenario that is nearly a mirror image of what you try to achieve. It describes how to create a project with WBS from quote line details / contract line details using Flow.

    https://daytodaydynamics365.com/dynamics-365-project-service-automation-using-flow-to-create-a-project-with-tasks-based-on-project-contract-line-details/

  • Suggested answer
    Matthew Lazowski Profile Picture
    3,163 on at
    RE: Issue with Action execution from JavaScript and Custom Workflow Activity

    Hi Ruslan,

    I guess that the issue is performance-related but I am a PSA consultant not a developer.

    Have you tried referencing msdyn_createquotelinedetailsfromestimate action directly from a workflow?

    docs.microsoft.com/.../msdyn_createquotelinedetailsfromestimate

    In principle you would be doing the same as what you are doing in the plugin but maybe worth trying. If it does not work, you could log a support request with MS as you are not using any code in such case but only a workflow and an action from MS.

    The author of the article (www.itaintboring.com/.../ says the following: As a non-developer, you can call this custom action from the workflow. As a developer, you can call it from a plugin/javascript. But you can’t even change the business logic since it’s all embedded into a plugin.

    Please let us know if this has helped at all.

    Kind regards

    Matthew

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 823 User Group Leader

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 673 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 491 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans