Making a REST request using a workflow

To create a new workflow, open Workflow Editor and click on the New button under the Workflows palette tab and fill in the new workflow form as detailed here:

Fill in the form with following values:

  • * Name: Get current weather info
  • * Table: Booking Request
  • Condition: Assigned to | is not empty

Click on the Submit button to create a workflow named Get current weather info on the Booking Request table. The workflow canvas will look similar to the one shown in the following screenshot:

Now drag and drop the Run Script activity on the workflow canvas. The New Activity form dialog will open up in a modal window. Fill in the form with the following values:

  • Name: Call Yahoo Weather Service
  • Script: Populate the Script field with the following code:
var yql = 'select item from weather.forecast where woeid in (select woeid from geo.places(1) where text="'  
   + current.u_from_location.name  
   + '")'; 
try {  
 var r = new sn_ws.RESTMessageV2('x_8940_travel_book.Yahoo Weather', 'Default GET'); 
//replace 8940 with your company code  
r.setStringParameterNoEscape('yql', encodeURI(yql)); 
 
 var response = r.execute(); 
 var responseBody = response.getBody(); 
 var httpStatus = response.getStatusCode(); 
    gs.addInfoMessage(httpStatus); 
 if (httpStatus==200) { 
  gs.addInfoMessage(responseBody); 
  var weatherJSON = JSON.parse(responseBody); 
  var currTemp = weatherJSON.query.results.channel.item.condition.temp; 
  var currText = weatherJSON.query.results.channel.item.condition.text; 
workflow.scratchpad.currentCondition = "Current condition at "+ current.u_from_location.name + ": " + currTemp + " - " + currText; 
 gs.addInfoMessage("Current condition at "+ current.u_from_location.name + ": " + currTemp + " - " + currText); 
 } 
} 
catch(ex) { 
 var message = ex.getMessage(); 
} 

The preceding code makes use of the RESTMessageV2 API and creates an instance of the Default GET method defined in the Yahoo Weather method in the outbound REST Messages module.

The YQL we are using will help us fetch the current weather conditions from the Yahoo Weather service.

The name of the city is picked from the From (u_from_location) field in the Booking Request table. Furthermore, we are employing a workflow scratchpad to pass values set in one activity to subsequent activities.

The completed form will look similar to the one shown here:

Let us now add another activity to our workflow--Set Values. As shown here, we will use the Set Values activity to update the Work Notes Journal field:

Fill in the Set Values new activity form with the following values:

  • Name: Set worknotes
  • Set Values: Work notes | ${workflow.scratchpad.currentCondition}

The workflow.scratchpad.currentCondition variable is set in the workflow scope in the previous Run Script activity.

Change the existing transition on the Begin activity to point to the new Run Script activity and then to Set Values and then publish the workflow.

The completed workflow will look similar to the one shown here:

Let us now test our workflow by creating a new entry in the Booking Request table and then setting the Assigned to field. Subsequently, when we set the Assigned to field, an instance of the workflow, known as a workflow context, is triggered because the start condition of the workflow is met. If everything goes well, we can see the Work notes entry in the Activities (filtered) formatter, as shown in the following screenshot:

We can also monitor the execution of a workflow instance, known as a workflow context, graphically using the Active contexts or All contexts navigational module.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.77.98