Modal in Service Portal to update a field

joao_fonseca
Tera Contributor

Hello everyone, hope you're all doing fine.

We are in need of some help, as we are trying to create a widget that is a Modal that receives a string input and saves on the comments field, but we can't seem to make it work because when we send the input value from the Client Controller to the Server Script it keeps coming as undefined. 

I will provide you the codes we have.

Body HTML template:

<button ng-click="c.onOpen()" class="btn btn-default">
    Prompt with label
  </button>
  <div ng-show="c.name">
    You answered <span>{{c.name}}</span>
  </div>


Client Controller:

api.controller = function($scope, spModal) {
    var c = this;

    c.onOpen = function() {

        // Ask the user for a string
        spModal.open({
            title: 'Why do you want to cancel your ticket?',
            message: 'Reason:',
            input: true,
            value: c.name || '' // Use empty string if c.name is initially undefined
        }).then(function(name) {
            if (name) { // Ensure input was provided
                c.name = name;
                c.server.update().then(function(response) {
                   
                });
        });
    };


Server Script: 

(function() {
    /* populate the 'data' object */
    /* e.g., data.table = $sp.getValue('table'); */
    // Get table & sys_id
    data.table = input.table || $sp.getParameter("table");
    data.sys_id = input.sys_id || $sp.getParameter("sys_id");
    var name = input.name;
  
    var gr = new GlideRecord(data.table); // Ensure 'var' is used for variable declaration
    if (!gr.isValid())
        return;

    // Valid sys_id
    if (!gr.get(data.sys_id))
        return;

    //gr.setValue("comments", comments);  // Use the input comments
    gr.comments = name;

    gr.update();


We appreciate any help you can provide and hopefully make it work!

Best Regards
 

1 ACCEPTED SOLUTION

mayankkumar
Tera Sage

Hi @joao_fonseca,
-------------------------------------------------------------------------------------------------------------------------------
You need to make few changes
in this code--> 

           if (name) { // Ensure input was provided
                c.name = name;
                c.server.update().then(function(response) {
                   
                });

if block "}" is missing
and the major change You have to use "c.data.name" instead of c.name in HTML and Client Script then your widget will work.
I tried it and it is working fine let me know if you still struggle.
------------------------------------------------------------------------------------------------------------------------------------
Please Mark my response as helpful and accept it as Solution.
Thanks & regards
Mayank

View solution in original post

3 REPLIES 3

mayankkumar
Tera Sage

Hi @joao_fonseca,
-------------------------------------------------------------------------------------------------------------------------------
You need to make few changes
in this code--> 

           if (name) { // Ensure input was provided
                c.name = name;
                c.server.update().then(function(response) {
                   
                });

if block "}" is missing
and the major change You have to use "c.data.name" instead of c.name in HTML and Client Script then your widget will work.
I tried it and it is working fine let me know if you still struggle.
------------------------------------------------------------------------------------------------------------------------------------
Please Mark my response as helpful and accept it as Solution.
Thanks & regards
Mayank

It really worked, thank you very much! 

I did marked your response as helpful and accept it as Solution

T

hanks & regards

mayankkumar
Tera Sage

Hi @joao_fonseca 

 

If your query has been resolved, kindly mark my response as helpful or accept it as the solution so others can benefit as well.

------------------------------------------------------------------------------------------------------------------------------------
Please Mark my response as helpful and accept it as Solution.
Thanks & regards
Mayank