Apply template to projection and add a property before committing the changes

Hi!

I'm trying to create a Service Request programmatically by creating a new projection object and then applying a template with 4 activities.

The problem is, I can apply the template successfully, but I'm trying to add the Id property for the child activities with the right prefix, but for some reason I cannot navigate the activities on the projection unless I submit the changes:

$NewReqProj = @{
    __CLASS  = "System.WorkItem.ServiceRequest";
    __OBJECT = @{
                 Id          = "SR{0}"
                 Status      = (Get-SCSMEnumeration ServiceRequestStatusEnum.New$).Id.Guid
                 CreatedDate = (Get-Date).ToUniversalTime()
                 UserInput   = $UserInput
                };
}

$NewRequest = New-SCSMObjectProjection -Type System.WorkItem.ServiceRequestProjection -Projection $NewReqProj -NoCommit
$NewRequest.ApplyTemplate($ReqTemplate)

PS C:\> $NewRequest.Object
ClassName                          DisplayName   LastModified
---------                          -----------   ------------
System.WorkItem.ServiceRequest     SR5176        1/1/0001 12:00:00 AM

PS C:\> $NewRequest
Key                                                         Value
---                                                         -----
Activity                                                    {}
Activity                                                    {}
Activity                                                    {}
Activity                                                    {}

Is anybody aware of any way of getting to the child activities and adding the Id property to them so I can commit the changes right after that?

Thanks!

German

July 28th, 2013 10:49pm

By the way, I know this should work, but it doesn't:

PS C:\> $WIContainsAct = Get-SCSMRelationshipClass System.WorkItemContainsActivity$

PS C:\> $NewRequest[$WIContainsAct.Target]
PS C:\> 

It returns no rows :(

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2013 11:15pm

Hello,

You are using the wrong projection it does not have a Activity component. You should use System.WorkItem.ServiceRequestAndActivityViewProjection

Change your type projection definition to look like this

 __OBJECT = @{
                 Id          = "SR{0}"
                 Status      = (Get-SCSMEnumeration ServiceRequestStatusEnum.New$).Id.Guid
                 CreatedDate = (Get-Date).ToUniversalTime()
                 UserInput   = $UserInput
				 Activity = @{__CLASS = "System.WorkItem.Activity.ManualActivity";
                               		__OBJECT = @{"ID"="MA{0}";}
                                 }
                }

You can read more about this here

If you need more data then you have to create a custom type projection.

July 30th, 2013 9:35am

Hi Gary!

Thanks, for a minute I thought that was going to fix it, but it didn't.

I'm getting the same results using that projection.

I'm getting close to give out and just add the prefixes on the templates (even though that means duplicating them).


Free Windows Admin Tool Kit Click here and download it now
July 30th, 2013 11:08am

I found the answer! We were all on the right track, it's just that it's not exposed, so you have to know how to reference the child activities adding ".Object". This works:

$NewRequest[$WIContainsAct.Target] |% {
    $ActivityClassName = $_.Object.ClassName
    $_.Object.Values |% {
        if ($_.Type.Name -eq "Id") {
            $_.Value = ((Get-ActivityIdPrefix $ActivityClassName) + $_.Value)
        }
    }
}

Thanks!

July 30th, 2013 3:32pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics