Forum Discussion

ShaneMiller1's avatar
ShaneMiller1
Qrew Cadet
2 years ago

javascript code page not working

Hello,
I was following a tutorial to capture a quickbase record as html, then save it as a pdf and place that pdf in an attachment field within the record itself: (https://community.quickbase.com/communities/community-home/digestviewer/viewthread?MessageKey=ac5d3752-a1e3-4b45-9c0b-62ff5ea239f3&CommunityKey=d860b0f8-6a48-487b-b346-44c47a19a804)

I followed the steps in the link above and all I am getting it to do is open the form as a popup but nothing is saved as a PDF in my attachment field.
I ran the Javascript code through an analyzer (codepen) and found this error (see attached):
'{a}' is only available in ES{b} (use 'esversion: {b}').

Anyone have any ideas on how to resolve this error?
Thank you in advance



------------------------------
Shane
------------------------------

9 Replies

  • Can you post the code from the code page?  (redact your private info)

    ------------------------------
    Jeff Peterson
    ------------------------------
    • ShaneMiller1's avatar
      ShaneMiller1
      Qrew Cadet
      Hey Jeff,
      Thank you for getting back to me. Yes, it's actually the same code that is on the linked discussion. It appears you actually got it all to work as well!
      Here is the Javascript:
      let urlParams = new URLSearchParams(window.location.search);
      let dbid = urlParams.get("myDbid");
      let recId = urlParams.get('recId');  
      let myForm = urlParams.get('myFormId');
      let usertoken = urlParams.get('usertoken');
      let apptoken = urlParams.get('apptoken');  
      let root = location.protocol + '//' + location.host;
      let myURL = `${root}/db/${dbid}?a=API_GetRecordAsHTML&rid=${recId}&dfid=${myForm}&apptoken=${apptoken}`;
      let myHostName = "EnterHostName.quickbase.com";
      
      const getURL = () => {
          $.post(myURL, {}, function (response) {
              $("#dbPagePayload").html(response);
          }).then(getDom);
      };
      
      
      
      const getDom = () => {
          let myDoc = document.getElementById("dbFormContainer");
          console.log(myDoc);
          var opt = {
              margin: 1,
              filename: 'myfile.pdf',
              image: { type: 'jpeg', quality: 0.98 },
              html2canvas: { scale: 2 },
              jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
          };
      
          html2pdf().from(myDoc).set(opt).outputPdf('dataurlstring').then(async res => {
            var base64result = res.split(',')[1];
                  
            var headers = {
                'QB-Realm-Hostname': myHostName,
                'User-Agent': 'File_Upload',
                'Authorization': `QB-USER-TOKEN ${usertoken}`,
                'Content-Type': 'application/json'
            }
      
      
            // 59 is my attachment field. Replace it with yours.
            body = { "to": dbid, "data": [{"3": {"value": recId}, "59": { "value": { "fileName": "FormPDF.pdf", "data": base64result }}  }], "fieldsToReturn": [ 3, 59 ] }
      
      
            fetch('https://api.quickbase.com/v1/records',
              {
                method: 'POST',
                headers: headers,
                body: JSON.stringify(body)
              })
            .then(async res => {
              if (res.ok) {
                return res.json().then(res => {
                  console.log(res)
                  window.close()
                });
              }
              return res.json().then(resBody => Promise.reject({status: res.status, ...resBody}));
            })
          .catch(err => console.log(err))
        });
      };
      getURL();​

      The only two things that were changed on this code was my host name for the variable $myHostName and the attachment field ID. Everything else was left the same



      ------------------------------
      Shane Miller
      ------------------------------
      • ShaneMiller1's avatar
        ShaneMiller1
        Qrew Cadet
        Would you like me to include the HTML code page, as well as the formula text field on the form?

        ------------------------------
        Shane Miller
        ------------------------------