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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Automatically update field of entity

(0) ShareShare
ReportReport
Posted on by 314

Hi all,

I have a scenerio in which i have 2 custom entities Country and City and They have 1 to N relation. Every country has some cities in grid. I have added a custom field "Number of cities" I want it to be updated automatically every time when i add a new city.

country.JPG

when i click + button it directly opens a new city form as i made country lookup business required in City entity

 city.JPG

When clicked on Save, a query should be fired against City finding total cities provided current Country as a criteria and update the Number of city field in Country entity. will it be possible through Out of box workflows and Javascript WebAPI.

Thanks in Advance!

Ishan Sharma

*This post is locked for comments

I have the same question (0)
  • Kokulan Profile Picture
    18,054 on at

    You could have the a roll-up field on the country to have the count of the Cities

    And you can then have a workflow (this could be real-time or background depending on how fast you need the total) that triggers on Create of City.

    This workflow can do a force calculate roll-up using - CalculateRollupFieldRequest.

    You can create your own custom workflow activity(CWA) and use that in the workflow or you can use a free Workflow Tool step to do the same

    if you want to create a CWA you can have a look at the following link

    www.magnetismsolutions.com/.../microsoft-dynamics-crm-sdk-manually-calculate-rollup-fields

    If you are okay with using the workflow tools (this has loads of other utility functions may be handy for other workflows and actions), please see the following

    github.com/.../Dynamics-365-Workflow-Tools

    You could do the same from JS but then it will only trigger if the city was added from UI. If the city was imported using CSV or other means, it wont work.  So better have the logic in Workflow or plugin.

    By the way you can always have the logic in a plugin as well if that's your preference - Plugin is always much much better in terms of  performance  than other approaches

  • gdas Profile Picture
    50,091 Moderator on at

    Hi Ishan,

    The best way to do this using rollup field . However if you want to do it using Web API , you can download CRMRestBuilder and build your query.

    Here is sample code using JS , think twice before use JS using JS Web API code , the field will be calculated at the time of form load or when you trigger the API code in the UI , if you push data from back end , or using SDK method then the count  field value will not be updated automatically , so you can go for post create , update event  plugin of city entity where you need to simply retrieve the total city and update the field.

    function onloadCountryForm() {
    
        var countryid = Xrm.Page.data.entity.getId();
        if (countryid != null) {
            countryid = countryid.replace('{', '').replace('}', '');
    
            var req = new XMLHttpRequest();
            req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_city?$select=new_name,new_country&$filter=_new_countrylookupid_value eq " + countryid + "", true);
            req.setRequestHeader("OData-MaxVersion", "4.0");
            req.setRequestHeader("OData-Version", "4.0");
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
            req.onreadystatechange = function () {
                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 200) {
                        var results = JSON.parse(this.response);
                        var count = results.value.length;
                        Xrm.Page.getAttribute("new_citiycount").setValue(count);
                    } else {
                        Xrm.Utility.alertDialog(this.statusText);
                    }
                }
            };
            req.send();
        }
    }


    Watch my video , how to generate JS API code using CRMRestBuilder.

    https://www.youtube.com/watch?v=VgumdjLd57E

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Ishan,

    In addition I tried this solution using workflow for you. Here you need do following steps. 

    1 . Make sure "City count" field should be whole number.

    2 . Create another field called "Increment" which is type whole number in the country entity.

    3. Make sure "Increment"  field  you should  fill/set default value "1" in create form and hide   the field update form. 

    4.Now create a workflow on City entity create event.

    5. In the workflow add step update record , choose the lookup of the country there click on view properties.

    2019_2D00_01_2D00_29_5F00_22_2D00_54_2D00_06_5F00_work2.png

    6.In the update of country form , put the cursor on  "City Count" , now under 

    2019_2D00_01_2D00_29_5F00_22_2D00_54_2D00_06_5F00_work2.png

    7. See the result, for my case here my entities are category and subcategory.

    2019_2D00_01_2D00_29_5F00_23_2D00_08_2D00_45_5F00_work3.png

    Hope this helps.

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    Just a friendly reminder, if you decided go for Goutam's workflow, you have to consider if city is removed/deleted from list. ( you have to use decrement by). Trigger on Record is deleted.

    Besides that, please add checking condition to workflow, ( if country field contain data).

    But if you dont require instant response, I will vote for Rollup feature suggested by Kokulan .

  • Ishan Sharma Profile Picture
    314 on at

    Hi all, i tried using workflows and it is working 

    totalcities.JPG

    Everytime when i create a new city from country it is incrementing the CityCount field as shown above. the only step i am having to do is refreshing the form just after adding the city to see updated count

  • Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi IShan,

    a friendly reminder, did you consider the scenario where user delete the city from country?

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Ishan ,

    For deletion scenario you need to create another workflow in the delete event of the city where in the same way in the update steps you need to decrement the counter using decrement by. Thats it .

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans