ABSTRACT:
Making an Ajax Service Call from Coach to get result in BAW
DESCRIPTION:
If you wish to get the result of a service call in the UI dynamically based on the inputs in the coach, then how will you create a service to be accessible in the coach? Lets assume there is a calculate service that takes in a string (as 15+56) as input and returns the result of the operation. I want this input values 15 and 56 to be entered in the UI with an operation entered like + or - or * or /.
How will you call this service from your coach?
SOLUTION WITH EXAMPLE:
1. I have created a Client Side Human Service HS_Order with a Coach "Order"
2. The Coach has a few components like input text i.e "inp1", "inp2", "operation" and "result"; button i.e "calculate" and a service call i.e "Calculate Service call" and 4 data components for each of the input Texts for calculations.
3. Looking at the properties of the calculateService we see the called service is "calculateService"
4. "CalculateService" was a service created by Services -> + -> Service Flow ->Enter Service name -> Finish
5. Overview of the CalculateService:
log.info("init of calculateGss service...");
var a = tw.local.data.toString();
var str = a.split("-");
log.info("num1:"+str[0]);
if(a){
if(a.indexOf ("+", 0) >= 0){
var str = a.split("+");
var num1 = Number(str[0]);
var num2 = Number(str[1]);
tw.local.results = parseInt(num1)+parseInt(num2);
}else if(a.indexOf("-", 0) >= 0){
var str = a.split("-");
var num1 = Number(str[0]);
var num2 = Number(str[1]);
tw.local.results = parseInt(num1)-parseInt(num2);
}else if(a.indexOf("*", 0) >= 0){
var str = a.split("*");
var num1 = Number(str[0]);
var num2 = Number(str[1]);
tw.local.results = parseInt(num1)*parseInt(num2);
}else if(a.indexOf("/", 0) >= 0){
var str = a.split("/");
var num1 = Number(str[0]);
var num2 = Number(str[1]);
tw.local.results = parseInt(num1)/parseInt(num2);
}else{
var num1 = 0;
var num2 = 0;
tw.local.results = 0;
}
}
Now, my CalculateService is ready.
6. Next, in the "Order" coach, where I have the calculate button, I will add the below code in the On-Click event property:
var input = ${Data1}.getData()+${Data3}.getData()+${Data2}.getData();
console.log("input is:"+input);
${Service_Call2}.execute(input);
7. In the calculateService -> On Result, I will have below code:
console.log("calculate result:"+me.getResult());
${Data4}.setData(me.getResult());
8. In CalculateService -> On Error , I will have below code:
console.log("calculate error..");
9. Now, when I run the Client Side Human Service "HS_Order", I should be able to get the result on entering the numbers and operation:
10. However, I find that the result is still appearing as "0".
11. On checking the logs, I see that the On error block got executed.
12. The problem was in the Service Definition i.e. Overview of "CalculateService".
13.Here, I have considered the alternative to resolve this issue. Under "Ajax Section", select "Allow Calls from all users" instead of "Allow calls from trusted users" (to use this see at the bottom of this post).
14. Now, run the "HS_Order" and enter the inputs and operation. You will be able to see the result in the result text box.
This way, I was able to make use of the service call to make an AJAX call in the UI.
ALLOW CALLS FROM TRUSTED USERS:
1. First, I create a team with name "CalculateUser" with one user "swethaj"
2. In CSHR "HS_Order" -> Overview -> Usage, I select "Startable Service" in Use as and in Expose as Start I select the team "CalculateUser".
3. This means only the users defined under the team "CalculateService" will have access to the services in the CSHR.
4. Now, I login the processCenter with another user say "Admin" that has access to my process app.
5. On running the CSHR "HS_Order", entering the values and clicking on "Calculate" button, the result value doesn't change and the console shows "ConnectionError" as observed.
6. When logged in with user "swethaj", I will get the calculated result in the result field.
Thus, in this way Service Flow can be used for AJAX calls for trusted users only.
Making an Ajax Service Call from Coach to get result in BAW
DESCRIPTION:
If you wish to get the result of a service call in the UI dynamically based on the inputs in the coach, then how will you create a service to be accessible in the coach? Lets assume there is a calculate service that takes in a string (as 15+56) as input and returns the result of the operation. I want this input values 15 and 56 to be entered in the UI with an operation entered like + or - or * or /.
How will you call this service from your coach?
SOLUTION WITH EXAMPLE:
1. I have created a Client Side Human Service HS_Order with a Coach "Order"
2. The Coach has a few components like input text i.e "inp1", "inp2", "operation" and "result"; button i.e "calculate" and a service call i.e "Calculate Service call" and 4 data components for each of the input Texts for calculations.
3. Looking at the properties of the calculateService we see the called service is "calculateService"
4. "CalculateService" was a service created by Services -> + -> Service Flow ->Enter Service name -> Finish
5. Overview of the CalculateService:
- Diagram of the CalculateService: The service has 1 script activity.
- In the script of this activity, below is the javascript code that I have:
log.info("init of calculateGss service...");
var a = tw.local.data.toString();
var str = a.split("-");
log.info("num1:"+str[0]);
if(a){
if(a.indexOf ("+", 0) >= 0){
var str = a.split("+");
var num1 = Number(str[0]);
var num2 = Number(str[1]);
tw.local.results = parseInt(num1)+parseInt(num2);
}else if(a.indexOf("-", 0) >= 0){
var str = a.split("-");
var num1 = Number(str[0]);
var num2 = Number(str[1]);
tw.local.results = parseInt(num1)-parseInt(num2);
}else if(a.indexOf("*", 0) >= 0){
var str = a.split("*");
var num1 = Number(str[0]);
var num2 = Number(str[1]);
tw.local.results = parseInt(num1)*parseInt(num2);
}else if(a.indexOf("/", 0) >= 0){
var str = a.split("/");
var num1 = Number(str[0]);
var num2 = Number(str[1]);
tw.local.results = parseInt(num1)/parseInt(num2);
}else{
var num1 = 0;
var num2 = 0;
tw.local.results = 0;
}
}
- Variables of the CalculateService:
Now, my CalculateService is ready.
6. Next, in the "Order" coach, where I have the calculate button, I will add the below code in the On-Click event property:
var input = ${Data1}.getData()+${Data3}.getData()+${Data2}.getData();
console.log("input is:"+input);
${Service_Call2}.execute(input);
7. In the calculateService -> On Result, I will have below code:
console.log("calculate result:"+me.getResult());
${Data4}.setData(me.getResult());
8. In CalculateService -> On Error , I will have below code:
console.log("calculate error..");
9. Now, when I run the Client Side Human Service "HS_Order", I should be able to get the result on entering the numbers and operation:
10. However, I find that the result is still appearing as "0".
11. On checking the logs, I see that the On error block got executed.
12. The problem was in the Service Definition i.e. Overview of "CalculateService".
13.Here, I have considered the alternative to resolve this issue. Under "Ajax Section", select "Allow Calls from all users" instead of "Allow calls from trusted users" (to use this see at the bottom of this post).
14. Now, run the "HS_Order" and enter the inputs and operation. You will be able to see the result in the result text box.
This way, I was able to make use of the service call to make an AJAX call in the UI.
ALLOW CALLS FROM TRUSTED USERS:
1. First, I create a team with name "CalculateUser" with one user "swethaj"
2. In CSHR "HS_Order" -> Overview -> Usage, I select "Startable Service" in Use as and in Expose as Start I select the team "CalculateUser".
4. Now, I login the processCenter with another user say "Admin" that has access to my process app.
5. On running the CSHR "HS_Order", entering the values and clicking on "Calculate" button, the result value doesn't change and the console shows "ConnectionError" as observed.
6. When logged in with user "swethaj", I will get the calculated result in the result field.
Thus, in this way Service Flow can be used for AJAX calls for trusted users only.