function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
msc7808msc7808 

Need assistance with WebService

Hi Guys, I need some assistance with basic WebService. I have put together a dummy service using the Orders table in the Northwind Database which returns Dataset of all the Orders for a particular Customer. Requires CustomerID as Parameter.

 

Here is the XML when the Service is invoked with "ALFKI" customer id.

 

<?xml version="1.0" encoding="utf-8" ?>

- <DataSet xmlns=http://172.16.1.254>
- <xs:schema id="NewDataSet" xmlns="" xmlns:xs="
http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
- <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
- <xs:complexType>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
- <xs:element name="Table">
- <xs:complexType>
- <xs:sequence>
  <xs:element name="OrderID" type="xs:int" minOccurs="0" />
  <xs:element name="CustomerID" type="xs:string" minOccurs="0" />
  <xs:element name="EmployeeID" type="xs:int" minOccurs="0" />
  <xs:element name="OrderDate" type="xs:dateTime" minOccurs="0" />
  <xs:element name="RequiredDate" type="xs:dateTime" minOccurs="0" />
  <xs:element name="ShippedDate" type="xs:dateTime" minOccurs="0" />
  <xs:element name="ShipVia" type="xs:int" minOccurs="0" />
  <xs:element name="Freight" type="xs:decimal" minOccurs="0" />
  <xs:element name="ShipName" type="xs:string" minOccurs="0" />
  <xs:element name="ShipAddress" type="xs:string" minOccurs="0" />
  <xs:element name="ShipCity" type="xs:string" minOccurs="0" />
  <xs:element name="ShipRegion" type="xs:string" minOccurs="0" />
  <xs:element name="ShipPostalCode" type="xs:string" minOccurs="0" />
  <xs:element name="ShipCountry" type="xs:string" minOccurs="0" />
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:choice>
  </xs:complexType>
  </xs:element>
  </xs:schema>
- <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
- <NewDataSet xmlns="">
- <Table diffgr:id="Table1" msdata:rowOrder="0">
  <OrderID>10643</OrderID>
  <CustomerID>ALFKI</CustomerID>
  <EmployeeID>6</EmployeeID>
  <OrderDate>1997-08-25T00:00:00-04:00</OrderDate>
  <RequiredDate>1997-09-22T00:00:00-04:00</RequiredDate>
  <ShippedDate>1997-09-02T00:00:00-04:00</ShippedDate>
  <ShipVia>1</ShipVia>
  <Freight>29.4600</Freight>
  <ShipName>Alfreds Futterkiste</ShipName>
  <ShipAddress>Obere Str. 57</ShipAddress>
  <ShipCity>Berlin</ShipCity>
  <ShipPostalCode>12209</ShipPostalCode>
  <ShipCountry>Germany</ShipCountry>
  </Table>
+ <Table diffgr:id="Table2" msdata:rowOrder="1">
  <OrderID>10692</OrderID>
  <CustomerID>ALFKI</CustomerID>
  <EmployeeID>4</EmployeeID>
  <OrderDate>1997-10-03T00:00:00-04:00</OrderDate>
  <RequiredDate>1997-10-31T00:00:00-04:00</RequiredDate>
  <ShippedDate>1997-10-13T00:00:00-04:00</ShippedDate>
  <ShipVia>2</ShipVia>
  <Freight>61.0200</Freight>
  <ShipName>Alfred's Futterkiste</ShipName>
  <ShipAddress>Obere Str. 57</ShipAddress>
  <ShipCity>Berlin</ShipCity>
  <ShipPostalCode>12209</ShipPostalCode>
  <ShipCountry>Germany</ShipCountry>
  </Table>
+ <Table diffgr:id="Table3" msdata:rowOrder="2">
  <OrderID>10702</OrderID>
  <CustomerID>ALFKI</CustomerID>
  <EmployeeID>4</EmployeeID>
  <OrderDate>1997-10-13T00:00:00-04:00</OrderDate>
  <RequiredDate>1997-11-24T00:00:00-05:00</RequiredDate>
  <ShippedDate>1997-10-21T00:00:00-04:00</ShippedDate>
  <ShipVia>1</ShipVia>
  <Freight>23.9400</Freight>
  <ShipName>Alfred's Futterkiste</ShipName>
  <ShipAddress>Obere Str. 57</ShipAddress>
  <ShipCity>Berlin</ShipCity>
  <ShipPostalCode>12209</ShipPostalCode>
  <ShipCountry>Germany</ShipCountry>
  </Table>
 
</NewDataSet>
</diffgr:diffgram>
</DataSet>

 


 NOTE:  Customer can have one or more orders.
 
I have the following code which grabs the XML and stores it in MAP. I am using Map for the first time so need some assistance on how to iterate the MAP and grab values and store it in Custom Object.
 
 
global class WSUpdates{
 
      WebService static Integer InsertOrders(String CustomerName,String CustomerID, String UserName,String RecordID )
    {
           Map<string,string> Orders= new Map<string,string>();
   
           HttpRequest req = new HttpRequest();
           req.setEndpoint('http://http://172.16.1.254/dummywebservice/OrderInfo.asmx');
           req.setMethod('POST');
           req.setHeader('Content-Type', 'text/xml; charset=utf-8');
           req.setHeader('SOAPAction', 'http://http://172.16.1.254/GetOrders');
           
           string b = '<?xml version="1.0" encoding="utf-8"?>';
           b += '<soap:Envelope xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
           b += '<soap:Header>';
           b += '<ServiceAuthHeader xmlns="
http://172.16.1.254">';
           b += '<Username>Username</Username>';
           b += '<Password>Password</Password>';
           b += '</ServiceAuthHeader>';
           b += '</soap:Header>';
           b += '<soap:Body>';
           b += '<GetOrders xmlns="
http://172.16.1.254">';
           b += '<ID>' + CustomerID + '</ID>';
           b += '</GetOrders>';
           b += '</soap:Body>';
           b += '</soap:Envelope>';
           
           req.setBody(b);
       
           Http http = new Http();
           HTTPResponse res = http.send(req);
           XmlStreamReader reader = new XmlStreamReader(res.getBody());
           CustomOrder__c[] orders = new CustomOrder__c[0];
         
        
          while(reader.hasNext())
            {
                if (reader.getEventType() == XmlTag.START_ELEMENT)
                {
                    string FieldName = reader.getLocalName();
                    reader.next();
                    if (reader.getEventType() == XmlTag.CHARACTERS)
                    {
                        Orders.put(FieldName, reader.getText());
                    }
                }
                reader.next();
            }    
 
//I dont know how to proceed from here. What I am trying to do here is contact the WebService, and use XMLStreamreader to read the XML Response, parse it and store it in the MAP object. Need to iterate the MAP, get the values and store it in Custom Object.
//THis is just sample code i have put together
 
      
           Orders.add(new CustomOrder__c(ItemName='Dummy',OrderDate__c='Date',RequiredDate__c='REQDDate')); 
           insert Orders
            //return Orders.size();
    }
}
Thanks for your help.
 
Manny

bbrantly1bbrantly1

public with sharing class TestForBlog { public string strThis{get;set;} public static testMethod void TestForBlogTest() { TestForBlog t = new TestForBlog(); t.RunMethodForTest(); } public void RunMethodForTest() { isApexTest = true; HttpRequest req = WSOrders('ALFKI'); HttpResponse res = new HttpResponse(); handleWSOrderResponse(res); } public static boolean isApexTest = false; public HttpRequest WSOrders(string CustomerID) { HttpRequest req = new HttpRequest(); req.setEndpoint('http://207.97.239.184/dummywebservice/service.asmx'); req.setMethod('POST'); req.setHeader('Content-Type', 'text/xml; charset=utf-8'); req.setHeader('SOAPAction', 'http://207.97.239.184/GetCustomerOrders'); string b = '<?xml version="1.0" encoding="utf-8"?>'; b += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'; b += '<soap:Body>'; b += '<GetCustomerOrders xmlns="http://207.97.239.184">'; b += '<CustomerID>' + CustomerID + '</CustomerID>'; b += '</GetCustomerOrders>'; b += '</soap:Body>'; b += '</soap:Envelope>'; req.setBody(b); return req; } public HttpResponse InvokeWSOrderResponse(Http h, HttpRequest req) { HttpResponse res = h.send(req); return res; } public void handleWSOrderResponse(HttpResponse res) { if (res.getBody() != null || isApexTest) { string xml; if (isApexTest) { xml = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetCustomerOrdersResponse xmlns="http://207.97.239.184"><GetCustomerOrdersResult><xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"><xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Table"><xs:complexType><xs:sequence><xs:element name="OrderID" type="xs:int" minOccurs="0" /><xs:element name="CustomerID" type="xs:string" minOccurs="0" /><xs:element name="EmployeeID" type="xs:int" minOccurs="0" /><xs:element name="OrderDate" type="xs:dateTime" minOccurs="0" /><xs:element name="RequiredDate" type="xs:dateTime" minOccurs="0" /><xs:element name="ShippedDate" type="xs:dateTime" minOccurs="0" /><xs:element name="ShipVia" type="xs:int" minOccurs="0" /><xs:element name="Freight" type="xs:decimal" minOccurs="0" /><xs:element name="ShipName" type="xs:string" minOccurs="0" /><xs:element name="ShipAddress" type="xs:string" minOccurs="0" /><xs:element name="ShipCity" type="xs:string" minOccurs="0" /><xs:element name="ShipRegion" type="xs:string" minOccurs="0" /><xs:element name="ShipPostalCode" type="xs:string" minOccurs="0" /><xs:element name="ShipCountry" type="xs:string" minOccurs="0" /></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><NewDataSet xmlns=""><Table diffgr:id="Table1" msdata:rowOrder="0"><OrderID>10643</OrderID><CustomerID>ALFKI</CustomerID><EmployeeID>6</EmployeeID><OrderDate>1997-08-25T00:00:00-04:00</OrderDate><RequiredDate>1997-09-22T00:00:00-04:00</RequiredDate><ShippedDate>1997-09-02T00:00:00-04:00</ShippedDate><ShipVia>1</ShipVia><Freight>29.4600</Freight><ShipName>Alfreds Futterkiste</ShipName><ShipAddress>Obere Str. 57</ShipAddress><ShipCity>Berlin</ShipCity><ShipPostalCode>12209</ShipPostalCode><ShipCountry>Germany</ShipCountry></Table></NewDataSet></diffgr:diffgram></GetCustomerOrdersResult></GetCustomerOrdersResponse></soap:Body></soap:Envelope>'; } else { xml = res.getBody(); } List<OrderInfo> Orders = new List<OrderInfo>(); strThis = ''; XmlStreamReader reader = new XmlStreamReader(xml); while(reader.hasNext()) { if (reader.getEventType() == XmlTag.START_ELEMENT) { if ('Table' == reader.getLocalName()) { Orders.add(GetOrderInfo(reader)); } } reader.next(); } //foreach statment strThis += '<table>'; for (OrderInfo o : Orders) { strThis += '<tr>'; strThis += '<td>' + o.OrderID + '</tr>'; strThis += '<td>' + o.CustomerID + '</tr>'; strThis += '<td>' + o.EmployeeID + '</tr>'; strThis += '<td>' + o.OrderDate + '</tr>'; strThis += '<td>' + o.RequiredDate + '</tr>'; strThis += '<td>' + o.ShippedDate + '</tr>'; strThis += '<td>' + o.ShipVia + '</tr>'; strThis += '<td>' + o.Freight + '</tr>'; strThis += '<td>' + o.ShipName + '</tr>'; strThis += '<td>' + o.ShipAddress + '</tr>'; strThis += '<td>' + o.ShipCity + '</tr>'; strThis += '<td>' + o.ShipPostalCode + '</tr>'; strThis += '<td>' + o.ShipCountry + '</tr>'; strThis += '</tr>'; } strThis += '</table>'; } } public OrderInfo GetOrderInfo(XmlStreamReader reader) { reader.next(); OrderInfo oi = new OrderInfo(); while(reader.hasNext()) { string FieldName = reader.getLocalName(); if ('Table' == FieldName) { break; } //reader.next(); reader.next(); if (reader.getEventType() == XmlTag.CHARACTERS) { //strThis += FieldName + ': ' + reader.getText()+ '\n'; if ( FieldName == 'OrderID') { oi.OrderID = reader.getText(); } else if (FieldName == 'CustomerID') { oi.CustomerID = reader.getText(); } else if (FieldName == 'EmployeeID') { oi.EmployeeID = reader.getText(); } else if (FieldName == 'OrderDate') { oi.OrderDate = reader.getText(); } else if (FieldName == 'RequiredDate') { oi.RequiredDate = reader.getText(); } else if (FieldName == 'ShippedDate') { oi.ShippedDate = reader.getText(); } else if (FieldName == 'ShipVia') { oi.ShipVia = reader.getText(); } else if (FieldName == 'Freight') { oi.Freight = reader.getText(); } else if (FieldName == 'ShipName') { oi.ShipName = reader.getText(); } else if (FieldName == 'ShipAddress') { oi.ShipAddress = reader.getText(); } else if (FieldName == 'ShipCity') { oi.ShipCity = reader.getText(); } else if (FieldName == 'ShipPostalCode') { oi.ShipPostalCode = reader.getText(); } else if (FieldName == 'ShipCountry') { oi.ShipCountry = reader.getText(); } } } return oi; } public class OrderInfo { public string OrderID; public string CustomerID; public string EmployeeID; public string OrderDate; public string RequiredDate; public string ShippedDate; public string ShipVia; public string Freight; public string ShipName; public string ShipAddress; public string ShipCity; public string ShipPostalCode; public string ShipCountry; public OrderInfo() { } } }