Thursday, 23 October 2025

POST API Script for APEX ( Invoice Posting to FBR ) Updated with Error Codes

 if (navigator.onLine) {


    // const url = 'https://gw.fbr.gov.pk/di_data/v1/di/postinvoicedata_sb';

    const url = $v('P12_API');

    const token = $v('P12_TOKEN');

    const json_payload = $v('P12_JSON_PAYLOAD');


    const invoiceData = json_payload;

    var popup = apex.widget.waitPopup();


fetch(url, {

  method: 'POST',

  headers: {

    'Authorization': `Bearer ${token}`,

    'Content-Type': 'application/json'

  },

  body: invoiceData

})

.then(response => {

    if (response.status === 200) {

      return response.json(); 


    } else {

      

      console.error(`Request failed with status: ${response.status}`);

    popup.remove();

      throw new Error(`HTTP error! status: ${response.status}`);

    }

  })

.then(data => {    

   

     if(data.validationResponse.status==="Valid"){


        apex.item("P12_RESPONSE").setValue(data.validationResponse.invoiceStatuses[0].invoiceNo);

        popup.remove();

    } else

    {

        var detailCount = $v('P12_PCOUNT');

        if (detailCount === 0) {

             alert(data.validationResponse.error);

        } else if (detailCount > 0) {

            if(data.validationResponse.error === null || data.validationResponse.error === undefined || data.validationResponse.error === ""){

                for (let i=0; i < detailCount; i++ ){

                    if (data.validationResponse.invoiceStatuses[i].error !== ""){

                         alert(data.validationResponse.invoiceStatuses[i].error);

                }

            }

            }else{

                 alert(data.validationResponse.error);             

            }


            

        }

        

       

       

        popup.remove();

    }

    

   

    

})

.catch(error => {

 

alert("Error: " + error.message);

   popup.remove();

});




} else {

    apex.message.alert("No Internet connection. Please check your connection and try again.");

}


No comments:

Post a Comment

Check Internet Connectivity on Local System

if (!navigator.onLine) {     showOfflineAlert(); } else {     // Set a strict 3-second network timeout threshold     const controller = new ...