> For the complete documentation index, see [llms.txt](https://docs.prebilt.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.prebilt.com/prebilt-for-im-wm-strm/enhancing-prebilt/connection-to-sap.md).

# Connection to SAP

This section will detail how the frontend connects to SAP through AJAX calls.

## Connection to SAP

The functions callAJAX and postAJAX are used in other AJAX call functions to find the method and to make the connection to SAP.&#x20;

CallAJAX is used to call the corresponding method in the backend, for example in each app there is a callAJAX to the function “GetSetup”.

<figure><img src="https://3149936122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2CkvzIq48sLzTY4bfcWl%2Fuploads%2FRtboFZQhWZIyJuHYaMgm%2Fimage.png?alt=media&amp;token=1c38d8f4-11f0-4735-88b4-7f013098ff99" alt=""><figcaption></figcaption></figure>

Another common AJAX method is SELECT, using the callAJAX function for SELECT will call the ajaxSelect function. This function in turn calls the postAJAX method for SELECT which triggers the SELECT method in the corresponding ABAP class.

When this method is completed any AJAX error in the call back will be handled by errorCB, e.g., a failed connection to SAP. However, if the method has completed it will trigger successCB and in turn selectSuccess. This will then handle any logic needed in the frontend depending on what response code was returned from SAP, which is stored in the SelectResp model. E.g., `gm("oSelectResp").getData().STATUS`.

## Standard Response Status Codes

After the connect to SAP has been made, the call will return a status code in the response. In PreBilt some of the status codes are reserved for standard responses:&#x20;

* &#x20;0 - This is the standard success code, some functions have more than one success status depending on what data has been sent to the backend and what functionality is required by the frontend.&#x20;
* &#x20;1 – This is the standard error status. This usually doesn’t have custom code to handle it in the frontend but will call the handleMessages function to display any messages in the responses MESSAGES section. &#x20;
* 5 – This is another success status code and means that a Selection Opt List has been sent from the backend, the code in this status will then call the function processSELOPT\_LIST with a callback function as a parameter to continue the processing.&#x20;
* 80 – This has been reserved for serial numbers and often includes logic to jump to the serial number app.
* 90 – This status code is reserved for any field validation issues, this status code usually has no custom code to handle it in the frontend but will call handleFieldValidationUpdates to display the field validation issues based on the data in the FIELD\_VALIDATION section of the code.&#x20;

## Response Models <a href="#toc214462483" id="toc214462483"></a>

In our ABAP methods we define a global structure called \<gs\_response> which allows us to pass fields to the frontend.

<figure><img src="https://3149936122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2CkvzIq48sLzTY4bfcWl%2Fuploads%2Fsca7MVR0WBTRo2JLQy4c%2Fimage.png?alt=media&amp;token=98be98cd-8878-4445-b7d3-2090c334dbac" alt=""><figcaption></figcaption></figure>

\<gs\_response> will always return:

* \<gs\_response>-status handles a status (as seen above)

E.g. the following returns an error status (1)

\<gs\_response>-status = /tctm/if\_wm\_app\_srv=>gc\_status\_field\_err.

* \<gs\_response>-messages handles any messages.

&#x20;Other fields we may want to pass to the frontend can be found in the corresponding TYPE (ty\_\*\*\*\_resp).

&#x20;With a successful AJAX call to SAP, we trigger the Success part of our function. This will have the naming convention “method\_name”Success, for example our Select method will call the selectSuccess function.

Within the above Success function, we call the following block of code to parse data received from the backend into our corresponding model.

```
// Parse Response Deep Structure
            sap.tctm.core.parseRespData({
                data: data,
                model: gm("oSelectResp")
            });
```

Models are defined at the top of the FE code:

<figure><img src="https://3149936122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2CkvzIq48sLzTY4bfcWl%2Fuploads%2Fzn3rx6SAmhPJNCN5uLPS%2Fimage.png?alt=media&amp;token=b5b7c94d-2eb1-448f-b266-79f43f245fd2" alt=""><figcaption></figcaption></figure>

We can then access any data passed from the backend with the defined models.

```
gm("oGetSetupResp").getData()
gm("oSelectResp").getData()
```

The two examples above are for the GET\_SETUP method and SELECT method in our ABAP class, these are two very common methods/responses, but all methods follow a similar naming convention. That being o”METHOD\_NAME”Resp” and .getData() call pulls out the oData information defined in our ABAP method.
