Splunk Dev

Java SearchManager condition execution of function

yogip86
Explorer

I am working with JavaSDK to execute custom python script.  In below code, i want to execute errorfunc (highlighted) in case "myResults.on("data", function ()" even is not triggered. How i can put non event condition. 

require([
'underscore',
'jquery',
'splunkjs/mvc',
"splunkjs/mvc/searchmanager",
'splunkjs/mvc/simplexml/ready!'
], function (_, $, mvc, SearchManager) {
console.log("sample.js has been loaded");
buttonClick()

function buttonClick(){
$("#run_search_btn").on("click",function(){
console.log("Clicked")
$('#run_search_btn').attr( "disabled", "disabled" );

script1 = "| script XPDM_Creation"

function success(val_cnt,val){
console.log("Search Result: Successful");
console.log("Result:",val_cnt);
console.log(val);
alert("Generated");
$("#run_search_btn").removeAttr( "disabled");
}
function err(){
console.log("Search Result: Failed");
alert("Generation Failed")
$("#run_search_btn").removeAttr( "disabled");
}

runScript1(script1,success,err)

})
}


function runScript1(script1, successfunc, errorfunc) {
var result = [];
var lookupSearch = new SearchManager({
earliest_time: "-24h@h",
latest_time: "now",
search: script1
});
var myResults = lookupSearch.data("results");

lookupSearch.on("search:done", function () {
console.log(myResults)
myResults.on("data", function () {
result_cnt = myResults.data().rows;
myResults_text = myResults.data().rows[0][0];
successfunc(result_cnt,myResults_text)
})
errorfunc()
});


lookupSearch.on('search:failed', function(properties) {
// Print the entire properties object
console.log("FAILED:", properties);
errorfunc()
});

lookupSearch.on('search:error', function(properties) {
// Print the entire properties object
console.log("FAILED:", properties);
errorfunc()
});

lookupSearch.on('search:progress', function(properties) {
// Print just the event count from the search job
console.log("IN PROGRESS.\nEvents so far:", properties.content.eventCount);

});


}
})

Labels (4)
0 Karma
Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...