This topic contains 7 replies, has 0 voices, and was last updated by david.smith 7 years, 9 months ago.

  • Author
    Posts
  • #21920

    bradsimpson

    This is going to be a 2 part question. Overall goal is to write a script that will add the selected values of a multi-select field on a transaction record to an array, use If statements to look for a string in the array and when found call an email function to email a specific recipient. I'm having issues with the string lookup and calling the email function and continuing the iteration through the array.

    Part 1) How to lookup a specific string in an array? What I have below uses the concept of if (array == "String") { do something. The issue here is if there is more than one value in the array, the if statement is satisfied and the loop seems to exit.

    Code:
    /**
    * Global Variable Declaration
    */
    var recipient = "";
    var cc = [];
    var practices = [];
    practices.push(nlapiGetFieldTexts('custbody50'));

    function cs_RequestSOW(){
    console.log("Request SOW Button Pressed");
    nlapiSetFieldValue('custbody_sow_rqstd', 'T');
    nlapiSetFieldValue('custbody_sow_req_date', date);
    for (var i = 1; i <= practices.length; i++){
    alert(practices);
    if (practices == "Big Data"){
    alert("The Practice is " + practice);
    recipient = "bigdataemail@mycompanysdomain.com";
    console.log(recipient);
    cs_emailPractice();
    }else if (practices == 'Cloud & Automation' || practices == 'Dev Ops'){
    alert("The Practice is " + practices);
    recipient = "cloudautodevopsemail@mycompanysdomain.com";
    console.log(recipient);
    cs_emailPractice();
    }else if (practices == 'Compute'){
    alert("The Practice is " + practices);
    recipient = "computeemail@mycompanysdomain.com";
    console.log(recipient);
    cs_emailPractice();
    Part 2) How to call the cs_emailPractice() function when a string is found in the array but also continue the loop looking for other matches? The cs_emailPractice() function is located further down in the script record so I don't need to call a different script in my execution.
    This is a cached copy. Click here to see the original post.

  • #21921

    wwinters

    Hey Brad,

    It looks like you're looping through the array, but not actually testing against the value of the string in the array.

    Change your if and alert statements from just practices to practices[i], which will look at the string inside the array at that index.

    The loop will continue until i is not <= practices.length

    Hope that helps!

    Wes

  • #21922

    bradsimpson

    Hey wwinters,

    I marked up the alert as you mentioned to

    Code:
    alert(practices[i]);
    however now the Alert shows undefined instead of showing the Values that make up the array. When it was just

    Code:
    alert(practices);
    the popup alert would show the values correctly. I also did the same thing in my If statements and no change.

  • #21923

    david.smith

    Code:
    /** * Global Variable Declaration */
    var recipient = "";
    var cc = [];
    var practices = nlapiGetFieldTexts('custbody50') || [];

    function cs_emailPractice(recipient){
    console.log("sending email to",recipient);
    // code to send email….
    }

    function cs_RequestSOW(){
    console.log("Request SOW Button Pressed");
    nlapiSetFieldValue('custbody_sow_rqstd', 'T');
    nlapiSetFieldValue('custbody_sow_req_date', date);
    for (var i = 0; i < practices.length; i++){
    var practice = practices[i];
    console.log(practice);
    switch(practice){
    case "Big Data":
    cs_emailPractice("bigdataemail@mycompanysdomain.com");
    break;
    case 'Cloud & Automation'
    case 'Dev Ops'
    cs_emailPractice("cloudautodevopsemail@mycompanysdomain.com");
    break;
    case "Compute":
    cs_emailPractice("computeemail@mycompanysdomain.com");
    break;
    }
    }
    }

  • #21924

    bradsimpson

    david.smith That worked perfectly. Exactly how I envisioned it. Thank you so much!


    david.smith replied on 01/24/2017, 12:51 PM: Glad it worked. I didn’t test it.

  • #21925

    michoel

    I guess it's a matter of personal preference, but here is another way of structuring which I typically use:

    Code:
    /** * Global Variable Declaration */
    var recipient = "";
    var cc = [];
    var practices = nlapiGetFieldTexts('custbody50') || [];

    var PRACTICE_EMAILS = {
    'Big Data': 'bigdataemail@mycompanysdomain.com',
    'Cloud & Automation': 'cloudautodevopsemail@mycompanysdomain.com',
    'Dev Ops': 'cloudautodevopsemail@mycompanysdomain.com',
    'Compute': 'computeemail@mycompanysdomain.com'
    };

    function cs_emailPractice(recipient) {
    console.log("sending email to",recipient);
    // code to send email….
    }

    function cs_RequestSOW() {

    console.log("Request SOW Button Pressed");
    nlapiSetFieldValue('custbody_sow_rqstd', 'T');
    nlapiSetFieldValue('custbody_sow_req_date', date);

    for (var i = 0; i < practices.length; i++) {
    var practice = practices[i];
    console.log(practice);

    if(PRACTICE_EMAILS.hasOwnProperty(practice)) {
    cs_emailPractice(PRACTICE_EMAILS[practice]);
    }

    }
    }

  • #21926

    erictgrubaugh

    Yes! The `switch` statement can often be replaced with an Object mapping like this. I tend to favor this pattern myself.

    To go further down the rabbit hole, obscuring the loop with `filter` and `map`:

    Code:
    /** * Global Variable Declaration */
    var recipient = "";
    var cc = [];
    var practices = nlapiGetFieldTexts('custbody50') || [];
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹var PRACTICE_EMAILS = {
    'Big Data':'bigdataemail@mycompanysdomain.com',
    'Cloud & Automation':'cloudautodevopsemail@mycompanysdomain.com',
    'Dev Ops':'cloudautodevopsemail@mycompanysdomain.com',
    'Compute':'computeemail@mycompanysdomain.com'
    };

    function cs_emailPractice(practice) {
    var recipient = PRACTICE_EMAILS[practice];
    console.log("sending email to", recipient);
    // code to send email….
    }

    function cs_RequestSOW() {
    console.log("Request SOW Button Pressed");
    nlapiSetFieldValue('custbody_sow_rqstd', 'T');
    nlapiSetFieldValue('custbody_sow_req_date', date);

    practices.filter(cs_hasPractice).map(cs_emailPractice);
    }

    function cs_hasPractice(practice) {
    return PRACTICE_EMAILS.hasOwnProperty(practice);
    }

  • #21927

    david.smith

    Well alright then… I'll add one more thing to the thread. I would never use text to determine a value. Since this is coming from a multiselect list the text values can easily change. "Bid Data" might become "Big Bad Data" and never fire off that email.


    erictgrubaugh replied on 01/26/2017, 02:09 PM: Agreed; the text-matching should be replaced with matching the Internal IDs of the select options.

You must be logged in to reply to this topic.