How to call function from widget controller from onSubmit script?

peter_repan
Tera Guru

Hi all,

I have a record producer with a variable type Macro which is using widget, so it can be displayed in the Service Portal. 

I'd like to use onSubmit script and from this script I'd like to call the specific function from the widget controller. 

Is it possible? 

Directly call the function?

Or trigger some custom event in onSubmit script and listen to this event in the widget?

3 REPLIES 3

ChrisBurks
Mega Sage

Passing in $rootScope into the client controller function it is possible to listen for a few broadcasts/emits items from the widget that displays the catalog item/record producer (SC Catalog Item [widget-sc-cat-item-v2]) 

Two of the broadcasts/emits are "$sp.sc_cat_item.submitted" and "$sp.service_catalog.cart.submitted". 

$sp.sc_cat_item.submitted - when submitting an item by itself

$sp.service_catalog.cart.submitted - submitting a cart of items

To listen to the broadcasts/emits place something like the following in the client controller:

//Client controller
function($scope, $rootScope){
    var c = this;
    
    // listening for when item is submitted
    $rootScope.$on('$sp.sc_cat_item.submitted', function(evt, data){
        // do something in here when item is submitted
	console.log( "cat item", evt, data)
        alert("item: " + JSON.stringify(data))
    });


    // listening for when a cart is submitted
    $rootScope.$on('$sp.service_catalog.cart.submitted', function(evt, data){
        // do something in here when cart is submitted
        console.log("cart", evt, data)
        alert("cart: " + JSON.stringify(data))
    })
}

 

In the example I used an alert because it won't be possible to stop the submission (at least I don't think it can). The alert pauses it so that you can see the data that is provided; not that you have to use it but just as an FYI. 

But it is possible to execute a script to do something during the submission. 

Another alternative is to listen to field changes:

//Client controller
function($scope, $rootScope){

   var c = this;
   
   //listens to field changes on the item. The name of the field is provided in the data
   $rootScope.$on('field.change', function(evt, data){
       console.log("Field change",data );
   });

}

This will listen to field changes on the item. Inputs trigger when leave focus or on blur. The name of the field is available and the newValue and more.

 

Hope this helps

$rootScope.$on('$sp.sc_cat_item.submitted', function(evt, data){

Genius  😄

Thank you @ChrisBurks !!