How to append new roles into readers field

I have a reader field with some entries (usernames and roles) I want to append the existing user name and roles with new username and roles.

Say for example [Admin], [HR] are the roles in the readers field. I want to append [Developer] as a role in the readers field.

More precisely I want to include the roles of the user in the document once he creates a new document.

Need to handle through script in initialize event.

Thanks in Advance

Ravi

Subject: How to append new roles into readers field

Look at the AppendToTextList method in the Designer help.

Subject: How to append new roles into readers field

Read the field values into an array, add “[Developer]” to the array then write the array back to the field, save the doc. Update the docs via an agent that operates on a selected group of docs or all docs in a view to do this en masse. Be sure to instantiate your array with a number larger than you will ever encounter in these docs.

Dim MyArray(10) as String

ctr = 0

Forall x in doc.ReadersFieldName

MyArray(ctr) = x

ctr = ctr + 1

End Forall

MyArray(ctr) = “[Developer]”

doc.ReadersFieldName = FullTrim(MyArray)

doc.Save(true,false)

Or something like that. Not sure why you would want to write the value on initialize since they won’t be able to see the doc unless they already have an accepted role but, you may have something else you are trying to do. Hopefully this helps.

Subject: RE: How to append new roles into readers field

If you are just adding a single fixed value, such as “[Developer]” in this example, you’re probably better off using the AppendToTextList method as it requires far less code.

Dim item As notesitem

Set item = doc.GetFirstItem(“ReadersFieldName”)

Call item.appendtotextlist(“[Developer]”)

Call doc.Save(True, False)

This will add Developer to the existing values in the Readers Field.

If you are needing to add in all of the user’s roles instead of fixed values, you would need to get hold of those in an array, then use ArrayAppend to merge the array with the existing field values into a new array. Next, set the readers field value to be that of the new array.

%REM

Get hold of user roles - there are a number of methods you could use for this.

Evaluating @UserRoles should work if this is a Notes Client Application, but I haven’t really tested it so use another

method if it doesn’t - i.e. the NotesACLEntry class Roles property or something like that

%END REM

Dim f$

Dim roles As Variant

f$ = “@UserRoles

roles = Evaluate(f$)

Dim returnArray

'Populate Return array with current readers field values and new roles array

returnArray = Arrayappend (doc.ReadersFieldName, roles)

doc.ReadersFieldName = Fulltrim(returnArray)

Call doc.save(True, False)

p.s. Code above is not tested, so may not work as it is, but should serve as a guideline.

Hope it helps.

Subject: RE: How to append new roles into readers field

Hi David,

I copied your code in the initialize event of the form. its throwing the following error message “Error : 183 On line : 14 and the error is Variant does not contain the object”. Please find below the code that I used.

‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’

Sub Initialize

%REM

Get hold of user roles - there are a number of methods you could use for this.

Evaluating @UserRoles should work if this is a Notes Client Application, but I haven’t really tested it so use another

method if it doesn’t - i.e. the NotesACLEntry class Roles property or something like that

%END REM

On Error Goto errhandler	

Dim f$

Dim roles As Variant

f$ = "@UserRoles"

roles = Evaluate(f$)

Dim returnArray

'Populate Return array with current readers field values and new roles array

returnArray = Arrayappend (doc.DocReader, roles)

doc.DocReader = Fulltrim(returnArray)

Call doc.save(True, False)

errhandler:

Messagebox  "Error : " + Cstr(Err) + " On line : " + Cstr(Erl) + " and the error is " & Error

End Sub

‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’

Subject: RE: How to append new roles into readers field

The code I provided was just the main chunk of what you need. You would also need to declare and set doc before the code would work.

Dim s As New notessession

Dim doc As notesdocument

Set doc = s.DocumentContext