Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

How to increment sequence number on other entity "B" when record is saved on entity "A".How can i achieve this

Posted on by 340

I have number field say "seqno"of type whole number consists of sequence number  starts from "500000"on entity "B" ,when entity "A" record  is loaded the "seqno" value is loaded to entity A from B and on save of entity A record the sequence number on B should be increment by 1. Here entity A and B does not have any lookup. How can i achieve this.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to increment sequence number on other entity "B" when record is saved on entity "A".How can i achieve this

    Hi Sandy,

    You can refer following js code:

     

    function number ()
    {
    Xrm.WebApi.online.retrieveMultipleRecords("new_test", "?$select=new_seqno&$orderby=createdon desc&$top=1").then(
        function success(results) {
            for (var i = 0; i < results.entities.length; i  ) {
                var new_seqno = results.entities[i]["new_seqno"];
                var new_seqno_formatted = results.entities[i]["new_seqno@OData.Community.Display.V1.FormattedValue"];
    
                if(new_seqno ==null)//the lastest test(B) record seqno field is null, create a new record with 50000
                {
                    var entity = {};
                    entity.new_name = "test500000";
                    entity.new_seqno = 50000;
                    
                    Xrm.WebApi.online.createRecord("new_test", entity).then(
                        function success(result) {
                            var newEntityId = result.id;
                        },
                        function(error) {
                            Xrm.Utility.alertDialog(error.message);
                        }
                    );
                }
                else // create a new record with seqno 1
                {
                    var seqno = new_seqno  1;
                    var entity = {};
                    entity.new_name = "test"   seqno;
                    entity.new_seqno = seqno;
                    
                    Xrm.WebApi.online.createRecord("new_test", entity).then(
                        function success(result) {
                            var newEntityId = result.id;
                        },
                        function(error) {
                            Xrm.Utility.alertDialog(error.message);
                        }
                    );
                }
            }
        },
        function(error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );
    }

    And Crm Rest Builder is a good tool to build request.

    This is the download page for it: https://github.com/jlattimer/CRMRESTBuilder

    1.retrieve one most recent test record.

     pastedimage1603347260940v1.png

    2.Create test record(B) with specific seqno value.

     pastedimage1603348994798v2.png

    3.Add the js code to OnSave event of the custom (A) entity.

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Sandy Hello Profile Picture
    Sandy Hello 340 on at
    RE: How to increment sequence number on other entity "B" when record is saved on entity "A".How can i achieve this

    Thanks for your response, i am new to flows is there any possible way to achieve this with Javascript

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to increment sequence number on other entity "B" when record is saved on entity "A".How can i achieve this

    Hi Sandy,

    The flow can achieve it, here are steps:

    In my example, custom(A) entity and test(B) entity have no relationship

    1.Go Power Automate(https://us.flow.microsoft.com/en-us/ ) to create an Automated flow.

    2.Create the Trigger event – in this case I have set the trigger to fire when a custom record is created.

    pastedimage1603162868058v2.png

    2.Add ‘List records’ step, Retrieve one test record with a most recent Created On date value.

    pastedimage1603162887311v3.png

    3.Add ‘compose’ step to get seqno value from ‘List records’ step.

    Once you do this and fill in the fields, Flow takes care of the Apply to each loop. So don’t get confused by this and look for a Apply to each. You don’t need to do this.

    pastedimage1603162907605v4.png

    4.In ‘Apply to each’ step, add ‘condition’ step to check seqno value is null or not.

    pastedimage1603163060999v5.png

    (1) In ‘Yes’ branch,

     Add ‘Compose’ step to convert seqno value to int and add 1.

    pastedimage1603163925650v6.png

    Add ‘create a new record’ and fill seqno field with the value of ‘compose2’ step.

     pastedimage1603164037806v7.png

    (2) In ‘No’ branch, add ‘create a new record’, fill seqno field with the 500000 value.

    pastedimage1603164108397v8.png

    5.Save and test the flow.

    The following is the overview of the flow:

    pastedimage1603164253245v1.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans