@Eduard Lebedyuk Looked at the class this classEnsLib.RecordMap.Service.BatchFileService and the EnsLib.RecordMap.Service.BatchStandard class apparently these are fine  but for some reason the custom class acts as if it goes into a loop.I have put some traces in my code and tried to capture the status and change  it to fail if a certain validation fails but that makes the whole service to log an error after successfully delivering the first message .

here are the changes made thanks for your help

ClassMethod GetBatchHeader(pIOStream As %IO.DeviceStream, pTimeout As %Numeric = -1, Output pBatch As EnsLib.RecordMap.SimpleBatch, ByRef pLookAhead As %String) As %Status
{
    $$$TRACE("Begin")
 Try {
  #dim cpStatus as %Status=$$$OK
    Set tStatus = $$$OK
    Set pBatch = ""
    Set tTerm = ..GetHeaderTerm()
    //Set tFullHeader = 63 _ tTerm
    Set tHeaderLen =63+$length(tTerm)
    set stage=""
    If tHeaderLen
       {
       Set tFound = 0
       $$$TRACE("0")
       Set tLeadingJunk = ""
       Set pLookAhead = $get(pLookAhead)
       Set tTimeout = pTimeout
       Set tEndTime = $zhorolog + pTimeout
      // While ('tFound) && ('pIOStream.AtEnd)
        //  {
          Set tReadLen = tHeaderLen - $length(pLookAhead)
          If tReadLen > 0
             {
             Set tData = pLookAhead _ pIOStream.Read(tReadLen, .tTimeout, .tStatus)
             $$$TRACE("1")
             If $$$ISERR(tStatus) Quit
             If tTimeout
                {
                Set tStatus = $$$ERROR($$$EnsErrTCPReadTimeoutExpired, pTimeout, tReadLen)
                Quit
                }
                  Set pLookAhead = ""
             }
             Else
             {
             $$$TRACE("Else treadlen less than 0-[2]")
             Set tData = $extract(pLookAhead, 1, tHeaderLen)
             Set pLookAhead = $extract(pLookAhead, tHeaderLen + 1, *)
             }
             If ($extract(tData,1,3) = "001" )
                 {
                Set pBatch = ..%New()
                set pBatch.BatchHeader = tData
                Set tFound = 1
                $$$TRACE("Foundlee[3]"_tFound)
                Quit
                 }
                 Else
                 {
                $$$TRACE("else $extract(tData1,3)Failed[3]")
                Set pLookAhead = pLookAhead _ tData
                 #; Check if we should start discarding leading data
                If ($length(pLookAhead) >= tHeaderLen)
                   {
                   If ($length(tLeadingJunk) < 400)
                      {
                     Set tLeadingJunk = tLeadingJunk _ $extract(pLookAhead,1)
                      }
                      Set pLookAhead = $extract(pLookAhead,2,*)
                   }
                   set cpStatus=0
                   set stage="Extractfailed" //check here
                  // Quit
                  //Continue
     
                 }
                 If (pTimeout = -1)
                    {
                    Set tTimeout = -1
                    }
                    Else
                    {
                     $$$TRACE("time out ok [4]")
                    Set tCurrTime = $zhorolog
                    If (tCurrTime > tEndTime)
                       {
                      Set tStatus = $$$ERROR($$$EnsErrTCPReadTimeoutExpired, pTimeout, tReadLen)
                      Quit
                       }
                       Set tTimeout = tEndTime - tCurrTime
                   }
                   If $$$ISERR(tStatus) Quit
         // } //while
         
          $$$TRACE("while end"_tFound)
         
          If $$$ISERR(tStatus) Quit
          #; Clear the lookahead buffer if we didn't find the batch header
          $$$TRACE("###Status###[5]"_tStatus)
          If (('tFound) && ($length(tLeadingJunk) < 400)&&('pLookAhead="")&&($$$ISERR(cpStatus)))
             {
             Set tLeadingJunk = tLeadingJunk _ $get(pLookAhead)
             Set pLookAhead = ""
             $$$TRACE("###Not Found###[6]"_tFound)
             }
            If (tLeadingJunk '= "") && ('..#IgnoreLeadingData)
               {
               #; Use JS escaping to handle control characters
               Set tLoggedJunk = $zconvert($extract(tLeadingJunk,1,400),"O","JS") _ $select($length(tLeadingJunk) > 400: "...", 1: "")
               $$$LOGWARNING($$$FormatText($$$Text("[]Discarding unexpected leading data: '%1'","Ensemble"),tLoggedJunk))
               }
               If (('tFound)&&($$$ISERR(cpStatus)))
                  {
                 Set pBatch = ""
                 $$$TRACE("Not Found[8]")
                 //Set tStatus = $$$ERROR($$$EnsRecordMapErrBatchHeaderNotFound,$classname($this))
                 if (stage="Extractfailed" &&($$$ISERR(cpStatus)))
                          {
                          
                        
                         set tStatus=cpStatus
                        
                         $$$LOGWARNING($classname($this)_":: Empty Batch Discarded")
                        
                          set tStatus=$$$OK
                          }
                          else
                          {
                            // Set tStatus = $$$EnsSystemError
                            Set tStatus = $$$ERROR($$$EnsRecordMapErrBatchHeaderNotFound,$classname($this))
                             }
                
                 Quit
                  }
     }
     Else
     {
   Set pBatch = ..%New()
   Set pLookAhead = $get(pLookAhead)
   Quit
     }
     }
 Catch ex
    {
      $$$TRACE("in catch]")
    Set tStatus = $$$EnsSystemError
      quit
      }
      
 Quit tStatus
}

Since this is made up of three classes how do I control the status to send the call with within the block have a look at where  I have newStatus and I would like to quit and pass new status

The concept is about relationships the batch needs to be instantiated outside the loop and the record created and inserted in the loop. The batch has many records so every time we loop we add to records and then save records to a batch which was created only once

Method OnRequest(pRequest As DQToolsSet.PrimaryMessageREQ, Output pResponse As HSCIC.DBS.RequestRBAT) As %Status
{
 #dim status as %Status=$$$OK
    #dim resultSet as %SQL.StatementResult
   
   set pResponse=##class(HSCIC.DBS.RequestRBAT).%New()
 set status=..Adapter.ExecuteQuery(.resultSet,..GetTheSubmissionData(pRequest.pMonth, pRequest.pApp, pRequest.pRef, pRequest.pInPat))

nbsp;if ($$$ISOK(status))
 {
  While (resultSet.%Next(.status) && $$$ISOK(status))
  {
  set ppResponse=##class(HSCIC.DBS.RequestRMAP.Record).%New()
     set ppResponse.RequestCode=resultSet.Get("recType")
        set ppResponse.PatientID=resultSet.Get("ClientID")
        set ppResponse.DOB=resultSet.Get("DOB")
        set ppResponse.Property4=resultSet.Get("u1")
        set ppResponse.Property5=resultSet.Get("u2")
        set ppResponse.NHSNumber=resultSet.Get("NNN")
        set ppResponse.Surname=resultSet.Get("Surname")
        set ppResponse.AltSurname=resultSet.Get("u3")
        set ppResponse.GivenName=resultSet.Get("GivenName1")
        set ppResponse.AltGivenName=resultSet.Get("u4")
        set ppResponse.SpineGender=resultSet.Get("Gender")
        set ppResponse.Address1=resultSet.Get("a1")
        set ppResponse.Address2=resultSet.Get("a2")
        set ppResponse.Address3=resultSet.Get("a3")
        set ppResponse.Address4=resultSet.Get("a4")
        set ppResponse.Address5=resultSet.Get("a5")
        set ppResponse.PostCode=resultSet.Get("Postcode")
        set ppResponse.AltAddress1=resultSet.Get("p1")
        set ppResponse.AltAddress2=resultSet.Get("p2")
        set ppResponse.AltAddress3=resultSet.Get("p3")
        set ppResponse.AltAddress4=resultSet.Get("p4")
        set ppResponse.AltAddress5=resultSet.Get("p5")
        set ppResponse.AltPostCode=resultSet.Get("p6")
        set ppResponse.GPCode=resultSet.Get("GPCode")
        set ppResponse.GPPracticeCode=resultSet.Get("PracticeCode")
        set ppResponse.NewField3=resultSet.Get("u5")
        set ppResponse.NewFiel23=resultSet.Get("u6")
  
   set status=ppResponse.%Save()
 
   if ($$$ISOK(status))
   {
    set status=pResponse.Records.InsertObject(pobject)
   
      }
  
  
  }
  }
set status=pResponse.Records.%Save()
  }
 

@Sean Connelly

Class TOpp Extends Ens.BusinessOperation
{
Parameter ADAPTER = "EnsLib.SQL.OutboundAdapter";
Property Adapter As EnsLib.SQL.OutboundAdapter;
Parameter INVOCATION = "Queue";
Method CheckRequests(pRequest As Ens.Request, Output pResponse As Ens.Response) As %Status
{
 set query= ";with apt as"
 _"("
 _" Select ac.ClientID from AmsAppointmentContact ac join AmsAppointment a on a.SequenceID = ac.SequenceID where"
 _"  a.AppointmentDate > CAST(DATEADD(M,-6,GETDATE()) AS DATE) and a.CancellationDateTime is null "
 _" ),"
 _" ref as"
 _"("
 _" select ClientID"
 _"  from"
 _" ( select ROW_NUMBER() OVER(PARTITION BY ClientID ORDER BY ReferralNumber) rn, * from AmsReferral"
 _"   where DischargeDateTime is null) a"
 _"  where rn = 1 "
 _" ),"
 _"  inp as"
 _" ("
 _" select ClientID"
 _" from"
 _"("
 _" select ROW_NUMBER() OVER (PARTITION BY ClientID ORDER BY EventNumber) as rn,* from ImsEvent"
 _" where DischargeDate is null) a "
 _"  where rn = 1"
 _" ),"
 _" cohort as "
 _" (select ClientID from "
 _" ( "
 _" select row_number() OVER(PARTITION BY ClientID ORDER BY ClientID) as rn,ClientID from "
 _" (select * from apt"
 _" union all "
 _"  select * from ref "
 _"  union all "
 _"  select * from inp) a) b where rn=1 "
 _" ) "
 _" select '10' as recType, c.ClientID, CONVERT(VARCHAR(8), c.DateOfBirth, 112) as DOB, '' as u1, '' as u2, ISNULL(c.NNN, '') as NNN,"
 _" cn.Surname, '' as u3, cn.GivenName1, '' as u4, g.SpineCode as Gender,"
 _" '' as a1,'' as a2,'' as a3,'' as a4,'' as a5 ,ca.PostCode,'' as p1,'' as p2,'' as p3,'' as p4,'' as p5,'ZZ99' as p6, ch.GPCode, ch.PracticeCode,'' as u5,'' as u6 "
 _" from ClientIndex c left join ClientName cn on cn.ClientID = c.ClientID left join ClientAddress ca on ca.ClientID = c.ClientID left join ClientHealthCareProvider ch on c.ClientID = ch.ClientID "
 _" left join GenGender g on g.Code = c.Gender join cohort on cohort.ClientID = c.ClientID where "
  _" c.DateOfDeath is null and ca.ToDate is null and ca.AddressGroup = 1 and cn.AliasType = '1' and ch.ToDate is null and c.NonClient = 0"
 
 set file = ##class(%Stream.FileCharacter).%New()
 set fileName="C:\File\123.txt"
 set sc=""
  do file.LinkToFile(fileName)
  while 'file.AtEnd {
 
  set rs = ##class(%SQL.Statement).%ExecDirect(, query)
  set sc = rs.%DisplayFormatted(100,file)
}
 quit sc
 
}
XData MessageMap
{
<MapItems>
  <MapItem MessageType="Ens.Request">
    <Method>CheckRequests</Method>
  </MapItem>
</MapItems>
}
}

@Dmitry Maslennikov I have  done this for starters but got an error ^
<INVALID OREF>zTalkToTheWorld

here is my code

 

 

Method TalkToTheWorld()
{
 Set httprequest=##class(%Net.HttpRequest).%New()
 Set httprequest.Server="https://maps.googleapis.com/maps/api/distancematrix/json?origins=BRISTOL&destinations=LONDON&key=TZhydlRg6-dUm-EoIuees43456rtyu7 "
 Do httprequest.Get("/")
 Do httprequest.HttpResponse.OutputToDevice()
 Do httprequest.Get("/cache/")
 Do httprequest.HttpResponse.OutputToDevice()
}