This topic contains 7 replies, has 0 voices, and was last updated by david.smith 7 years, 9 months ago.
-
AuthorPosts
-
January 24, 2017 at 7:26 am #21920
bradsimpsonThis 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. -
January 24, 2017 at 9:54 am #21921
wwintersHey 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
-
January 24, 2017 at 11:15 am #21922
bradsimpsonHey 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 justCode:
alert(practices);
the popup alert would show the values correctly. I also did the same thing in my If statements and no change. -
January 24, 2017 at 11:42 am #21923
david.smithCode:
/** * 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;
}
}
} -
January 24, 2017 at 12:48 pm #21924
bradsimpsondavid.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.
-
January 24, 2017 at 11:43 pm #21925
michoelI 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]);
}}
} -
January 25, 2017 at 12:03 am #21926
erictgrubaughYes! 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);
} -
January 25, 2017 at 3:12 pm #21927
david.smithWell 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.
-
AuthorPosts
You must be logged in to reply to this topic.