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.

Help on portal related Issue.

Sahil Chaudhary
Tera Contributor

I have created the portal with required pages and widgets (can be found in attachment). However, I am struggling with the submit function and the record is not getting inserted into the customer case table. When I click on the submit button, there is no response.

Attaching Files and screenshot

 

widget 1: Case Submission Form

 

HTML Code:

<div class="panel panel-default case-submission-form">
<div class="panel-heading"><h4>Submit a Case</h4></div>
<div class="panel-body">
<form ng-submit="c.submitCase()" novalidate>

<div class="form-group" style="margin-bottom: 20px;">
<label>Customer Name</label>
<input class="form-control" type="text" ng-model="c.form.customerName" placeholder="Your name" required>
</div>

<div class="form-group" style="margin-bottom: 20px;">
<label>Issue Description</label>
<textarea class="form-control" ng-model="c.form.issueDescription" rows="4" placeholder="Describe your issue" required></textarea>
</div>

<button class="btn btn-primary" type="submit">Submit Case</button>
</form>
</div>
</div>

 

Server Script:

(function() {

    // Handle form submission
    if (input && input.action === 'createCase') {
        try {
            var caseData = input.case || {};
            var gr = new GlideRecord('x_880053_csm_porta_customer_case');
            gr.initialize();

            gr.customer_name = caseData.customerName || gs.getUserName();
            gr.issue_description = caseData.issueDescription || '';
            gr.opened_by = gs.getUserID();

            var sysId = gr.insert();
            data.result = {
                sys_id: sysId.toString(),
                number: gr.getValue('number')
            };
        } catch (error) {
            gs.error('Error creating case: ' + error.message);
            data.error = 'Failed to create case. Please try again later.';
        }
    }
})();
 
Client Script:
api.controller = function() {
    var c = this;


    // Submit case
    c.submitCase = function() {
        try {
            if (!c.form.customerName || !c.form.issueDescription) {
                spUtil.addErrorMessage('Please fill in all required fields.');
                return;
            }

            c.server.update({
                action: 'createCase',
                case: c.form
            }).then(function(response) {
                if (response.data && response.data.result) {
                    spUtil.addInfoMessage('Case submitted: ' + response.data.result.number);
                    c.emit('cases:created', response.data.result);
                } else {
                    spUtil.addErrorMessage('Failed to submit case.');
                }
            }, function() {
                spUtil.addErrorMessage('Server error while creating case.');
            });
        } catch (error) {
            spUtil.addErrorMessage('An unexpected error occurred: ' + error.message);
        }
    };
};
 
Link:
function link(scope, element) {
    function focusFirstField() {
        var inputEl = element.find('input')[0];
        if (inputEl) inputEl.focus();
    }
    setTimeout(focusFirstField, 0);

    scope.$on('cases:created', function() {
        setTimeout(focusFirstField, 0);
    });
}
 
 
1 REPLY 1

GlideFather
Tera Patron

Hi @Sahil Chaudhary,

 

for submitting records to a table, widget isn't necessary. Alternatively, create a record producer applied on the Case table instead of it.

 

You can create that app (custom scope), so far so good, in that scope you will create a record producer, select a table, add title, description, all the necessary details, then variables (questions/fields) and then you will search for this form on that portal.

 

For the CSM /csm and /csp exists, there's no need to create a new portal unless really required...

 

let me know your progress

_____
This reply is 100 % GlideFather and 0 % AI