Autogenerate Case ID With Conditional Letter Prefix, Next Avail Number, and Current Year

145 Views Asked by At

I have researched this and have in mind how to do parts of it, but don't quite understand how to put it all together. When someone is creating a new record in my app, if the results of the InvestigationLog.CourtType dropdown is either "Transfer In (F)" or "Transfer In (M)", then I need to fill a text field, InvestigationLog.TCaseNo with a Case Number, but ONLY if it is one of those two choices. (There are other choices besides these two that should be disregarded.)

The case number needs to be in the format of T(then F or M depending upon the 2 choices above)-###-YY. For example, TF-001-24 would be the first F case of 2024.

My current .cshtml for this field is:

<td style="width: 25%">
    <div class="mb-3">
        <label asp-for="InvestigationLog.TCaseNo"></label>
        <input asp-for="InvestigationLog.TCaseNo" id="TCaseNumber" type="text" class="form-control" readonly="readonly" />
    </div>
</td>            

And the .cshtml for the Investigation.CourtType field is:

                <td style="width: 25%">
                    <div class="mb-3">
                        <label asp-for="InvestigationLog.CourtType"></label>
                        <select asp-for="InvestigationLog.CourtType" id="SelectCourtType" class="form-select" asp-items="@(new SelectList(Model.DisplayCourtTypeData.OrderBy(x => x.CourtTypeDesc),"CourtTypeDesc", "CourtTypeDesc"))" onchange="assignCaseNo" ><option value="" selected disabled>---Select Court Type---</option></select>
                    </div>
                </td>

I would like this TCaseNo to populate once someone has tabbed out of the InvestigationLog.CourtType dropdown. If they go back (before submitting and binding the record) to change the value of the CourtType dropdown, I would like it to change accordingly.

In my research on here, I have come up with the code below to increment the 'F' and 'M' TCaseNo:

Just don't know enough Javascript to do this correctly. I "think" I am pretty close. Just can't get my values to populate the .TCaseNo field on my create view.

Also, should I be doing one set of the variables that are the same for each outside of the if?

<script type="text/javascript">

    function assignData() {
            const assignCT = $("#SelectCourtType").val().trigger('input');
        }

    function updateTCaseNumber('#TCaseNumber') {
        if ('#SelectCourtType').val() = "Transfer In (F)"
        {
            var year = DateTime.Now.Year;
            int incFCaseNo = 0;
            "F" + String.Format("{0:000}", incFCaseNo, year);
            incFCaseNo++;
        }
        else if('#SelectCourtType').val() = "Transfer In (M)"
        {
            var year = DateTime.Now.Year;
            int incMCaseNo = 0;
            "M" + String.Format("{0:000}", incMCaseNo, year);
            incMCaseNo++;
        }
        else
        {
            null
        }
        $(document).ready(function () {
            $('#SelectCourtType').on('input', updateTCaseNumber);
        });

    }
</script>

THIS PART ANSWERED BELOW. I DO NOT NEED TO FREE UP NUMBER AND REUSE IT: I'm also not sure how to deal with the auto-generated number after the fact? For example, someone later wants to edit the CourtType and it is no longer a Transfer type. Should that TCaseNo be freed up to reuse? What happens to it? Is there standard practice on this?

If you can assist, please do.

EDITED TO ADD: Recently found this thread after many trial searches for the right words: Auto Generate control number base on input data

Looks like what I am looking for, except I still need help sussing out the CourtType dropdown to filter out the 'Transfer In (F)" and the "Transfer In (M)" values and use them to prefix the numbers. (NOTE: Each F and M case series needs to have their own 001+ series of numbers, ie NOT TF-001-24, TM-002-24, TM-003-24, TF-004-24, etc) Please HELP!! Nothing working yet.

Latest Javascript edit:

        <script type="text/javascript">
        
        let incFCaseNo = 1;
        let incMCaseNo = 1;
        
        function updateTCaseNumber(e) 
        {
        
            const input = $('#TCaseNumber');
            
            // value will be "Tranfer in (F)" or "Transfer in (M)"
            
            const selected = $('#SelectCourtType').val();

            if (selected === "Transfer In (F)") {
                // only take 2 digits of the year
                const yearF = new Date().getFullYear() % 100;
                // const caseNoF = "TF" + selected + String(incFCaseNo).padStart(3, '0') + year;
                const caseNoF = `TF-${String(incFCaseNo).padStart(3, '0')}-${yearF}`;
                input.val(caseNoF);
                incFCaseNo++;
            }
            else if (selected === "Transfer In (M)") {
                // only take 2 digits of the year
                const yearM = new Date().getFullYear() % 100;
                // const caseNoM = "TM" + selected + String(incMCaseNo).padStart(3, '0') + year;
                const caseNoM = `TM-${String(incMCaseNo).padStart(3, '0')}-${yearM}`;
                input.val(caseNoM);
                incMCaseNo++;
            }
            else { 
                input.val("");
            }
            
        }
    
    </script>

The javascript above will give me either a TF or TM number, but if I toggle between Transfer In (M) and Transfer In (F) in the dropdown, it will keep incrementing the sequence number. For example, if I pick Transfer In F, I will get TF-001-24. If I then toggle to Transfer In M, I will get TM-001-24. All good. BUT, if I toggle back to Transfer In F, I will then get TF-002-24. Not good. I can see in the code where the placement is making it do this, but do not know WHERE to place the increment so that it will increment AFTER I hit the submit button on the create page.

Can someone explain how I would do that?

2

There are 2 best solutions below

11
quyentho On BEST ANSWER

The problem can be solved by simple JS, I recommend you learn JS if you are serious with your career. But here is how it can be done, I'm assuming you are using JQuery, please notice the tag in the head of the document if you haven't done it:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    

    <!-- Include JQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
    <td style="width: 25%">
        <div class="mb-3">
            <label ></label>
            <input  id="TCaseNumber" type="text" class="form-control" readonly="readonly" />
        </div>
    </td>    
    <td style="width: 25%">
        <div class="mb-3">
            <label ></label>
            <select  id="SelectCourtType" class="form-select" onchange="updateTCaseNumber(event)" >
                <option value="" selected disabled>---Select Court Type---</option>
                <option value="F">Transfer In (F)</option>
                <option value="M">Transfer In (M)</option>
            </select>
        </div>
    </td>

<script type="text/javascript">
    let incFCaseNo = 0;
    function updateTCaseNumber(e) {
        const input = $('#TCaseNumber');
        
        // value will be "F" or "M"
        const selected = $('#SelectCourtType').val();

        if (selected === "") {
            input.val("");
        } else {
            // only take 2 digits of the year
            const year = new Date().getFullYear() % 100;
            // const caseNo = "T" + selected + String(incFCaseNo).padStart(3, '0') + year;
            const caseNo = `T${selected}-${String(incFCaseNo).padStart(3, '0')}-${year}`
            input.val(caseNo);
            incFCaseNo++;
        }
    }
</script>

</body>
</html>

1
Logarr On

This is the kind of rule that should be decided by the "business". It's the same kind of question as, "Must the identity be an un-broken sequence?" for numeric IDs.

Generally speaking if you have an ID that can be computed based on aspects of the object (a type/flag) and the conditions under which it was created (date, location, etc) then those things need to always accurately represent the object. Otherwise the standard is useless.

That means that if one of those attributes can be changed after the fact, the standard must allow for it. Imagine if you made TF-001-24, and someone changes it so that it needs to be TM-001-24, but later decides no wait... it really is a TF one. If you allow that original ID to be used by something else after the switch, you have broken the standard because now you either cannot change the initial case back to an F, or you can but the ID can't change. In the second situation you've officially negated the purpose of the computable ID.

In the standard you've provided, the 001 is not a subset of TF or TM. It is a standalone counter. You don't need to know if it's the first of its kind for the year, because that's technically not knowable if you can change the type after it was created.

My favorite issue tracker uses a modified version of this scheme where the shortcode for a Project is the prefix, and the number is the sequence of the issue within that Project. So BC-123, IR-456, etc. But the Project field can be changed! If I move BC-123 from Project BC to IR it becomes IR-457. But what about the old ID? Well, it's still mapped to that issue actually. So any links to BC-123 that might exist out in the world still work, and will take you to IR-457. If I change the Project back to BC it goes back to the original ID of BC-123 and now any links for IR-457 will lead there instead.

When it comes to computed IDs there must always be something about it that is unique, but not the whole thing. In your case the number of the case per the given year is that unique thing. There will always be an order to the cases being made, and so long as you don't need to worry about gaps in your IDs overall there's no reason that every other aspect of the case that you might put into the ID can be changed whenever.

The more important part here is that 001-24 will be enough to get you to that case no matter what other values exist. Think of it like the issues in that tracker. If part of the ID can change, the old ones must remain valid or else any references that exist frozen in time like on a sticky note or an email will be invalid.