List Type Field

Swathi KS
Tera Contributor

Which is the best way to Use gs.getUserID  method for the LIST type fields in UI actions 

1 ACCEPTED SOLUTION

Mark Manders
Mega Patron

You need to add the users to the field, instead of setting the field to the user. 
You probably also needs some kind of validation that the user does not yet exist in the list field. The list field is just a list of sys_id's, so you can add the current user's sys_id to the field 

(function executeAction(current, previous /*optional*/) {
    var mySysId = gs.getUserID();
    var exmFieldValues = current.exm_field;

    if (!exmFieldValues || exmFieldValues.indexOf(mySysId) === -1) {
        if (exmFieldValues) {
            current.exm_field += ',' + mySysId;
        } else {
            current.exm_field = mySysId;
        }
        current.update();
        gs.info('Sys_id added to the field.');
    } else {
        gs.info('Sys_id already exists in the field.');
    }
})(current, previous);

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

4 REPLIES 4

karthiknagaramu
Kilo Sage

Hi,

 

Could you please elaborate your query?

 

Regards,

Karthik Nagaramu 

Mark Manders
Mega Patron

Please elaborate. What are you trying to do? What is the UI action you have and what does it need to do with the gs.getUserID method?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Swathi KS
Tera Contributor

we made changes to field type from reference to List type 

But in UI actions as part the condition we are using ex: current.exm_field== gs.getUserID we are using for reference once we changed to list what is the best way to use method gs.getUserID so it won't impact while we are adding more users to that field

Mark Manders
Mega Patron

You need to add the users to the field, instead of setting the field to the user. 
You probably also needs some kind of validation that the user does not yet exist in the list field. The list field is just a list of sys_id's, so you can add the current user's sys_id to the field 

(function executeAction(current, previous /*optional*/) {
    var mySysId = gs.getUserID();
    var exmFieldValues = current.exm_field;

    if (!exmFieldValues || exmFieldValues.indexOf(mySysId) === -1) {
        if (exmFieldValues) {
            current.exm_field += ',' + mySysId;
        } else {
            current.exm_field = mySysId;
        }
        current.update();
        gs.info('Sys_id added to the field.');
    } else {
        gs.info('Sys_id already exists in the field.');
    }
})(current, previous);

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark