0
votes

I have two SharePoint lists Issues(Parent) with Title column and Time(Child list) with Title and Issue(lookup to Title of Issues) columns. The lists are connected so that the parent item display children items. What I am looking is when a child is added to a parent, I want to start counting from 1 and keep increment based on the parent item. For example, Parent A doesn't have a child so when the first child item(First time) is added I want a Title column of Time to auto populates 1 on Default New Form, other case is Parent B have 2 children items so when a third one is added Title column of Time to auto populates 3 on Default New Form. I prefer to use java script or jquery.

1
Please include some codeNino Filiu

1 Answers

0
votes

Sample code for your reference.

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript">
        $(function () {
            ExecuteOrDelayUntilScriptLoaded(PreInit, "sp.js");
        });
        function PreInit() {
            $('select[Title="Issue"]').change(function () {
                var selectedItem = $(this).children("option:selected").val();                
                $.ajax({
                    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists(guid'" + _spPageContextInfo.pageListId.replace('{', '').replace('}', '') + "')/Items?$select=Title&$filter=Issue/Id eq " + selectedItem + "",
                    method: "GET",
                    headers: {
                        "Accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    },
                    success: function (data) {
                        var count = data.d.results.length;
                        $('input[title="Title Required Field"]').val(count+1);
                    },
                    error: function (data) {
                        console.log(data);
                    }
                });
            });
        }
    </script>

enter image description here

enter image description here