Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Square Brackets Added to Array's First and Last Objects #260

Closed
tehfonz opened this issue Jan 14, 2024 · 4 comments · Fixed by #261
Closed

JSON Square Brackets Added to Array's First and Last Objects #260

tehfonz opened this issue Jan 14, 2024 · 4 comments · Fixed by #261
Assignees
Labels

Comments

@tehfonz
Copy link

tehfonz commented Jan 14, 2024

Environment

Operating System: Windows Server 2016
ServiceNow module version: 4.0.3
PowerShell version: 5.1

Steps to reproduce

$CIs = @('Server1', 'Server2')
$UpdateChangeRequestDetails = @{
	u_affected_ci_list = $CIs
}
Update-ServiceNowRecord -ID $CHG -InputData $UpdateChangeRequestDetails -Verbose

Expected behavior

Field in ServiceNow should just be populated with Server1 & Server2

Actual behavior

Field in ServiceNow is populated with [Server1 & Server2]

Screenshots

image
image

Not sending an array and instead casting a single object as a string removes the brackets

[string]$CIs = 'Server1'
$UpdateChangeRequestDetails = @{
	u_affected_ci_list = $CIs
}
Update-ServiceNowRecord -ID $CHG -InputData $UpdateChangeRequestDetails -Verbose

image

Sending a variable cast as a string with "array" objects separated by a comma adds them to the field separated by a line (i.e. unique entries) and no square brackets:

[string]$CIs = 'Server1, Server2'
$UpdateChangeRequestDetails = @{
	u_affected_ci_list = $CIs
}
Update-ServiceNowRecord -ID $CHG -InputData $UpdateChangeRequestDetails -Verbose

image

So if the variable is an array:

[array]$CIs = @('Server1', 'Server2')
[string]$CIsString = $CIs -join', '
$UpdateChangeRequestDetails = @{
	u_affected_ci_list = $CIsString
}
Update-ServiceNowRecord -ID $CHG -InputData $UpdateChangeRequestDetails -Verbose

Works, sending the data without brackets. Unsure what you can do with this info, I can build my script around it, not sure if this matters to anyone else or if I'm just using the function incorrectly.

Cheers
_Fonz

@gdbarron
Copy link
Collaborator

Hi @tehfonz, this was great info, thanks.

From my research, although SN says it wants json for the payload, that's not applicable to List based fields. Values for those fields are just comma separated and not arrays which add [ ] when converting to json. Converting to a string with [string]$CIs = 'Server1, Server2' has a value of Server1, Server2 which is what SN wants.

I'll give some thought to the best way to handle this and update you when I've come up with a solution.

@gdbarron gdbarron self-assigned this Jan 24, 2024
@gdbarron gdbarron added the bug label Jan 24, 2024
@gdbarron gdbarron linked a pull request Feb 17, 2024 that will close this issue
@gdbarron
Copy link
Collaborator

@tehfonz would you mind testing the fix?

@tehfonz
Copy link
Author

tehfonz commented Jun 4, 2024

Hey! So sorry for the long delay. I have finally been able to test this, however I'm not sure how as I have no updated module or code? It looks like this fix was blocked? Am I reading that wrong?

@gdbarron
Copy link
Collaborator

My turn to apologize for the long delay. The updated code is in the PR, https://github.com/Snow-Shell/servicenow-powershell/pull/261/files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants