AgileApps Support Wiki Pre Release

Difference between revisions of "Web Service HttpConnection"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
 
Line 16: Line 16:
                 "record_id,invoice_lookup,part_number", "invoice_lookup not equal to '" + "BLANK" + "'");
                 "record_id,invoice_lookup,part_number", "invoice_lookup not equal to '" + "BLANK" + "'");
//Log to debug the records found using the above API
//Log to debug the records found using the above API
Functions.debug("searchRecords retrieved : " + searchPartsResult.getCode());
Logger.info("searchRecords retrieved : " + searchPartsResult.getCode(), "HttpConnection");
 
//If some records were found using the search criteria, the code will be greater than zero
//If some records were found using the search criteria, the code will be greater than zero
if(searchPartsResult.getCode() > 0)
if(searchPartsResult.getCode() > 0)
Line 24: Line 25:
   //getIterator() will give you the ParametersIterator using which you can iterate through the list
   //getIterator() will give you the ParametersIterator using which you can iterate through the list
   ParametersIterator partsIterator = searchPartsResult.getIterator();
   ParametersIterator partsIterator = searchPartsResult.getIterator();
 
   //Iterate through the list of records in the partsIterator by checking if there is more
   //Iterate through the list of records in the partsIterator by checking if there is more
   //ParametersIterator class has a 'hasNext()' method that will let you know if there are more records
   //ParametersIterator class has a 'hasNext()' method that will let you know if there are more records
Line 35: Line 37:
     String InvoiceRecordID = partsParameter.get("invoice_lookup");
     String InvoiceRecordID = partsParameter.get("invoice_lookup");
     //Tracking purpose log the record ID of the current object that the parts record is pointing to
     //Tracking purpose log the record ID of the current object that the parts record is pointing to
     Functions.debug("Record ID of invoice lookup from parts record : " + InvoiceRecordID);
     Logger.info("Record ID of invoice lookup from parts record : "  
                    + InvoiceRecordID, "HttpConnection");
     //Check if the record ID for the current record matches
     //Check if the record ID for the current record matches
     //the record ID that the parts record's lookup field is pointing to
     //the record ID that the parts record's lookup field is pointing to
Line 43: Line 46:
    //then the parts record is associated with the current record and will be displayed in the grid
    //then the parts record is associated with the current record and will be displayed in the grid
    //that is in the current record
    //that is in the current record
     Functions.debug("Parts Record found that depends on current record: " + currentRecordID);
     Logger.info("Parts Record found that depends on current record: "
                      + currentRecordID, "HttpConnection");
    //The part number of the parts object will be entered in the grid by user
    //The part number of the parts object will be entered in the grid by user
    //When the 'Invoice' record is saved we will be able to get the part_number of the 'Parts' record
    //When the 'Invoice' record is saved we will be able to get the part_number of the 'Parts' record
Line 49: Line 53:
         String partNumber = partsParameter.get("part_number");
         String partNumber = partsParameter.get("part_number");
    //Log the partNumber to track the program flow
    //Log the partNumber to track the program flow
         Functions.debug("Part number from parts record: " + partNumber);
         Logger.info("Part number from parts record: " + partNumber, "HttpConnection");
         //Create a new HttpConnect to the WebService at HelloWorldParts using the url that they have provided
         //Create a new HttpConnect to the WebService at HelloWorldParts using the url that they have provided
         HttpConnection con = new HttpConnection(
         HttpConnection con = new HttpConnection(
Line 68: Line 72:
         int code= con.execute();  
         int code= con.execute();  
    //Log the code returned by the execute method
    //Log the code returned by the execute method
         Functions.debug("Code from HttpConnection execute:"+ code);
         Logger.info("Code from HttpConnection execute:"+ code, "HttpConnection");
    //Use the getResponse() method of HttpConnection to get the response of the request that we have executed above
    //Use the getResponse() method of HttpConnection to get the response of the request that we have executed above
         response = con.getResponse();
         response = con.getResponse();
         //Log the response from the connection
         //Log the response from the connection
         Functions.debug("Result from connection :" + response);
         Logger.info("Result from connection :" + response, "HttpConnection");
    //Add the response to a multitext field
    //Add the response to a multitext field
         requestParams.add("wsresponse", response);
         requestParams.add("wsresponse", response);
Line 101: Line 105:
           //that you have added to the Parameters object - updateParams
           //that you have added to the Parameters object - updateParams
           Result updatePartsResult = Functions.updateRecord("1869974057orq1554390959", partsRecordId, updateParams );
           Result updatePartsResult = Functions.updateRecord("1869974057orq1554390959", partsRecordId, updateParams );
           Functions.debug("Result of Parts record update : " + updatePartsResult.getMessage());
           Logger.info("Result of Parts record update : "  
                          + updatePartsResult.getMessage(), "HttpConnection");
         } catch(XPathExpressionException ex) {
         } catch(XPathExpressionException ex) {
           Functions.debug(ex.getCause());
           Logger.info(ex.getCause(), "HttpConnection");
         }
         }
       }
       }
       else
       else
       {
       {
         Functions.debug("Record ID did not match the current record!");
         Logger.info("Record ID did not match the current record!", "HttpConnection");
       }
       }
   }
   }
Line 117: Line 122:
   //Add code here based on your business logic on what you would like to do  
   //Add code here based on your business logic on what you would like to do  
   //if searchRecord fails or does not retrieve any records
   //if searchRecord fails or does not retrieve any records
   Functions.debug("searchRecord returned with following result code: " + searchPartsResult.getCode());
   Logger.info("searchRecord returned with following result code: "
                + searchPartsResult.getCode(), "HttpConnection");
}
}
</pre>
</pre>

Latest revision as of 22:48, 10 September 2013

This code is a complex example of the use of Java code to take a series of actions, each based on the previous code section. For information on the use of this code sample, see: Integration to a Web Service Using HttpConnection.

//You have to import the following packages in order to be able to use the classes to parse the XML Response from HelloWorldParts
import java.io.StringReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

// response that we would retrieve after creating a HttpConnection and sending the request XML
String response = null;
// Following posts using HTTPConnection
//Get the record ID of the current record
String currentRecordID = requestParams.get("record_id");
//Use searchRecords JAVA API to search the 'Parts' object for records that have a a lookup to the current object
Result searchPartsResult = Functions.searchRecords("1869974057orq1554390959", 
                "record_id,invoice_lookup,part_number", "invoice_lookup not equal to '" + "BLANK" + "'");
//Log to debug the records found using the above API
Logger.info("searchRecords retrieved : " + searchPartsResult.getCode(), "HttpConnection");

//If some records were found using the search criteria, the code will be greater than zero
if(searchPartsResult.getCode() > 0)
{
   //From the searchRecords result - searchPartsResult - get the iterator to iterate through the records retrieved
   //Result class has a method - getIterator() 
   //getIterator() will give you the ParametersIterator using which you can iterate through the list
   ParametersIterator partsIterator = searchPartsResult.getIterator();
   
   //Iterate through the list of records in the partsIterator by checking if there is more
   //ParametersIterator class has a 'hasNext()' method that will let you know if there are more records
   while(partsIterator.hasNext())
   {
     //The next() method in ParametersIterator will get you the next Parameters object in the list
     Parameters partsParameter = partsIterator.next();
     //From the Parameters object get the record ID
     String partsRecordId = partsParameter.get("record_id");
     //Also from the Parameters object get the look up record id from the lookup field - invoice_lookup
     String InvoiceRecordID = partsParameter.get("invoice_lookup");
     //Tracking purpose log the record ID of the current object that the parts record is pointing to
     Logger.info("Record ID of invoice lookup from parts record : " 
                    + InvoiceRecordID, "HttpConnection");
     //Check if the record ID for the current record matches
     //the record ID that the parts record's lookup field is pointing to
     if(currentRecordID.equals(InvoiceRecordID))
     {
	    //If the current record ID matches the record ID of the lookup field in the parts record
	    //then the parts record is associated with the current record and will be displayed in the grid
	    //that is in the current record
    	Logger.info("Parts Record found that depends on current record: "
                       + currentRecordID, "HttpConnection");
	    //The part number of the parts object will be entered in the grid by user
	    //When the 'Invoice' record is saved we will be able to get the part_number of the 'Parts' record
	    //using the 'get' method of the Parameters object
        String partNumber = partsParameter.get("part_number");
	    //Log the partNumber to track the program flow
        Logger.info("Part number from parts record: " + partNumber, "HttpConnection");
        //Create a new HttpConnect to the WebService at HelloWorldParts using the url that they have provided
        HttpConnection con = new HttpConnection(
                     CONSTANTS.HTTP.METHOD.POST, "http://wstest.HelloWorldParts.com:xxxx/ws/GetPartInfo");
	    //Once the connection is established add the request to the connection so the request can be executed
	    //Use HttpConnection's addParameter method to send the request to the web service, 
            //the xml should be formatted to match the requirements of the web service
        con.addParameter("Request",
          "<Request>
            <auth_params>
             <username>USER</username>
             <password>HelloWorldParts</password>
            </auth_params>
            <query_params>
             <part_number>M108-43H-30</part_number>
            </query_params>
           </Request>");
        int code= con.execute(); 
	     //Log the code returned by the execute method
        Logger.info("Code from HttpConnection execute:"+ code, "HttpConnection");
	    //Use the getResponse() method of HttpConnection to get the response of the request that we have executed above
        response = con.getResponse();
        //Log the response from the connection
        Logger.info("Result from connection :" + response, "HttpConnection");
	    //Add the response to a multitext field
        requestParams.add("wsresponse", response);
         //Create an instance of Parameters object to add values to the parts record
         Parameters updateParams = Functions.getParametersInstance();
         //Using the XPathPactory class in the javax package create a new instance of XPathFactory
         //to create a new XPath object
         XPathFactory factory = XPathFactory.newInstance();
         XPath xPath = factory.newXPath();
         try {
	       //using the XPath object created evaluate the XML response from the HttpConnection
	       //The evaluate method of xPath will parse the response from HttpConnection and retrive the value for each element
	       //The add() method of Parameters class will add key-value pairs 
               //of all the fields that you would like to update in the parts record
           updateParams.add("parts_description", xPath.evaluate("/Response/part_description", 
                             new InputSource(new StringReader(response))));
           updateParams.add("level", xPath.evaluate("/Response/current_revision_level", 
                             new InputSource(new StringReader(response))));
           updateParams.add("current_eco_", 
             xPath.evaluate("/Response/current_eco_number", new InputSource(new StringReader(response))));
           updateParams.add("pending_eco", 
             xPath.evaluate("/Response/pending_revision_level", new InputSource(new StringReader(response)))); 
           //update the parameters object with key-value pairs of other fields to be updated in 'Parts' record      
           updateParams.add("description", response);
           //Make a call to the updateRecord API by passing
           //the object ID of the parts record, record ID of the parts record and the parameters object built above
           //The following call will update the parts record with the key-value pairs 
           //that you have added to the Parameters object - updateParams
           Result updatePartsResult = Functions.updateRecord("1869974057orq1554390959", partsRecordId, updateParams );
           Logger.info("Result of Parts record update : " 
                          + updatePartsResult.getMessage(), "HttpConnection");
        } catch(XPathExpressionException ex) {
           Logger.info(ex.getCause(), "HttpConnection");
        }
      }
      else
      {
        Logger.info("Record ID did not match the current record!", "HttpConnection");
      }
   }
}
//If the searchRecord fails or retrieves no records
else
{
  //Add code here based on your business logic on what you would like to do 
  //if searchRecord fails or does not retrieve any records
  Logger.info("searchRecord returned with following result code: "
                 + searchPartsResult.getCode(), "HttpConnection");
}