cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

How to create a user with default Name, email, telephone etc programatically?

EM_9923519
13-Aquamarine

How to create a user with default Name, email, telephone etc programatically?

To create a user programatically there is this script:

 

var userParams = {
   password: somePassword,
   name: someUserName,
  description:someDescription
};
Resources["EntityServices"].CreateUser(userParams);

 

 

How to include user fields like Name, email and telephone programmatically?

 

ACCEPTED SOLUTION

Accepted Solutions
yhan
17-Peridot
(To:EM_9923519)

Seems that parameters not defined in the system UserOptions datashape can not be set as an expected input parameter of Create User services (see article CS245936).

But you can use the below workaround instead:

var params = {
    infoTableName : "InfoTable",
    dataShapeName : "UserOptions"
};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(UserOptions)
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var row = new Object ();
row.userName="testUser2";
var password = Resources["EncryptionServices"].EncryptPropertyValue({data: "Login@thingworx"});
row.password = password; //password
result.AddRow(row);
Resources["EntityServices"].CreateUserWithOptions({
    userOptions: result /* INFOTABLE */
});
Users["testUser2"].emailAddress="abc123@gmail.com";

 That is after the creation of a user, we can set its user extension parameters directly in code.

View solution in original post

5 REPLIES 5
yhan
17-Peridot
(To:EM_9923519)

You can use CreateUserWithOptions service instead. Please refer to the community post and article CS329993

EM_9923519
13-Aquamarine
(To:yhan)

I am doing something wrong

I created a UserDataShape (copying some user fields from the composer) like this:

EM_9923519_0-1622229346301.png

And the script is:

 

 

var params = {
   infoTableName: "InfoTable",
   dataShapeName: "UserDataShape"
};

var infotable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
var newEntry = new Object();
newEntry.userName = 'usrtestscript4';
newEntry.Name = 'testscript4';
newEntry.Description = 'test 4 descr';
var password = Resources["EncryptionServices"].EncryptPropertyValue({data: "Login@12345"});
newEntry.Password = password;
newEntry.fullName = 'full name test 4 ';
newEntry.emailAddress= 'testscript4@site.com';
newEntry.mobilePhone = '12344567';
infotable.AddRow(newEntry);
// no return
Resources["EntityServices"].CreateUserWithOptions({
    userOptions: infotable
});

 

User is cretaed but emailAddress, mobilePhone  etc fields are blank

EM_9923519_1-1622229560733.png

 

What am I missing?

 

 

yhan
17-Peridot
(To:EM_9923519)

Seems that parameters not defined in the system UserOptions datashape can not be set as an expected input parameter of Create User services (see article CS245936).

But you can use the below workaround instead:

var params = {
    infoTableName : "InfoTable",
    dataShapeName : "UserOptions"
};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(UserOptions)
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var row = new Object ();
row.userName="testUser2";
var password = Resources["EncryptionServices"].EncryptPropertyValue({data: "Login@thingworx"});
row.password = password; //password
result.AddRow(row);
Resources["EntityServices"].CreateUserWithOptions({
    userOptions: result /* INFOTABLE */
});
Users["testUser2"].emailAddress="abc123@gmail.com";

 That is after the creation of a user, we can set its user extension parameters directly in code.

EM_9923519
13-Aquamarine
(To:yhan)

I ended using my code with assigning each property as you described

mgoel
17-Peridot
(To:EM_9923519)

@EM_9923519 

 

If you think previous response on this thread was helpful, please mark that accepted solution for the benefit of other community users.

 

Regards,

Mohit

Announcements


Top Tags