eScripting Guide

34
Siebel eScript Guide

Transcript of eScripting Guide

Siebel eScript Guide

Chapter 1: Introduction Chapter 2: A Hello World Script Chapter 3: Event Based Scripting.

Chapter 1: IntroductionSiebel eScript is a scripting or programming language that application developers use to write simple scripts to extend Siebel applications. JavaScript, a popular scripting language used primarily on Web sites, is its core language. You should regard coding as a last resort. Siebel Tools provides many ways to configure your Siebel application without coding, and these methods should be exhausted before you attempt to write your own code, for the following reasons: Using Siebel Tools is easier than writing code. More important, your code may not survive an upgrade. Customizations created directly in Siebel Tools are upgraded automatically when you upgrade your Siebel application, but code is not touched, and it may need to be reviewed following an upgrade. Finally, declarative configuration through Siebel Tools results in better performance than implementing the same functionality through code. A hands on experience of any object oriented/object based language (like C++ or java script) is useful when it comes to learning Siebel eScript. Siebel eScript is implemented as part of Siebel applications and is interpreted by the Siebel Object Manager at run time.

Chapter 2: A Hello World Script!After that brief introduction, let us start with a very simple eScript. Keeping in synch with all the language guides, we will write a script that Pops up a Hello World message to the user when he/she clicks on a button labeled HelloButton. To do this, we have to Create a new Button Control on an applet labeled HelloButton and set its required properties. Add the button to the web-layout of the applet Enable the button (Let the user be able to click the button). Write a script code to display the Hello World message to the user. Compiling and testing on the web client. Okay, so to summarize, here is what we are going to do. We are going to put a HelloButton on the Account List Applet. When the user clicks this button, a Hello World message (alert) will be displayed to the user. Step 1: Create a new Button Control Open Siebel Tools and in the Object Explorer tab, click on Applet. Query for Account List Applet. Right Click on the applet and select Check-Out Object. Compile the applet after the checkout is done. Select the Account List Applet. Click on the (+) sign for the applet in the Object Explorer. Select Controls. All the vanilla controls for the applet are displayed on the right hand section. Right click the controls section and select New Record. Assign the following values to the properties for the newly created record. o Name: HelloButton o Caption String Override: HelloButton o HTML Type: MiniButton o Method Invoked: HelloButtonMethod Step Off from the record to save it.

Step 2: Adding the button to the applet web-layout Open Siebel Tools and in the Object Explorer tab, click on Applet. Query for Account List Applet. Right Click on the applet and select Edit Web Layout. In the web layout edit screen, select the Edit List mode. In the controls section select the control (created in the first step) HelloButton. Drag and Drop it to the applet layout. (Make sure that you drop it to a control placeholder named [X]. Dont put the button on a placeholder for some default button type like Cancel, Query etc). Save the applet web-layout. Right click on the applet web-layout and select Preview. The button should be visible in the applet preview. Close the web-layout screen. Step 3: Enable the button Open Siebel Tools and in the Object Explorer tab, click on Applet. Query for Account List Applet. Right Click on the applet and select Edit Server Scripts. The WebApplet script canvas opens up. It has two sections left one for selecting the method of the applet and right one to write the code in that method. Select the method WebApplet_PreCanInvokeMethod. Add the following piece of code to this method.function WebApplet_PreCanInvokeMethod (MethodName, &CanInvoke){ try{ if(MethodName == "HelloButtonMethod"){ CanInvoke = "TRUE"; return (CancelOperation); } return (ContinueOperation); } catch(e){ //Some error handling code } }

Save the script (Ctrl + S). Close the script window.

Step 4: Write the Hello World Script Open Siebel Tools and in the Object Explorer tab, click on Applet. Query for Account List Applet. Right Click on the applet and select Edit Server Scripts. The WebApplet script canvas opens up. Select the method WebApplet_PreInvokeMethod. Add the following piece of code to this method.function WebApplet_PreInvokeMethod (MethodName){ try{ if(MethodName == "HelloButtonMethod"){ TheApplication().RaiseErrorText("Hello World"); return (CancelOperation); } return (ContinueOperation); } catch(e){ //Some error handling code } }

Save the script.

Step 5: Compile and Run

Open Siebel Tools and in the Object Explorer tab, click on Applet. Query for Account List Applet. Right Click on the applet and select Compile Selected Objects. Select the correct application SRF and click on compile. After the compile is successful, open up the web client. Navigate to the Accounts Screen > Account List View. On the Account List Applet, click on the HelloButton. A popup message is displayed to the user which says Hello World!

Lets look at two steps in detail Step 3 and Step 4. In the 3rd step, we enabled the user to click on the HelloButton by writing a small snippet of code in the PreCanInvokeMethod. This method, as its name suggests, checks whether the user will be able to invoke any of the other applet methods. Our code, in the PreCanInvokeMethod, checks if the name of the method is HelloButtonMethod and if it is then the user can click it [Recall that the MethodInvoked property behind the button control that we created was also equal to HelloButtonMethod]. In Other words, this is Siebels way of enabling/disabling a button. The Fourth step checked whether the called method was named HelloButtonMethod. PreInvokeMethod is called, everytime a specialized method or a named method is called for an applet. The line which gives the actual alert is TheApplication().RaiseErrrorText(Hello World); TheApplication() is an object which has a set of all Application level functions, like the RaiseErrorText(), which we used in this case. It displays a popup to the user with the enclosed string message as the error text/message. CancelOperation and ContinueOperation

Chapter 3: Event Based ScriptingIn the last chapter, we put a button on the applet to provide a trigger for a code that we wrote. Siebel, by default, keeps a track of a lot of events happening at the Applet and BucComp layer, which can also be used to trigger a custom code. In this chapter, we will look at two such examples at the BusComp layer. Scenario 1: [BusComp_PreSetFieldValue] When the user tabs out from a certain field on an, applet, irrespective of what value the user sets, we want a default value entered in the field. Scenario 2: [BusComp_PreWriteRecord] If the Opportunity Sales Stage is Closed Lost or Closed Won then the Reason Won/Lost field should be populated before saving the record.

Scenario 1:Open the web client and navigate to the Service Request screen. The Service Requests list view is displayed. Service Request Detail Applet is the top applet. What we want to do is --Whenever the user steps off from the Summary field on the Service Request Detail Applet, irrespective of the value entered in the Summary field, we wish to put a default value in it. This is how you do it --1. Open Siebel Tools. In the Object explorer section, select Business Component. In the Business Components List, query for Service Request buscomp. 2. Right click on the Service Request buscomp and from the options displayed, select Edit Server Scripts. 3. The scripting canvas will open, with the event (method) names on the left side and their corresponding script on the right side. 4. Select BusComp_PreSetFieldValue on the left side. Enter the following piece of code in the script area.function BusComp_PreSetFieldValue (FieldName, FieldValue) { if(FieldName == "Abstract"){ this.SetFieldValue("Abstract", "Default"); return (CancelOperation); } return (ContinueOperation); }

5. Save the code and compile the Service Request BusComp. 6. Start the web-client, and navigate to the Service request screen. Create a new record. Enter the value in all the mandatory fields. After that click inside the Summary text area and enter any random text. After you are done, click on the drop-down for Type or Area field. 7. As soon as you step off from the summary field, its value should be updated with the default value that we have set in the script (Default). Before:

After:

Scenario 2:Open the web client and navigate to the Opportunities screen > Opportunity List view. Opportunity List Applet is the top applet. What we want to do is --For a record on the applet, when the user selects the value Closed Won or Closed Lost in the Sales Stage field, the script should verify if the value in the Reason Won/Lost field is populated. If its not, then the user has to enter a value in Reason Won/Lost field, before saving the record. This is how you do it 1. Open Siebel Tools. In the Object explorer section, select Business Component. In the Business Components List, query for Opportunity buscomp. 2. Right click on the Opportunity buscomp and from the options displayed, select Edit Server Scripts. 3. The scripting canvas will open, with the event (method) names on the left side and their corresponding script on the right side. 4. Select BusComp_PreWriteRecord on the left side. Enter the following piece of code in the script area.function BusComp_PreWriteRecord () { var SalesStage; var ReasonWonLost; SalesStage = this.GetFieldValue("Sales Stage"); if(SalesStage == "Closed - Won" || SalesStage == "Closed - Lost"){ ReasonWonLost = this.GetFieldValue("Reason Won Lost"); if(ReasonWonLost == "" || ReasonWonLost == null){

TheApplication().RaiseErrorText("Please populate the Reason Won/Lost field"); return (CancelOperation); } return (ContinueOperation); } }

5. Save the code and compile the Opportunity BusComp. 6. Start the web-client, and navigate to the Opportunities screen. Select an existing record. In the dropdown for Sales Stage select Closed Won. Dont populate a value in the Reason Won/Lost field. Try to save the record. 7. As soon as you step off from the record, an error message is displayed to the user asking him to populate the value in the Reason Won/Lost field before saving the record.

Summary: We used the two vanilla events from Siebel to trigger our code. There was no explicit triggering from our end (like in the second chapter when we put a button on the applet to run a code). Comment: Take a good look at the various methods available for the buscomps. Instead of PreSetFieldValue and PreWriteRecord use SetFieldValue and WriteRecord and see how it affects the application.

Chapter 4: Script to set the field having a Pick List

In the previous chapter we saw that how a field can be set using SetFieldValue method. However, it will not work in the same way for a field having a pick list. Setting a field having a pick list through script involves three major steps:1> Create the instance of the Pick List Business Component with GetPicklistBusComp () Method. 2> Navigate in the pick list business component to the record you want to pick. 3> Use Pick () Method to pick the value you want to set. The above would be explained with the help of an example:-

Scenario 1:Open the web client and navigate to the Opportunity screen. The Opportunity list view is displayed. Opportunity List Applet is the top applet. What we want to do is --Whenever the user selects a value on the Winning F.I (Competition) field on the Opportunity Detail Applet, we wish to set the Field Sales Stage with the value Closed - Won. This is how you do it --8. Open Siebel Tools. In the Object explorer section, select Business Component. In the Business Components List, query for Opportunity buscomp. 9. Right click on the Opportunity buscomp and from the options displayed, select Edit Server Scripts. 10. The scripting canvas will open, with the event (method) names on the left side and their corresponding script on the right side. 11. Select BusComp_SetFieldValue on the left side. Enter the following piece of code in the script area.function BusComp_SetFieldValue (FieldName) { if(FieldName == "Competition") { var oBC = this.GetPicklistBusComp("Sales Stage"); with (oBC) { ActivateField("Sales Cycle Stage"); ClearToQuery(); SetSearchSpec("Sales Cycle Stage","Closed - Won"); ExecuteQuery(ForwardOnly); if (FirstRecord())

Pick(); } } return(ContinueOperation); }

12. Save the code and compile the Opportunity Bus Comp. 13. Start the web-client, and navigate to the Opportunity screen. Select A record for which the Winning F.I field is not set. 14. Select a value in Winning F.I field. 15. As soon as you step off from the record, the value in the field Sales Stage should be set to Closed Won from the script. Before:

After:

Chapter 5: Script to add a new record to an Mvg Field:In this chapter we will see that how a new record can be added to a field having an Mvg. A new record can be added or associated to a Mvg field with the use of following methods:1> GetMvgBusComp: - This method returns the MVG business component associated with the business component field. 2> GetAssocBusComp: - This method returns the association business component for a business component. In the context of a many-to-many relationship, you can either add a new record, that is, associate a new child record, or insert a record, that is, create a new record in the child business component. To add a record, use GetAssocBusComp and the Associate method. To insert a record, use GetMvgBusComp and the NewRecord method. This would be explained with the help of an example:-

Scenario 1:Open the web client and navigate to the Opportunity screen. The Opportunity list view is displayed. Opportunity List Applet is the top applet. What we want to do is --Whenever the user creates a new correspondence record under the Account record, than the primary contact associated to the Account record should be added to the newly created correspondence record as its recipients. This is how you do it --1. Open Siebel Tools. In the Object explorer section, select Business Component. In the Business Components List, query for Correspondence buscomp. 2. Right click on the Correspondence buscomp and from the options displayed, select Edit Server Scripts. 3. The scripting canvas will open, with the event (method) names on the left side and their corresponding script on the right side. 4. Select BusComp_NewRecord on the left side. Enter the following piece of code in the script area.function BusComp_NewRecord () { var sViewName = TheApplication().ActiveViewName(); if (sViewName == "TD Account Correspondence View") { TheApplication().SetProfileAttr ("NewCorrespondence","Yes"); }

}

5. Select BusComp_WriteRecord on the left side. Enter the following piece of code in the script area.function BusComp_WriteRecord () { var sAccId = null; var sFirstName = null; var sPriConId = null; var parentBC = null; if (TheApplication().GetProfileAttr("NewCorrespondence") == "Yes") { if (TheApplication().ActiveViewName() == "TD Account Correspondence View") { TheApplication().SetProfileAttr("NewCorrespondence","No"); this.ActivateField("Auto CF Asset Id"); parentBC = this.ParentBusComp(); if(parentBC == null || (parentBC.Name() != "Account" ) { parentBC = null; TheApplication().RaiseErrorText("Parent BC is not found"); } sAccId = this.ParentBusComp().GetFieldValue("Id"); var boBusObj = TheApplication().GetBusObject("Account"); var boCorrBusObj = TheApplication().GetBusObject("Correspondence"); var bcCorr = boCorrBusObj.GetBusComp("Correspondence"); var bcAcc = boBusObj.GetBusComp("Account"); bcAcc.SetSearchSpec("Id" , sAccId); bcAcc.ExecuteQuery(ForwardOnly); if(bcAcc.FirstRecord()) { sPriConId = bcAcc.GetFieldValue("Primary Contact Id"); } Else { TheApplication().RaiseErrorText("Error in Primary Contact Id: " + sFirstName); } var bcRecipients = this.GetMVGBusComp("Recipient Last Name").GetAssocBusComp(); bcRecipients.ClearToQuery bcRecipients.SetSearchSpec("Id" , sPriConId); bcRecipients.ExecuteQuery(); if(bcRecipients.FirstRecord()) { bcRecipients.Associate(NewAfter); } Else { TheApplication().RaiseErrorText("There was an error trying to find the Primary Contact for this Customer. Please ensure that there is a Primary Contact.");

} }//if } }

6. Save the code and compile the Correspondence Bus Comp. 7. Start the web-client, and navigate to the Customers/Account screen. Select a record for which the Primary Contact is set. 8. Click on the hyperlink on Customer Name and then Navigate to Account Correspondence View. 9. Create a New Correspondence record below the Account record. 10. As soon as you step off from the record, the Primary Contact associated to the Account record would be added to the Correspondence record you created as its recipients from the script. Before:

After:-

Chapter 6: Business Service:Business service objects are objects that can be used to implement reusable business logic within the Object Manager. They include both built-in business services, which may be scripted but not modified, and user-defined objects. Using business services, you can configure stand-alone objects or modules with both properties and scripts. Below are some important Business Service methods:1> Service_InvokeMethod ():- The InvokeMethod () event is called after the InvokeMethod method is called on a business service. 2> Service_PreCanInvokeMethod ():- The PreCanInvokeMethod () event is called before the PreInvokeMethod, so the developer can determine whether or not the user has the authority to invoke the business service method. 3> Service_PreInvokeMethod ():- The PreInvokeMethod () event is called before a specialized method is invoked on the business service. Business Service can be invoked from any applet/BC code with the help of the method TheApplication ().GetService (). It would be explained with the help of the following scenario:-

Scenario 1:Open the web client and navigate to the Opportunity screen. The Opportunity list view is displayed. Opportunity List Applet is the top applet. What we want to do is --Whenever the user clicks on the newly created button GetResponsibility. A message should pop up showing the responsibility of the logged in user. This is how you do it --1. Open Siebel Tools. In the Object explorer section, select Business Service. Create a new record with the name Test BS and give other properties as shown in the fig:-

2. Click on Business Service method and create a new record by entering the values as shown in the screen below:-

3. Create the process properties to be used by clicking on Business Service Method Arg and then creating the new records as shown in the fig:-

4. Right click on the Business service record and from the options displayed, select Edit Server Scripts. 5. The scripting canvas will open, with the event (method) names on the left side and their corresponding script on the right side. 6. Select Service_PreInvokeMethod on the left side. Enter the following piece of code in the script area. function Service_PreInvokeMethod (MethodName, Inputs, Outputs) { try { switch(MethodName) { case "GetResponsibility": GetResponsibilityValue(Inputs, Outputs); break; default: return (ContinueOperation); } return (CancelOperation); } catch(e)

{ throw e; } finally { } } 7. On the left side click on the declarations tab and enter the following code into it function GetResponsibilityValue(Inputs, Outputs) { try{ var boBusObj; var bcEmp; var sId; var sResp; boBusObj = TheApplication().GetBusObject("Employee"); bcEmp = boBusObj.GetBusComp("Employee"); //sId= TheApplication().LoginId(); sId = Inputs.GetProperty("LoginId"); with(bcEmp){ SetViewMode (AllView); ActivateField ("Responsibility"); ClearToQuery(); SetSearchSpec ("Id", sId); ExecuteQuery(ForwardOnly); if(FirstRecord()){ sResp = GetFieldValue("Responsibility"); } } //return sResp; Outputs.SetProperty ("Resp", sResp); } catch(ex){ throw ex; } finally { bcEmp = null;

boBusObj= null; } } 8. In the Object explorer section, select Applets. Query for Opportunity List Applet. Add a new button control onto the Applet with the name as GetResponsibilty and Method invoked as GetResponsibilty following the steps mentioned in chapter 1. 9. Then Right click on the record and from the options displayed, select Edit Server Scripts. 10. Select WebApplet_PreCanInvokeMethod on the left side. Enter the following piece of code in the script area. function WebApplet_PreCanInvokeMethod (MethodName, &CanInvoke) { if (MethodName == "GetResponsibilty") { CanInvoke = "TRUE"; return (CancelOperation); } return (ContinueOperation); } 11. Select WebApplet_PreInvokeMethod on the left side. Enter the following piece of code in the script area. function WebApplet_PreInvokeMethod (MethodName) { if (MethodName == "GetResponsibilty") { var oBS; var inpPS; var outPS; var resp; var login; inpPS = TheApplication().NewPropertySet(); outPS = TheApplication().NewPropertySet(); login = TheApplication().LoginId(); inpPS.SetProperty("LoginId",login); oBS = TheApplication().GetService("TD Test BS"); oBS.InvokeMethod("GetResponsibility", inpPS, outPS); resp = outPS.GetProperty("Resp"); TheApplication().RaiseErrorText (resp); inpPS = null; outPS = null;

oBS = null; return (CancelOperation); } else { return (ContinueOperation); } } 11. Save the code and compile the Business service and the list Applet. 12. Start the web-client, and navigate to the Opportunity List Applet. 13. Press the button GetResponsibilty 14. Pressing the button a pop-up message would be displayed with the logged in users responsibiltiy. Before:

After: