cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

how to exit from script after hitting certain condition

Ritz
Active Contributor
0 Likes
1,967

Hello Expert,

I have a long script which execute multiple transactions in happy path, I am working on error handling and want to exit from script if it certain condition , generate message and go back to the initial page of transaction. I am able to use if condition to validate condition as well as able to generate message. But when user correct the input of initial screen and hit script execution button its starting from middle of the script not the initial line.

Could you please suggest a way to come out of script once script faces a error, some thing like refresh screen and start from the beginning once user hit execute.

Thanks

RD

Accepted Solutions (1)

Accepted Solutions (1)

Brian_Wills
Contributor

Hi Ritesh,

Without the details of what you are doing, it will be hard to give you a specific answer. I'm going to make the assumption you are in the same flavor and scripting from there (i.e. you are not switching to a new flavor during your script). Anytime I need to stop my script from running I use a "return true". This will immediately stop the current script from continuing or stop the current function from continuing. I would normally do validation in the first part of my scripting after user clicks a button such as save or next, etc. If validation fails then I would do a "return true".

Here is a snippet of code I use in one of my flavors.

//--------------------------------------------------------------------------------------------------------------
//Validation

// 4) Get Order and Operation numbers
	var z_wo = session.findById("wnd[0]/usr/subHEADER:SAPLCMFU:0201/ctxtCMFUD-AUFNR").text;
	var z_op = session.findById("wnd[0]/usr/subHEADER:SAPLCMFU:0201/ctxtCMFUD-VORNR").text;

//5) Check that a value is entered in the order field
	if(z_wo === '')
	{
		session.findById("wnd[0]/sbar").setMessage('Enter an Order', 'E');
		session.findById("wnd[0]/usr/subHEADER:SAPLCMFU:0201/ctxtCMFUD-AUFNR").setFocus();
		return true;
	}


// 6) Check that a value is entered in the operations field
	if(z_op === '')
	{
		session.findById("wnd[0]/sbar").setMessage('Enter an Operation', 'E');
		session.findById("wnd[0]/usr/subHEADER:SAPLCMFU:0201/ctxtCMFUD-VORNR").setFocus();
		return true;
	}


// 7) Press enter to trigger SAP validatation
	session.findById("wnd[0]").sendVKey(0);


// 8) Deal with any SAP messages - LOOKING FOR ERROR MESSAGE BY SAP
	if(session.findById("wnd[0]/sbar").messageType == 'E')
	{
		session.utils.log('Error message encountered - STOP THE SCRIPT FROM FURTHER PROCESSING');
		return true;
	}

Hope this helps.

Thanks,

Brian

Answers (3)

Answers (3)

Ritz
Active Contributor

bwills,

Sorry I was not able to explain my issue properly but you assumed it correctly and solved my problem.

A Big Thank You !!

Thanks

RD

Brian_Wills
Contributor

ritesh.dube,

You are welcome! Glad I was able to help.

Thanks,

Brian

cris_hansen
Product and Topic Expert
Product and Topic Expert

Hi RD,

I would use multiple functions, and depending on the value returned by every function, execute the next one, otherwise, go back to the beginning. I would use variables to control the flow (store the results from each function) and act upon their values.

Regards,

Cris

Ritz
Active Contributor

cristiano.hansen

Thanks, for each success result i do not have any issue. But when I come across a error i want to back to the inertial screen , and i am not sure how to do that .

go back to the beginning. I would use variables to control the flow (store the results from each function) and act upon their values.

could you add more on it or give a example.

Thanks

RD

cris_hansen
Product and Topic Expert
Product and Topic Expert

Hi RD,

Like I mentioned, if there was an error, it would be caught in the result of a function. Then a simple if, testing for the error, would cause the script to reset all variables and calling the original transaction again (using, for example, session.callTransaction()).

Regards,

Cris