Create custom script to extract specific data from Email

Create custom script to extract specific data from Email

Hello, can you help me set up my workflow?
I need some specific function to parse data from email ZOHO to Bigin, I need to extract specific data from the body and put into Bigin . Or maybe you can help me to convert this Javascript to Deluge?



  1.   if (trimmedInnerHTML.startsWith("Nome:")) {
        name = trimmedInnerHTML.substring(trimmedInnerHTML.indexOf("Nome:") + "Nome:".length).trim();
        const brIndex = name.indexOf("<br>");
        if (brIndex !== -1) {
          name = name.substring(0, brIndex);
        }
      }
      return name.trim();
    }

    function extractEmail(trimmedInnerHTML, htmlDoc) {
      let email = null;
      const emailStartIndex = trimmedInnerHTML.indexOf("Email:") + "Email:".length;
      let emailEndIndex = trimmedInnerHTML.indexOf("<br>", emailStartIndex);
      if (emailEndIndex === -1) {
        emailEndIndex = trimmedInnerHTML.length;
      }
      email = trimmedInnerHTML.substring(emailStartIndex, emailEndIndex).trim();
      const aElement = htmlDoc.createElement("div");
      aElement.innerHTML = email;
      email = aElement.getElementsByTagName("a")[0].innerHTML;
      return email.trim();
    }

    function extractPhone(trimmedInnerHTML) {
      let phone = null;
      phone = trimmedInnerHTML.substring(trimmedInnerHTML.indexOf("Telefone:") + "Telefone:".length).trim();
      const brIndex = phone.indexOf("<br>");
      if (brIndex !== -1) {
        phone = phone.substring(0, brIndex);
      }
      return phone.trim();
    }

    function removeBrTags(str) {
      return str.replace(/<br>|<\/br>|<\/b>|<b>/g, '').trim();
    }


    function extractDetails(trimmedInnerHTML) {
      let details = null;
      const bIndex = trimmedInnerHTML.indexOf("<b>");
      if (bIndex !== -1) {
        details = trimmedInnerHTML.substring(bIndex).trim();
      }
      return removeBrTags(details);
    }


    function extractLink(trimmedInnerHTML, pElement) {
      let link = null;
      const aElement = pElement.getElementsByTagName("a")[0];
      link = aElement.getAttribute("href");
      return link.trim();
    }


    function FromEmailToLead(html){

      const parser = new DOMParser();
      const htmlDoc = parser.parseFromString(html, 'text/html');
      const pElements = htmlDoc.getElementsByTagName("p");
      var name, email, phone, details, marketvalue, link;

      for (let i = 0; i < pElements.length; i++) {
        const trimmedInnerHTML = pElements[i].innerHTML.trim();
        if (trimmedInnerHTML.startsWith("Nome:")) {
          name = extractName(trimmedInnerHTML);
          email = extractEmail(trimmedInnerHTML, htmlDoc);
          phone = extractPhone(trimmedInnerHTML);
          details = extractDetails(trimmedInnerHTML);
     
          marketvalue = null;
          if (i + 1 < pElements.length) {
            marketvalue = removeBrTags(pElements[i + 1].innerHTML.trim());
          }
        }
        if (trimmedInnerHTML.startsWith("Link para a Análise Comparativa de Mercado:")){
          link = extractLink(trimmedInnerHTML, pElements[i]);
        }
      }
      return {
        name,
        email,
        phone,
        details,
        marketvalue,
        link,
      };
    }

    const htmlString = document.documentElement.outerHTML;
    console.log(FromEmailToLead(htmlString));
Thanks in adwance.

      • Sticky Posts

      • How to Add Users to your Organization in ZohoMail?

        A better clarity so you can create other users to start using Zoho Mail. You can directly Add Users from the Control Panel to your Organization. You can invite users with the existing email address. If the person (user) already uses ZohoCRM, then you can import users from Zoho CRM. You can also import them using a .csv file. (if you are planning to add them in Bulk)  In this topic, We will be discussing on how to Add and Invite users only.  The Import options are self explanatory. ____________________________________________________________________________________________________________