Monday, 20 July 2026

Consuming a SOAP Web Service in PeopleCode

 Although REST APIs have become the preferred integration method in modern applications, many government agencies, banks, ERP systems, and enterprise applications still expose SOAP web services.


In this article, we'll build a complete SOAP client in PeopleCode that calls a web service, sends a National ID, receives address information, parses the XML response, and returns the formatted address.

The example used in this article calls the following SOAP operation:

GetIndividualWaselAddress

which returns information such as:

  • Building Number
  • Additional Number
  • Unit Number
  • District
  • Street
  • City
  • Full Name
  • Zip Code

Function GetAddress(&NID) Returns string
   try
      &reqStr = CreateSOAPDoc();
      &reqStr.AddEnvelope(%SOAP_Custom);
      &reqStr.AddBody();
      &reqStr.AddMethod("GetIndividualWaselAddress", 1);
      &reqStr.MethodNode.AddAttribute("xmlns", "http://tempuri.org/");
      &reqStr.AddParm("IdentifierType", "NationalID");
      &reqStr.AddParm("Identifier", &NID);
      
      &reqMsg = CreateMessage(Operation.GETINDIVIDUALWASELADDRESS);
      
      &inXml = CreateXmlDoc();
      &inXml = &reqStr.XmlDoc;
      &reqMsg.SetXmlDoc(&inXml);
      
      &respMsg = %IntBroker.SyncRequest(&reqMsg);
      &outXml = &respMsg.GetXmlDoc();
      
      /* Check for each tag's existence before accessing */
      &BuildingNumber = "";
      &nodeList = &outXml.DocumentElement.GetElementsByTagName("BuildingNumber");
      If &nodeList <> Null And
            &nodeList.len > 0 Then
         &BuildingNumber = &nodeList.Get(1).NodeValue;
      End-If;
      
      &AdditionalNumber = "";
      &nodeList = &outXml.DocumentElement.GetElementsByTagName("AdditionalNumber");
      If &nodeList <> Null And
            &nodeList.len > 0 Then
         &AdditionalNumber = &nodeList.Get(1).NodeValue;
      End-If;
      
      &ZipCode = "";
      &nodeList = &outXml.DocumentElement.GetElementsByTagName("ZipCode");
      If &nodeList <> Null And
            &nodeList.len > 0 Then
         &ZipCode = &nodeList.Get(1).NodeValue;
      End-If;
      
      &UnitNo = "";
      &nodeList = &outXml.DocumentElement.GetElementsByTagName("UnitNumber");
      If &nodeList <> Null And
            &nodeList.len > 0 Then
         &UnitNo = &nodeList.Get(1).NodeValue;
      End-If;
      
      &DistrictAreaArabic = "";
      &nodeList = &outXml.DocumentElement.GetElementsByTagName("DistrictAreaArabic");
      If &nodeList <> Null And
            &nodeList.len > 0 Then
         &DistrictAreaArabic = &nodeList.Get(1).NodeValue;
      End-If;
      
      &DistrictAreaEnglish = "";
      &nodeList = &outXml.DocumentElement.GetElementsByTagName("DistrictAreaEnglish");
      If &nodeList <> Null And
            &nodeList.len > 0 Then
         &DistrictAreaEnglish = &nodeList.Get(1).NodeValue;
      End-If;
      
      &StreetNameArabic = "";
      &nodeList = &outXml.DocumentElement.GetElementsByTagName("StreetNameArabic");
      If &nodeList <> Null And
            &nodeList.len > 0 Then
         &StreetNameArabic = &nodeList.Get(1).NodeValue;
      End-If;
      
      &StreetNameEnglish = "";
      &nodeList = &outXml.DocumentElement.GetElementsByTagName("StreetNameEnglish");
      If &nodeList <> Null And
            &nodeList.len > 0 Then
         &StreetNameEnglish = &nodeList.Get(1).NodeValue;
      End-If;
      
      &CityNameArabic = "";
      &nodeList = &outXml.DocumentElement.GetElementsByTagName("CityNameArabic");
      If &nodeList <> Null And
            &nodeList.len > 0 Then
         &CityNameArabic = &nodeList.Get(1).NodeValue;
      End-If;
      
      &CityNameEnglish = "";
      &nodeList = &outXml.DocumentElement.GetElementsByTagName("CityNameEnglish");
      If &nodeList <> Null And
            &nodeList.len > 0 Then
         &CityNameEnglish = &nodeList.Get(1).NodeValue;
      End-If;
      
      &fullname = "";
      &nodeList = &outXml.DocumentElement.GetElementsByTagName("FullName");
      If &nodeList <> Null And
            &nodeList.len > 0 Then
         &fullname = &nodeList.Get(1).NodeValue;
      End-If;
      
      /* Construct Address */
      &Returns = &BuildingNumber | ", " | &AdditionalNumber | ", " | &UnitNo | ", " | &DistrictAreaArabic | ", " | &DistrictAreaEnglish | ", " | &CityNameArabic | ", " | &CityNameEnglish | ", " | &StreetNameArabic | ", " | &StreetNameEnglish | ", " | &fullname;
      
      /* If all values are empty, return "No Data Found" 
      If All(&BuildingNumber = "", &AdditionalNumber = "", &UnitNo = "", &DistrictAreaArabic = "", 
             &DistrictAreaEnglish = "", &CityNameArabic = "", &CityNameEnglish = "", 
             &StreetNameArabic = "", &StreetNameEnglish = "", &fullname = "") Then
         &Returns = "No Data Found";
      End-If;
          */
   catch Exception &exp
      &Returns = "No Data Found" | &exp.ToString();
   end-try;
   
   Return &Returns;
End-Function;

/* Test MessageBox */
MessageBox(0, "", 0, 0, "" | GetAddress("2588888888"));

No comments:

Post a Comment