How to Implement Business logic
Thanks for joining me for this video. For Servoy University, I’m Bob Cusick. In this video, we’re going to take a look at implementing business logic. Now business logic is basically anything that you want to happen based on the user’s input. Obviously, this is a huge topic, but we’re going to do something pretty simple today. What we’re going to do is we’re going to add previous and next buttons to this application. Let me show you how this application looks right now, and then we’ll go ahead and add the buttons. So go up to my smart client. As you can see, there’s no way to go to the next or previous record here. So we’re going to add some buttons to help us do that. All right, let’s pop back over to Servoy Developer. And now I’m going to add a couple of buttons. So let’s make a little button object here. Set the size to 4025. And we’re going to change the word button to a less than arrow. And we’re going to make the horizontal alignment be center. All right, so now we have a little button that has like a previous sign on it. We’ll take that button and I’m going to hold down my control key and my shift key and drag it. And now we can change this to a greater than symbol. So that will be our next arrow. Next we need to add our business logic to these buttons because in and of themselves, they don’t do anything at the moment. So I’m going to select both of them. Again, I’m going to click on one, hold down my control key, click on another one. Now what we want to capture is the click event. So in Servoy, that’s the on action event. So I’m going to double click this. And I’m going to say make a new method. And you can create it in three different scopes. We’ll talk about that in a different video. Right now I’m going to create it on the form. And we’re going to keep the on action. I’m going to say next previous. I’m going to make this a private. And I’m going to say OK and show. Now this will open up a JavaScript editor. And so this is where I actually write my business code. Servoy provides me a stub of a basic event. But we want to know whether they want to go to the previous record or the next record. So I’m going to add another parameter here. So I’m going to do at after I type the at symbol. This will give me a list of things. I’m going to say a param. Press my entry key. This is going to be a string. And we’ll call this direction. And then we’re going to have to add it right over here to our function. So now we can tell it whether it’s going to be next or previous. So what we want to do is we want to say if it’s previous, go to the previous record. If it’s next or the next record. So we’re going to add if statement. If s direction equals and notice that that will equal sign next. Then perform this action. Otherwise, we put an else in here. Perform this action. Now there’s a part of every form called a controller. And it’s the controller object that knows how to do things like go to a certain record or add records or delete records. So I’m going to type the word controller. And we want to set the selected index. I’m going to say set selected index. And we want it to go from where it is to the next record. So I’m going to get the current position. Why controller get selected index. All right. And we’re going to go ahead and we’re going to add one to that for the next record. Now I can just take this line. If it’s previous, I’m going to copy this and paste it in here. And we want to go to the previous record. So the current one minus one. All right. Let’s go ahead and save that. Go back to our customer form. Let’s put these buttons over here. Okay. So we have our buttons. Let’s go ahead and click these. Let’s align them so they’re a little bit clearer. Now, when I click on it, notice that there’s a carrot here. And here’s my S direction. So I can set it directly here. I’m going to say this is going to be the previous. And then I’m going to click on my next one. And I’m going to say this. I want you to pass in the value next. When they click it. Now we’ll go ahead and open up in the smarter web plans. All right. Now here I have my buttons. I’m going to the next and the previous. Wonderful. Everything’s working great both in the web and in the smart client. But I want to add some logic here and say if it’s on the first row, then I want to disable this button. So it grays out. Otherwise, I want it to be enabled so they can click it. Let’s go back and modify our method. If you want to act on an object at runtime, it has to have a physical name. So if I click on my previous arrow, you can see there’s no name. And so we need to add a name where we can’t programmatically control it at runtime. So I’m going to call this button previous. And I’ll call this one button next. Perfect. And now we’ll come over to our script. And we’ll add some code below here. And we’ll say if. Controller get selected index. And then index double equals one. Then we’re going to have our element. But in previous. And we’re going to set the enabled property to false. And we’re going to say else. And we’re going to copy this. And we’re going to set it to true. So let’s go ahead and save that. So that’ll work great as long as the button is clicked. But what happens when it first comes up? When you’re on the first record and your first launch, it’s not going to run this method. So what we need to do is tell our button here, number one, to turn off the enabled so we will be dimmed a grade out by default. So here we can see that it’s dimmed out or grade. And when I go to the next, it becomes enabled. Likewise, when I go to the first one and disables, it’s a bit harder to see because the widget looks differently than it does in the Smart Client. But it does exactly the same thing. Now you can have your own custom graphics on here. You could do anything that you wanted basically to show that there is a previous and next state. That’s a pretty basic example of business logic. But as you can see, it’s really easy to do. That’s about it for the short tutorial. I hope you enjoyed it. Thanks for watching.