javascript functions in processflow

Author
Messages
Maynard F.
Basic Member
Posts: 10
Basic Member
    Hello.  I would like to see some of the custom functions that PFI users have developed.  Specifially, I am wanting a function that will format a numeric field into an alpha numeric field complete with currency sign, commas and a decimal point.  Any other functions will be appreciated.  Please reply or post the code and tell me where to find it.  Thank you.
    Kyle Jorgensen
    Veteran Member
    Posts: 122
    Veteran Member
      
      //*****Function Definition -->String=formatDollarAmt(var1)                      //
      /************************************************************
              Function: FormatDollarAmt
      
              Purpose:  Format a dollar amount with dollar symbol, commas and decimal
                        Half cents are rounded up on .005 and down on .004
      ************************************************************/
      function formatDollarAmt(s) {
          
          if (isNaN(parseFloat(s))) {
              return "";
          }
          
          dblValue = parseFloat(s);
          blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
          dblValue = Math.floor(dblValue*100+0.50000000001);
          intCents = dblValue%100;
          strCents = intCents.toString();
          dblValue = Math.floor(dblValue/100).toString();
          if(intCents<10)
              strCents = "0" + strCents;
          for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
                  dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
                  dblValue.substring(dblValue.length-(4*i+3));
          RetVal = (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
          return RetVal;
          
      } 
      
      Kyle Jorgensen
      Veteran Member
      Posts: 122
      Veteran Member
        Heh, even though I used the 'code' tags it didn't like the single quote marks in the "RetVal" assignment line. Just replace each "'" with single quotes.
        Kyle Jorgensen
        Veteran Member
        Posts: 122
        Veteran Member
          Dang...it didn't like that either. Replace each "ampersand-pound-thirtynine-semicolon" with single quotes.
          Maynard F.
          Basic Member
          Posts: 10
          Basic Member
            Thanks Kyle.  Is this function one that you have used before, or did you code it because I was asking for one like this?
            Kyle Jorgensen
            Veteran Member
            Posts: 122
            Veteran Member
              It's one we use.
              Thibaud Lopez Schneider
              Advanced Member
              Posts: 30
              Advanced Member
                Have you tried toLocaleString? https://developer.mozilla...umber/toLocaleString

                (1122334455.67890).toLocaleString("en-US", {style:"currency", currency:"USD", maximumFractionDigits: 2}) ==> "$1,122,334,455.68"
                GeoffTSJY
                Basic Member
                Posts: 16
                Basic Member
                  This is more of a work in progress. But I use this solution to fill in the gaps with processflows email functionality:

                  First I build an XML object for an email structure. I use the same template. There is actually more to it than this. If anyone wants to use it I'll give the missing pieces. Most of the missing pieces you can probably figure out pretty easy.

                  This allows you to dynamically change the email that you are going to send. For example. You could have multiple emailBuilders. Call them something different though. Leave emailBuilder in the email nodes. Then, right before you get to the email node, you can decide what email you want to send and assign it to emailBuilder. So if I have an XML email structure for situation A and one for B, I just say emailBuilder = emailBuilderForA right before the email node. Then all of that data is what will be used in the email node. Your flows can look far more elegant with this and you can do much more powerful things and not risk emailing a real person during testing. You don't need to restructure your code when you deploy. Just switch the testing flag to false and it goes into production mode.

                   

                  Never re-write HTML emails over and over again. Write it one time and assign it to a function. Then have the sendEmailContent function build it into your styled and beautiful email template for you...


                  Remember, this is something I just whipped up. I'll be refining it over time.

                  
                          //I have a bool flag called isTesting. I set to true when I'm testing it and don't want to send real emails.
                          emailBuilder = ( //XML TYPE -- insert
                          
                  elements into the to, cc, bcc fields xx~! Please do not reply to this email. This is an automatic notification generated by ProcessFlow ); //all of these sendEmail... functions take the emailBuilder as the input sendEmailTo = ( //I have the same for CC and BCC - XML TYPES function(input) { var output = ""; var tmp; if(isTesting){ tmp = input.test.to.children(); } else { tmp = input.prod.to.children(); } for (var i in tmp) { output += tmp + ", " } return output; } ); sendEmailSubject = ( function(input) { var output = ""; if(isTesting){ output += input.test.subjectPrefix; } output += input.subject; return output; } ); sendEmailContent = ( function(input) { var output = ""; var tmp = input.body.children(); for (var i in tmp) { switch (tmp.name().toString()) { case "paragraph": output += Paragraph(tmp); break; case "button": output += Button(tmp.text, tmp.url); break; default: break; } } return output; } ); Paragraph = ( //XML TYPE - Function to format the HTML for a paragraph function (input) { var text = input; var output = ""; output += "

                  "; output += ""; output += "

                  "; output += inLabel; output += "
                  "; output += ""; output += "<\![endif]-->"; output += "<\!--[if !mso]>"; output += inLabel; output += ""; output+=""; output += "<\![endif]-->"; return output; } ); // DEMO TO USE THIS // - First add the addresses emailBuilder.prod.to.appendChild(
                  {approverList.primary.person.(id.ein ==approverList.current.ein).contact.email.toString()}
                  ); emailBuilder.test.to.appendChild(
                  {variableWithMyEmail}
                  ); // - next add the subject and body emailBuilder.subject = tmpCurrent.name.first + " Position Approval Action Needed"; emailBuilder.body.prependChild( Dear {tmpCurrent.name.first.text()}, please check your LTM inbasket for action. ); emailBuilder.body.appendChild( Some more body text. ); emailBuilder.body.appendChild( ); //Then in the email node Set the following: //TO: <!--sendEmailTo(emailBuilder)--> //CC: <!--sendEmailCC(emailBuilder)--> //BCC: <!--sendEmailBCC(emailBuilder)--> //FROM: <!--fromEmailVariable--> //SUBJECT: <!--sendEmailSubject(emailBuilder)--> //BODY: This is my default template. If you look closely, you'll see sendEmailContent(emailBuilder) in there. //There is another function in there I use for highlighted alerts at the bottom, if you want that, let me know. MailStyler - New <style type="text/css"> .mainTable, body { height: 100%!important; width: 100%!important; margin: 0; padding: 0 } a img, img { border: 0; outline: 0; text-decoration: none } .imageFix { display: block } table, td { border-collapse: collapse; mso-table-lspace: 0; mso-table-rspace: 0 } p { margin: 0; padding: 0; margin-bottom: 0 } .ReadMsgBody { width: 100% } .ExternalClass { width: 100% } .ExternalClass, .ExternalClass div, .ExternalClass font, .ExternalClass p, .ExternalClass span, .ExternalClass td { line-height: 100% } img { -ms-interpolation-mode: bicubic } a, blockquote, body, li, p, table, td { -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100% } .button { height: 100%; width: 30%; background-color: orange; margin: 0 auto; font-size: medium; font-weight: 700 } </style>
                   

                  xxx Department of xxx

                  xxx xxx Information Systems ()

                   

                  Dear ,

                  <!--sendEmailContent(emailBuilder)-->
                  <!--sendEmailAlerts(emailBuilder)-->

                  The following is for xxxx use only:

                  • Workunit: <!--oWorkUnit-->
                  • ProductLine: <!--appProdline-->
                  • Host: <!--host-->
                   
                  GeoffTSJY
                  Basic Member
                  Posts: 16
                  Basic Member

                    Here is an image of what you will see. Sorry I scribbled up the picture. Non of that info was probably super sensitive but I really just feel like being overly cautious and not giving out too much info. The yellow part at the bottom of the email is the missing piece I didn't include. I could, though if anyone wants. I think I made the font look nicer too since I last used this.

                     

                    I forgot to mention that the test flag also makes it so that the subject line has the option of adding a prefix to it. I add a prefix like "xx~!". That way I can setup and outlook filter to put my test emails in it's own box. Testing can clutter your inbox fast. You can also prepend it with a timestamp or whatever you want...

                     

                    So making email nodes for me is super fast, easy and reliable. Now, instead of formatting a custom email each time, I just think I want a paragraph of text, followed by another paragraph, flowed by a button and then a paragraph. And boom, my code beautifully creates it all for me.

                     

                    As you can see, all of my email nodes will look IDENTICAL to this. All of the content is determined and setup in my code. It is stored in XML in the start node. So it will persist if the process is suspended, like from a userAction node.

                     

                    I'd rather do this with objects and methods (I had it like that originally). But those don't persist through things like userActions. So I rebuilt using XML. I did it quickly. I'd rather have it be instantiable and with methods. I'll be researching if there is a good way to do that with XML...

                     

                     

                    Joan Herzfeldt
                    Veteran Member
                    Posts: 74
                    Veteran Member

                      Thank you Geoff - I need something just like this, but haven't started looking for it yet. I'll need to decipher (review) it later, to make sure I understand the code. If I have questions, may I email you?

                      Thanks -Joan

                      ---