quick question, i have my for loop set up correctl...
# suitescript
m
quick question, i have my for loop set up correctly, but it is only returning 1 value, why is that?
s
Probably need a code block to determine the problem.
m
Copy code
var approvalList = objRecord.getValue({fieldId:'custrecord7'});
			log.debug('afterSubmit','approvalList =='+approvalList);
			var approvalListId = [];
			for(var i = 0; i < approvalList.length; i++)
			{
				approvalListId.push(approvalList[i]);
				log.debug('afterSubmit','approvalListId =='+approvalListId);
e
What is
approvalList
?
m
the value of custrecord7
e
OK more specifically, what is
custrecord7
m
a multiselect field
with multi values
i am return 0000, 0000
approvalList ==722277,737549
approvalListId ==722277
u
i would
Copy code
log.debug('details',{
    value:approvalList, 
    type:typeof approvalList
});
and see what it returs
s
You are trying to print a string and array together, log them separately (dont string + array)
m
i just showed you
i want the for loop to pick up the 2 values
when the loop runs, it prints just the single value
everything is working correctly except this
the field is giving me the wrong value, i need the 2nd value
for loop
u
can you log
typeof approvalList
and tell us the value
s
It's already an array of strings, why you are looping on an array to make the exact same array.
e
Why is there a
for
loop in the first place? Assuming
getValue()
is returning an Array of two values, you're just making a copy of that Array
☝️ 1
and if
getValue()
isn't returning an Array, then a
for
loop won't work
At any rate, I'd log more data beforehand. Log the
typeof approvalList
as suggested previously, and log its
length
as well
s
gentle general advice - avoid
for
loops
e
And don't concatenate the Array with a string as suggested
Just
log.debug("approvalList", approvalList)
will be clearer
m
so, i need to read each value separately so I can compare it to the user that is logged in
If the user logged in = value in array, then i need to do something else
you mean read the Id direct from approvalList?
and i dont need the for loop?
e
Sure. My focus is on what the code is doing. In order for your
for
loop to work as intended,
getValue()
must return an Array of values. And if it's already returning an Array of values, there's no need for you to iterate and make a new Array.
If it's not already returning an Array of values, then you are going to need an extra step before the
for
loop to make sure you have an Array of values; generally that means it's returning a string that you need to
split()
. That's why we're advising you log the
typeof
j
approvalList
is probably a comma separated string, not an array. You probably want something like this:
Copy code
var approval_list = objRecord.getValue({fieldId:'custrecord7'}).split(',');

for(var i = 0; i < approval_list.length; i++) {

	// Do whatever you need to do with each of the elements.
	var approval_list_id = approval_list.length[i].trim();

	log.debug({title: 'afterSubmit()', details: 'Currently doing things with ' + approval_list_id});

}
also please rename ‘custrecord7’ to a meaningful id
m
i get an error on split
it is a comma separated string
my other array is working fine, just not this one
which is pushing the data from this array/loop into that array
then setting the multiselect field on record
however, this first loop is giving me the trouble by not getting all of the values
j
what was the error on split(‘,’) and what was the typeof for the approval_list ?
(without the split I mean)
m
error is cannot find function
e
Then it's not a string
A quick log of the
typeof
would tell you very clearly. Alternatively, use the
util
module's typechecking methods
m
i havent used util
if the values are an array, then my loop is correct
then why is it returning just the single value back? if there are 2
oh i know why now
okay, now this is weird, i get both of values that i need, and when i set the field, it says, you entered invalue field value for null/00051, 1 is the employee record
before, it was setting the correct single value, but because i have multi approvers, i had to do array and check values, then remove the one i didnt need, then set correct 2
now i have correct 2 values, but it is stating this null error
i got it resolved, thanks