The CreatorCon Call for Content is officially open! Get started here.

SweetAlert - a beautiful replacement for javascript's "alert"

Matt Saxton - G
Kilo Guru

 

SweetAlerts2 is out. 

 

SweetAlerts2

 

Hi Dev's,

 

Last night i was working on an application that needed some input. So i used SweetAlerts on the custom UI page and it turned out really nice looking, and i thought i could use globally!.

 

ServiceNow Share - SweetAlert

 

Once you install you can do any of the tests via the console

 

parent.swal({     title: "Are you sure?",     text: "You will not be able to recover this imaginary file!",     type: "warning",     showCancelButton: true,     confirmButtonColor: "#DD6B55",     confirmButtonText: "Yes, delete it!",     closeOnConfirm: false }, function(){     swal("Deleted!", "Your imaginary file has been deleted.", "success"); });

 

Make sure that you use "parent.swal()"

 

Here's more examples listed here SweetAlert

 

Screenshot's below.

 

2015-10-01 13_48_38-ServiceNow.png

 

2015-10-01 13_37_06-ServiceNow.png

44 REPLIES 44

Try this example. Change wording/options to required:

 


swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

elemonnier
Tera Expert

It doesn't seem to reach the function(isConfirm) in your example. Does it work for you?

 

 

Hi,

Just to confirm. Are you using SweetAlert or SweetAlert2?

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

Paul Curwen
Giga Sage

Actual code below (SweetAlert 1 example)  tested in Jakarta as working with SweetAlert (Not 2), isConfirm options work as expected.

 

If you are using SweetAlert 2 then look here: https://sweetalert.js.org/guides/ at the section 'Upgrading from 1.x" The most important change is that callback functions have been deprecated in favour of promises i.e. you should be using .then((value) { rather than function(isConfirm) {if (isConfirm) { etc

 

SweetAlert 2 Question/Response Example:

function testit() {

swal({
  title: "Are you sure?",
  text: "Once deleted, you will not be able to recover this imaginary file!",
  icon: "warning",
  buttons: true,
  dangerMode: true,
})
.then((willDelete) => {
  if (willDelete) {
    swal("Poof! Your imaginary file has been deleted!", {
      icon: "success",
    });
  } else {
    swal("Your imaginary file is safe!");
  }
});
 
}

 

SweetAlert 1 Question/Response Example:

 

function testit() {

swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});

}

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

It does seem like an issue with SweetAlert 2 since they switched to promises. ServiceNow doesn't seem to support the => method.

I searched around and found user, geni, said promise was possible in SN: https://community.servicenow.com/community?id=community_question&sys_id=c83683a1db1cdbc01dcaf3231f9619e4&view_source=searchResult

I just see how his rough example would be adapted for SA2.