Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Some how the c.rows automatically changing back to 0 even though i have added a row

JayachandrReddy
Tera Contributor

Hi Team,
Some how the c.rows in the client controller script(custom variable) automatically changing back to 0 even though i have added a row, I have used this custom variable and widget in my record producer to replica the OOB MRVs.

 

HTML:

<div>
<p><strong><span style="font-size: 14pt; background-color: rgb(194, 224, 244);">Please list the names of the organization and individuals that ultimately control ≥ 25% of the organization's shares</span></strong></p>
<br>
<div class="table-container">
<table class="responsive-table">
<thead>
<tr>
<th></th>
<th>S.No</th> <!-- Auto-increment column -->
<th>Organization</th>
<th>Title/Position <span style="color:red;">*</span></th>
<th>First Name <span style="color:red;">*</span></th>
<th>Middle Name</th>
<th>Last Name <span style="color:red;">*</span></th>
<th>Street <span style="color:red;">*</span></th>
<th>City <span style="color:red;">*</span></th>
<th>State/Province <span style="color:red;">*</span></th>
<th>Country <span style="color:red;">*</span></th>
<th>Postal/Zip Code <span style="color:red;">*</span></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in c.rows track by $index">
<td><button class="btn btn-danger" ng-click="c.removeRow($index)">Remove</button></td>
<td>{{ $index + 1 }}</td> <!-- Auto-incrementing number -->
<td><textarea ng-model="row.organization" placeholder="Organization" rows="3" cols="30" oninput="limitLines(this, 3)"></textarea></td>
<td><input type="text" ng-model="row.titleposition" placeholder="Title/Position" required /></td>
<td><input type="text" ng-model="row.firstName" placeholder="First Name" required /></td>
<td><input type="text" ng-model="row.middleName" placeholder="Middle Name" /></td>
<td><input type="text" ng-model="row.lastName" placeholder="Last Name" required /></td>
<td><input type="text" ng-model="row.street" placeholder="Street" required /></td>
<td><input type="text" ng-model="row.city" placeholder="City" required /></td>
<td><input type="text" ng-model="row.state" placeholder="State/Province" required /></td>
<td><input type="text" ng-model="row.country" placeholder="Country" required /></td>
<td><input type="text" ng-model="row.postalcode" placeholder="Postal/Zip Code" required /></td>
<td><input type="hidden" ng-model="row.fieldName" value="{{ c.fieldName }}" /></td>
</tr>
</tbody>
</table>
<button class="btn btn-primary" ng-click="c.addRow()">Add</button>
<div class="alert alert-danger" ng-if="clientError" style="white-space: pre-wrap;">
<i class="fa fa-exclamation-triangle"></i> {{clientError}}
</div>
</div>
</div>

Client controller script:

api.controller = function($scope, $rootScope, snRecordWatcher) {

    var c = this;
    var g_form = $scope.page.g_form;
    c.rows = [];
    c.submitted = false;

    // Mandatory fields configuration
    c.mandatoryFields = [{
            display: "Title/Position",
            field: "titleposition"
        },
        {
            display: "First Name",
            field: "firstName"
        },
        {
            display: "Last Name",
            field: "lastName"
        },
        {
            display: "Street",
            field: "street"
        },
        {
            display: "City",
            field: "city"
        },
        {
            display: "State/Province",
            field: "state"
        },
        {
            display: "Country",
            field: "country"
        },
        {
            display: "Postal/Zip Code",
            field: "postalcode"
        }
    ];

    if ($scope.page && $scope.page.field && $scope.page.field.name) {
        c.fieldName = $scope.page.field.name;
    } else {
        c.fieldName = "default_container_id";
    }
    // Validation function
    c.validateForm = function() {
        var incompleteFields = [];

        var clientTypes = [
            g_form.getValue("type_of_client_s_new"),
            g_form.getValue("type_of_client_s_other_party_1"),
            g_form.getValue("type_of_client_s_other_party_2"),
            g_form.getValue("type_of_client_s_other_party_3"),
            g_form.getValue("type_of_client_s_other_party_4"),
            g_form.getValue("type_of_client_s_other_party_5"),
            g_form.getValue("type_of_client_s_other_party_6"),
            g_form.getValue("type_of_client_s_other_party_7"),
            g_form.getValue("type_of_client_s_other_party_8"),
            g_form.getValue("type_of_client_s_other_party_9"),
            g_form.getValue("type_of_client_s_other_party_10")
        ];

        var backupRows = [...c.rows]; // Backup rows before validation

        // Check if any client type requires Individual Details
        var requiresIndividualDetails = clientTypes.some(type =>
            type && ["Corporation", "Partnership", "Limited Partnership"].includes(type.trim())
        );

        // Ensure at least one row is added if Individual Details are required
        if (requiresIndividualDetails && c.rows.length === 0) {
            incompleteFields.push("Please add at least one row in Individual Details.");
        }

        // Restore rows if they were unintentionally reset
        if (c.rows.length === 0 && backupRows.length > 0) {
            c.rows = backupRows;
        }

        c.rows.forEach(function(row, rowIndex) {
            c.mandatoryFields.forEach(function(field) {
                if (!row[field.field] || String(row[field.field]).trim() === '') {
                    incompleteFields.push(field.display + " (Row " + (rowIndex + 1) + ")");
                }
            });
        });

        return incompleteFields;
    };
    g_form.$private.events.on('submit', function() {
        if (c.submitted) {
            return false; // Prevent duplicate submission
        }

        // Run validation first
        var missingFields = c.validateForm();

        if (missingFields.length > 0) {
            g_form.clearMessages();
            g_form.addErrorMessage("Please complete all required fields for Individual Details:\n" +
                missingFields.map(field => " " + field).join("\n"));
            return false;
        }

        // Ensure rows persist before submission
        var hiddenIdentifier = g_form.getValue("hidden_identifier");
        c.data.tableData = JSON.stringify(c.rows);
        c.data.hiddenIdentifier = hiddenIdentifier;

        // Mark as submitted before updating server
        c.submitted = true;
        c.server.update();

        return true;
    });


    c.addRow = function() {
        c.rows.push({
            organization: '',
            titleposition: '',
            firstName: '',
            middleName: '',
            lastName: '',
            street: '',
            city: '',
            state: '',
            country: '',
            postalcode: '',
            fieldName: c.fieldName
        });
    };

    c.removeRow = function(index) {
        c.rows.splice(index, 1);
    };
};




0 REPLIES 0