Microsoft.Xrm.Data.PowerShell icon indicating copy to clipboard operation
Microsoft.Xrm.Data.PowerShell copied to clipboard

Exception /Error Handling

Open thuld opened this issue 10 years ago • 13 comments

Is it possible to get the server error message, when I execute the follwoing code....

Set-CrmRecordState -EntityLogicalName "invoice" -conn $conn `
-Id $invoice.invoiceid -StateCode $stateCodeActive -StatusCode $statusCodeNew

... is only this error generated

ScriptHalted
In C:\Users\foobar\Documents\WindowsPowerShell\Modules\Microsoft.Xrm.Data.PowerShell\Microsoft.Xrm.Data.Powershell.psm1:2088 Zeichen:4
+             throw $conn.LastCrmException
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], RuntimeException
    + FullyQualifiedErrorId : ScriptHalted

thuld avatar Jun 03 '16 07:06 thuld

I checked and this is not coming from server side but inside our code. (when calling base function). I will think about how to fix the issue. You are passing integer but our function expects string. So I may try to parse if its integer or not then handle appropriately.

kenakamu avatar Jun 24 '16 03:06 kenakamu

Interesting, it's not casting from int automatically toString?

seanmcne avatar Jun 24 '16 03:06 seanmcne

It doesn't. The function accept it as String and it is string forever. So we either need to add another paramset to accept integers or do try parse to integer in case it's not string.

kenakamu avatar Jun 24 '16 03:06 kenakamu

The Set-CrmRecordState does not work with integers. For some reason also calling the $conn.UpdateCrmRecordState does not work with integers also. It always told me that "Status" is missing.

So I had to make a legacy request, which works without problems. This is my request to disable a user:

$stateRequest = New-Object -TypeName  "Microsoft.Crm.Sdk.Messages.SetStateRequest";
$stateRequest.EntityMoniker = $UserRef;
$stateRequest.State = New-Object -TypeName  "Microsoft.Xrm.Sdk.OptionSetValue" -ArgumentList 1;
$stateRequest.Status = New-Object -TypeName  "Microsoft.Xrm.Sdk.OptionSetValue" -ArgumentList -1;

$result = $CrmConn.ExecuteCrmOrganizationRequest($stateRequest, "");
if(!$result)
{
    throw $CrmConn.LastCrmException
}

FlorianGrubert avatar Jul 04 '16 09:07 FlorianGrubert

I too am running into issues trying to use set-CrmRecordState and $conn.UpdateCrmRecordState when trying to use it to disable or re-enable a User in EntityLogicalName systemuser. I am able to run it fine on account without error.

@FlorianGrubert - I am attempting to use your Legacy Request for now until my issue is resolved but i cannot work out how to set the EntityMoniker via Powershell. Is it possible or can it only be set via C#?

tycho13 avatar Jul 05 '16 13:07 tycho13

@tycho13 The EntityMoniker is just a entity reference. You can create it with New-EntityReference. For the code above I used: $UserRef = New-CrmEntityReference -EntityLogicalName "systemuser" -Id $UserData.systemuserid

But be carefull: For using the New-Object you need the PowerShell to rund in Full Language Mode. So you usually have to start PowerShell as Administrator.

My fixed Set-Method Looks like this:

<#
.SYNOPSIS
    Updates the state of the given record
.DESCRIPTION
    Updates the state of the given record to the given state and status
.PARAMETER CrmConn
    The connection to use
.PARAMETER EntityReference
    Reference to the entity to update
.PARAMETER StateCode
    The numeric code of the state to set
.PARAMETER StatusCode
    The numeric code of the status to set. If not provided, the default will be used for the state
.EXAMPLE
    SetCrmRecordState $CrmConn $entityRef 1
.EXAMPLE
    SetCrmRecordState -CrmConn $CrmConn -EntityReference $entityRef -StateCode 0 -StateCode -1
#>
function SetCrmRecordState {
    param([Parameter(Mandatory=$True)][Microsoft.Xrm.Tooling.Connector.CrmServiceClient]$CrmConn,
        [Parameter(Mandatory=$True)][Microsoft.Xrm.Sdk.EntityReference]$EntityReference,
        [Parameter(Mandatory=$True)][int]$StateCode,
        [Parameter(Mandatory=$false)][int]$StatusCode = -1)

    $stateRequest = New-Object -TypeName  "Microsoft.Crm.Sdk.Messages.SetStateRequest";
    $stateRequest.EntityMoniker = $EntityReference;
    $stateRequest.State = New-CrmOptionSetValue $StateCode;
    $stateRequest.Status = New-CrmOptionSetValue $StatusCode;

    $result = $CrmConn.ExecuteCrmOrganizationRequest($stateRequest, "");
    if(!$result)
    {
        throw $CrmConn.LastCrmException
    }
}

FlorianGrubert avatar Jul 05 '16 13:07 FlorianGrubert

@tycho13 I was able to use the Legacy Request code by doing the following:

$stateRequest = New-Object -TypeName  "Microsoft.Crm.Sdk.Messages.SetStateRequest";
$stateRequest.EntityMoniker = New-CRMEntityReference systemuser $guid
$stateRequest.State = New-CrmOptionSetValue 1
$stateRequest.Status = New-CrmOptionSetValue -1

sisutcliffe avatar Jul 06 '16 08:07 sisutcliffe

Closing it for now as new function accept string, and int. Will add OptionSet too.

kenakamu avatar Aug 24 '16 10:08 kenakamu

FYI this bug is still happening for me.... Is there any plan to fix it?

`set-crmrecordstate -EntityLogicalName systemuser -Id $systemuserid Inactive Inactive

ScriptHalted At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.Xrm.Data.Powershell\Microsoft.Xrm.Data.Powershell.psm1: 2376 char:4

  •         throw $conn.LastCrmException
    
  •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : OperationStopped: (:) [], RuntimeException
    • FullyQualifiedErrorId : ScriptHalted`

kmacmillan25 avatar Nov 24 '17 20:11 kmacmillan25

Does this work for you?

$stateRequest = New-Object -TypeName "Microsoft.Crm.Sdk.Messages.SetStateRequest"; $stateRequest.EntityMoniker = New-CRMEntityReference systemuser $guid $stateRequest.State = New-CrmOptionSetValue 1 $stateRequest.Status = New-CrmOptionSetValue -1

seanmcne avatar Nov 25 '17 03:11 seanmcne

Thank you for your help! That looks like it should work but I ran it and unfortunately it seemed to have no effect. The test user is still enabled.

kmacmillan25 avatar Nov 27 '17 16:11 kmacmillan25

It's interesting that manually disabling the record through CRM and then trying to set it Active fails as well.

Also apparently the State and Status parameters only accept -1 as a valid integer. If I try it with 1 it doesn't like it.

PS C:> set-crmrecordstate -EntityLogicalName systemuser -Id $systemuserid Active Active ScriptHalted At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.Xrm.Data.Powershell\Microsoft.Xrm.Data.Powershell.psm1: 2376 char:4

  •         throw $conn.LastCrmException
    
  •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : OperationStopped: (:) [], RuntimeException
    • FullyQualifiedErrorId : ScriptHalted

PS C:> set-crmrecordstate -EntityLogicalName systemuser -Id $systemuserid 1 1 Expected value between -1 and -1 inclusive. At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.Xrm.Data.Powershell\Microsoft.Xrm.Data.Powershell.psm1: 2376 char:4

  •         throw $conn.LastCrmException
    
  •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : OperationStopped: (:) [], FaultException`1
    • FullyQualifiedErrorId : Expected value between -1 and -1 inclusive.

PS C:> set-crmrecordstate -EntityLogicalName systemuser -Id $systemuserid -1 -1 ScriptHalted At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.Xrm.Data.Powershell\Microsoft.Xrm.Data.Powershell.psm1: 2376 char:4

  •         throw $conn.LastCrmException
    
  •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : OperationStopped: (:) [], RuntimeException
    • FullyQualifiedErrorId : ScriptHalted

kmacmillan25 avatar Nov 27 '17 16:11 kmacmillan25

Ok I figured out why it wasn't working. You need an execute statement to make it apply the changes. So for example this worked for me:

Import-Module Microsoft.Xrm.Data.Powershell -verbose –force
$me = Get-Credential
Connect-CrmOnPremDiscovery -Credential $me -ServerUrl http://uatcrm.myorg.net -OrganizationName STABLE

$user = "Helen Matteer"

$CRMusers = Get-CrmRecords -EntityLogicalName systemuser -FilterAttribute fullname -FilterOperator eq -FilterValue $user -Fields systemuserid,fullname,domainname,isdisabled

$systemuserid = $CRMusers.CrmRecords.systemuserid

$stateRequest = New-Object -TypeName "Microsoft.Crm.Sdk.Messages.SetStateRequest";
$response = New-Object -TypeName "Microsoft.Crm.Sdk.Messages.SetStateResponse";
$stateRequest.EntityMoniker = New-CRMEntityReference systemuser $systemuserid
$stateRequest.State = New-CrmOptionSetValue 1
$stateRequest.Status = New-CrmOptionSetValue -1
$response = $Conn.Execute($stateRequest)

kmacmillan25 avatar Nov 27 '17 17:11 kmacmillan25