Home Advanced Request Offering

Add Reviewer to RA based on usertext. ParentSR

Ryan_KelleyRyan_Kelley Customer IT Monkey ✭

I have a rather large script that deals with alot of manipulations but this part stumps me:
this code works but I wanted to modify it so that it take usertext9 off of the parentSR and then applies on a RA down the workflow based on title. However I get error messages:
An error occurred while adding a reviewer: Id='RA1034963' -- Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

function Add-SCSMReviewer ($ReviewActivityID, $SCSMUser)
{
#Get the classes we need to work with
$raClass = Get-SCSMClass -name "System.WorkItem.Activity.ReviewActivity$"
$reviewerClass = Get-SCSMClass -name "System.Reviewer$"
$raHasReviewerRelClass = Get-SCSMRelationshipClass "System.ReviewActivityHasReviewer$"
$raReviewerIsUserRelClass = Get-SCSMRelationshipClass "System.ReviewerIsUser$"
#Get the Review Activity to update
$ra = Get-SCSMObject -class $raClass -filter "Name -eq '$ReviewActivityID'"

#Define an empty Reviewer object and their voting options in memory only by declaring the "-NoCommit" switch
$reviewer = New-SCSMObject -Class $reviewerClass -PropertyHashtable @{"ReviewerID" = "{0}"; "Veto" = $false; "MustVote" = $false} -NoCommit

#Relate the empty Reviewer to the Review Activity in memory by declaring the "-NoCommit" switch
$RAhasReviewer = New-SCSMRelationshipObject -Source $ra -Relationship $raHasReviewerRelClass -Target $reviewer -NoCommit

#Relate the empty Reviewer object to a User in memory by declaring the "-NoCommit" switch
$reviewerHasUser = New-SCSMRelationshipObject -Source $reviewer -Relationship $raReviewerIsUserRelClass -Target $SCSMUser -NoCommit

#With everything defined in memory, finally commit/save all of those items in one go to Service Manager
$RAhasReviewer.Commit()
$reviewerHasUser.Commit()
} #Get the user class and the user object for the reviewer
$userClass = Get-SCSMClass -name "System.Domain.User$"
$user = Get-SCSMObject -Class $userClass -Filter "UserName -eq 'me!'" #Call the function to add the reviewer to the Review Activity
Add-SCSMReviewer -ReviewActivityID "RA1034963" -SCSMUser $user
param([guid]$parentId)

# Set default computer for SMLets Prod
#$GLOBAL:smdefaultcomputer = "CINSHRSSM01"
# Set default computer for SMLets test
$GLOBAL:smdefaultcomputer = "CINSHRSSMt4"

# Ensure SMLets module is loaded
if(-not(Get-Module -Name Smlets)) {
    Import-Module SMLets
}
#### This sets all activties with the title format
# Retrieve the parent SR object
$parentSR = Get-SCSMObject -Id $parentId

# Check if parent SR was successfully retrieved
if($null -like $parentSR) {
    Write-Output "Parent Service Request not found."
}
# Retrieve all activities related to the parent SR $containsActivity = Get-SCSMRelationshipClass -Name "System.WorkItemContainsActivity$" $AllActivities = Get-SCSMRelatedObject -SMObject $parentSR -Relationship $containsActivity -Depth Recursive
# Ensure the SMLets module is loaded if (-not (Get-Module -Name Smlets)) { Import-Module Smlets } $userTexts = @( ($parentSR.UserText1 -split "~"), $parentSR.UserText2, $parentSR.UserText3, $parentSR.UserText4, ($parentSR.UserText5 -split "~"), $parentSR.UserText6, ($parentSR.UserText7 -split "~"), ($parentSR.UserText8 -split "~"), $parentSR.UserText9, $parentSR.UserText10 ) try { # Retrieve and process activities related to the parent SR $containsActivity = Get-SCSMRelationshipClass -Name "System.WorkItemContainsActivity$" $AllActivities = Get-SCSMRelatedObject -SMObject $parentSR -Relationship $containsActivity -Depth Recursive $activityTitles = @("*Manager Approval For Remote/Mobile Access For Travel Outside The USA*") } catch { Write-Output "An error occurred in the script at line number: $($_.InvocationInfo.ScriptLineNumber). The error message is: $($_.Exception.Message)" } function Add-SCSMReviewer ($ReviewActivityID, $SCSMUser) { try { $raClass = Get-SCSMClass -Name "System.WorkItem.Activity.ReviewActivity$" $reviewerClass = Get-SCSMClass -Name "System.Reviewer$" $raHasReviewerRelClass = Get-SCSMRelationshipClass "System.ReviewActivityHasReviewer$" $raReviewerIsUserRelClass = Get-SCSMRelationshipClass "System.ReviewerIsUser$" $ra = Get-SCSMObject -Class $raClass -Filter "Id -eq '$ReviewActivityID'" if ($null -eq $ra) { Write-Output "No Review Activity found with ID: $ReviewActivityID" return } $reviewer = New-SCSMObject -Class $reviewerClass -PropertyHashtable @{ "ReviewerID" = "{0}"; "Veto" = $false; "MustVote" = $false } -NoCommit $RAhasReviewer = New-SCSMRelationshipObject -Source $ra -Relationship $raHasReviewerRelClass -Target $reviewer -NoCommit $reviewerHasUser = New-SCSMRelationshipObject -Source $reviewer -Relationship $raReviewerIsUserRelClass -Target $SCSMUser -NoCommit $RAhasReviewer.Commit() $reviewerHasUser.Commit() } catch { Write-Output "An error occurred while adding a reviewer: $($_.Exception.Message)" } } try { foreach ($Activity in $AllActivities) { foreach ($title in $activityTitles) { if ($Activity.Title -like $title) { $userClass = Get-SCSMClass -Name "System.Domain.User$" $SCSMUser = Get-SCSMObject -Class $userClass -Filter "UserName -eq $($parentSR.UserText9)" Add-SCSMReviewer -ReviewActivityID $Activity.Id -SCSMUser $SCSMUser } } } } catch { Write-Output "An error occurred in the script at line number: $($_.InvocationInfo.ScriptLineNumber). The error message is: $($_.Exception.Message)" } Write-Output "Process completed successfully." # Optional: Unload SMLets module after execution Remove-Module SMLets

my script:

The usertext9 field comes in displayed as the userid.

Please let me know if there are nay questions I can answer but I am looking for a bit of help as I am stumped. Thank you!

Best Answer

  • Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
    edited April 15 Answer ✓

    @Ryan_Kelley in the second script you receive the RA with

    Get-scsmobject -class $raClass -filter "Id -eq '$ReviewActivityID'"
    

    That cannot work as you pass the workitemId (which is not the Guid) to the function. If you filter by Id you have to pass the guid though. In your first script the filter is correct ;) so you can either change the filter to

    "Name -eq $ReviewActivityID"
    

    or you pass the RA Id to the function call instead of the workitem Id

Answers

  • Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
    edited April 15 Answer ✓

    @Ryan_Kelley in the second script you receive the RA with

    Get-scsmobject -class $raClass -filter "Id -eq '$ReviewActivityID'"
    

    That cannot work as you pass the workitemId (which is not the Guid) to the function. If you filter by Id you have to pass the guid though. In your first script the filter is correct ;) so you can either change the filter to

    "Name -eq $ReviewActivityID"
    

    or you pass the RA Id to the function call instead of the workitem Id

  • Ryan_KelleyRyan_Kelley Customer IT Monkey ✭

    That worked!
    Thank you @Simon_Zeinhofer !

  • Geoff_RossGeoff_Ross Cireson Consultant O.G.

    Great work Simon.

    Ryan, could this be a good solution to submit for the Cireson ITSM Awards?

    https://cireson.com/cireson-itsm-awards/

  • Ryan_KelleyRyan_Kelley Customer IT Monkey ✭

    That could be a good solution! Yes please submit for the awards- @Geoff_Ross !

  • Geoff_RossGeoff_Ross Cireson Consultant O.G.

    You'll need to submit Ryan - all the details are on the link. Let me know if you need help though.

    https://cireson.com/cireson-itsm-awards/

Sign In or Register to comment.