Is it possible to take screenshot automatic when "WebUI.verifyEqual()" is failed?

Is it possible to take screenshot automatic when “WebUI.verifyEqual()” is failed?

Maybe with if statements?
if (WebUI.verifyEqual()==false){
WebUI.takeScreenshot()
}
Didnt test it, but feel free to try