2

spModal seems to have an option called "shared" as per here: https://github.com/service-portal/x-archive/blob/master/documentation/spModal.md (scroll down to shared).

I am trying to get this to work.

In our service portal I have a page with a client script that loads a widget, I have added the "shared" option here as per above link:

function onLoad() {
    if (g_scratchpad.canWrite && !g_form.isReadOnly('u_custom_company') && spModal) {

        var wait = setInterval(function() {
            var vendorEle = this.document.querySelector('#u_custom_company > .form-group');
            if (!vendorEle) {
                return;
            }
            
            var ccompany = {};
            
            var btn = this.document.createElement('button');
            btn.innerHTML = 'Add New Custom Company';
            btn.className = 'btn btn-sm btn-primary m-t-sm';
            btn.onclick = function() {
                spModal.open({
                    title: 'Add New Custom Company',
                    widget: 'new_custom_company',
                    footerStyle: { display: "none" },
                    shared: ccompany
                }).then(function() {
                        // Shared object was updated
                        console.log(ccompany);
                    });

            };
            vendorEle.appendChild(btn);

            clearInterval(wait);
        }, 500);
    }
}

In the above script I keep getting "undefined" in the result of the console.log().

Then in the client script of my custom widget I am unsure how to assign a value to this shared variable so that the above client script has access to it. This is what I have tried so far:

function($scope) {
        /* widget controller */
        var c = this;
        c.submit = function() {

            if ( $('#name').val() && ( $('#idone').val() || $('#idtwo').val() )) {
                c.data.action = 'createCustomCompany';
                c.server.update().then(function() {
                                    c.widget.options.shared = $scope.data.custom_companay.sys_id;

                                    });
                            
                // close modal popup when submit is clicked
                $scope.$parent.$parent.buttonClicked({ label: "Submit", submit: true });
            } else {
                $('.errormessage').show();
            }
        };
}

custom_company.sys_id is being populated by my server script and contains a sys_id, for example of a newly created custom company record.

I am unsure how to hand over the data from $scope.data.custom_companay.sys_id to the variable in the first client script?

BrokenCode
  • 951
  • 4
  • 19
  • 43

1 Answers1

0

it may be a bit late but adding it here just in case someone has the same problem in the future.

You can't set the widget options property through spModal. instead, use the widget input property. to access the data from your spModal, you need to write your code in the server script component of your widget.

(function() {  
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */

     if(input && input.shared){
       
         input.shared= "value from controller";
      }
 })();
MasterBettor
  • 99
  • 1
  • 1
  • 9