VMware Cloud Community
BillStreet00
Enthusiast
Enthusiast

Convert string array to object and add to an array of objects

I am reading in a string (comma separated user names)  and converting it to a string array. I am taking each item in my string array and searching AD for the user account.  I am storing the result into an AD:User variable.  I now need to add that variable to an Array/AD:User variable.

I am trying to use push but the error I get is that I cannot call method "push" of null.  When I add AD:User variable to a string array it works just fine. Google says the push method should work just fine. How do you add the AD:User variable to the Array/AD:User array?

Reply
0 Kudos
8 Replies
jarushepic
Enthusiast
Enthusiast

Are you initializing your variable that is an array of AD:User?  Just having it set as an array type doesn't actually assign it a value, you still need to do a var myArr = [];

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

Yes, it is done with push.

Could you show the code you use to create this array of users and to add the user object to it? It seems the variable you are trying to push to doesn't refer to your object array but is a null.

Reply
0 Kudos
BillStreet00
Enthusiast
Enthusiast

I have listUsers set as a string under Inputs.  I defined userArray as AD:User and goodUserList as Array AD:User in my attributes.

var userList = new Array();

userList = listUsers.split(',');

var goodUsers = new Array();

var badUsers = new Array();

var invalidUsers = new Array();

goodUserList = new Array();

for each(user in userList){

     var accountCheck = user.substring(0, 3);

     if(accountCheck == "prv"){

          userArray = ActiveDirectory.searchExactMatch("User",user);

          if(userArray == ""){

               badUsers.push(user);}

     else{

          goodUserList.push(userArray);}

     }

     else{

          invalidUsers.push(user);}

}

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

On which line exactly are you getting this 'cannot call method' error?

The code looks ok to me. All the places where you do call push() are using a properly constructed non-null arrays.

Reply
0 Kudos
BillStreet00
Enthusiast
Enthusiast

This line:  goodUserList.push(userArray)   The other push statements are just pushing the user name (string) into another string array so I can email them back to the requestor.

This is my error: I called System.log on userArray just before trying to push it to goodUserList and it came back with [AD_User]-[class ch.dunes.ad.object.User] -- VALUE : #_v2_#,#User#,#012827d2-77fb-435f-bf6e-8fff3fcf4e76#,#CN=xxxxxx,OU=xxxxxx,DC=xxxxxx,DC=xxxxxx# ​which is correct.

[2017-12-08 10:19:25.677] [E] Error in (Workflow:Step 2 / Scriptable task (item1)#19) TypeError: Cannot call method "push" of null

[2017-12-08 10:19:25.685] [E] Workflow execution stack:

***

item: 'Step 2/item1', state: 'failed', business state: 'null', exception: 'TypeError: Cannot call method "push" of null (Workflow:Step 2 / Scriptable task (item1)#19)'

workflow: 'Step 2' (9c731e39-daf2-4d23-b29e-34906a84a0e8)

|  'attribute': name=goodUserList type=Array/AD:User value=__NULL__

|  'attribute': name=badUserList type=string value=

|  'attribute': name=userArray type=AD:User value=__NULL__

|  'input': name=listUsers type=string value=prv_streebil

|  'no outputs'

*** End of execution stack.

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

This error could only happen if the line goodUserList = new Array(); is not there. I don't know, all this is very basic Javascript stuff and I can't imagine a case when it won't work.

BTW, the error is reported to happen on line 19, and your code has fewer lines. Is there some other code? If possible, could you export and attach the whole workflow? Also, which versions/build numbers of vRO and AD plug-ins do you use?

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

Oh, I think I see where the problem is.

ActiveDirectory.searchExactMatch() returns an array of objects; eg. you may have several users that match the user name you search for. In this case, you'll try to convert an array of users to a variable which is of type single user.

Reply
0 Kudos
jeet11
VMware Employee
VMware Employee

Hello All,

We are stuck in vRA machine request form where customer need to set a tab called Application Owner/VM owner  - where they need to add required user from AD Server .

, if anybody have simple workflow for same please share !!!

Reply
0 Kudos