Friday, March 20, 2015

Your eScript loop sucks!

Scripting in Siebel has a bad reputation.

I'm not that old with Siebel, but I believe this bad reputation may had started because Siebel started scripting with VBScript language. It is that easy to make terrible code with it.

If you don't agree, please leave some comments explaining why Microsoft moved from it favoring other programming languages.

Anyway, I'm always listening that scripting is bad and you should avoid it at all costs.

Yeah, right.

"Because learning how to program is not for everybody" should be the rest of the recommendation that nobody will give you.

That's not because programming with eScript requires an "Einstein". Because programming requires attention and dedication. If you want to do it right, you must practice it, study it. There is no way around, this is not the same thing than drawing squares and dragging arrows around.

And, to finish my rant, let's go for a example. You should have seen that a lot in your life developing with Siebel:

var OfferBO = TheApplication().GetBusObject("Offer");
var OfferBC = OfferBO.GetBusComp("Offer");

OfferBC.ActivateField("Priority");
OfferBC.SetSearchSpec("Id", TreatmentID);
OfferBC.SetViewMode(AllView);
OfferBC.ExecuteQuery();
var rec = OfferBC.FirstRecord();
while (rec) {
    //do something with the data
    rec = OfferBC.NextRecord();
}

OfferBC = null;
OfferBO = null;


It's the same old story to get data from the data layer. Let's ignore that this lame code is not using try-catch for now,  just please take a look at the while block.

You did? Right... now, if you're doing loops like that PLEASE STOP for God's sake!

eScript has the proper statement to do that kind of thing and I wonder why people keep writing the same damn thing:

if ( OfferBC.FirstRecord() ) {

    do {
   
        //do something with the data
   
    } while ( OfferBC.NextRecord() )

} else {

    TheApplication().RaiseErrorText("What the hell?!? Where is my data???");

}


Now you have it. Cleaner code. No extra variable to control execution flow. Properly validating that you have the data you're looking for or doing something about otherwise.

Let's tweak a little further:

    var OfferBO: BusObject = TheApplication().GetBusObject("Offer");
    var OfferBC: BusComp = OfferBO.GetBusComp("Offer");

    try {

        //Get the priority
        OfferBC.ActivateField("Priority");
        OfferBC.SetSearchSpec("Id", TreatmentID);
        OfferBC.SetViewMode(AllView);
        OfferBC.ExecuteQuery(ForwardOnly);

        var Priority:String = null;

        if (OfferBC.FirstRecord()) {

            do {


                //a silly example, but anyway...
                Priority = OfferBC.GetFieldValue("Priority");

            } while (OfferBC.NextRecord())

        } else {

            TheApplication().RaiseErrorText("No offer found for treatment id " + TreatmentID);

        }

    } catch (e) {

        throw (e);

    } finally {

        OfferBC = null;
        OfferBO = null;

    }


Let's review the changes:
  1. Using strongly typed variables: you should be using it nowadays to improve eScript performance.
  2. Hey, try-catch-finally is there for something! Use it!
  3. Always declare your cursor type when calling ExecuteQuery, and use a constant for that, not a number. "ForwardOnly" is the one you want in most cases.
  4. Always check if you're getting what you're expecting. If you're not, do something except hiding the dirt under the carpet. If you don't know what to do in such situation, add an exception and later you figure it out during your QA tests with some functional expert.
  5. Clean up your objects, you don't want a memory leak crashing the Siebel component.
That's it. And come on, it wasn't that difficult, it was?

Additionally, let me tell that despite Siebel Tools not helping you with that, your code does not have to look like indented by your cat sleeping on your keyboard. There are plenty tools to make your code look nice, I personally use http://jsbeautifier.org/ for that.

Cheers!

2 comments:

  1. Thank you, Alceu!
    That's everyone must have in your notes.

    ReplyDelete
  2. Thanks for all sharing informations Alceu. I just get to know about your blog and I found a lot of interesting stuff. Nice work.

    ReplyDelete