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

Set-CrmRecordState for Invoice is not working

Open thuld opened this issue 10 years ago • 10 comments

The following code is not working:

$conn = Get-CrmConnection -InteractiveMode

$invoiceNo = "INV-07017-G5W3W7"

# status
$stateCodeActive = 0
$stateCodePaid = 2

# status-reason
$statusCodeNew = 1
$statusCodeComplete = 100001

$invoice = (Get-CrmRecords -EntityLogicalName "invoice" "invoicenumber" "eq" $invoiceNo -Fields "invoiceid", "statuscode", "statecode").CrmRecords

# change invoice status back to "Active"

# not working!
# Set-CrmRecordState -EntityLogicalName "invoice" -conn $conn -Id $invoice.invoiceid -StateCode $stateCodePaid -StatusCode $statusCodeComplete
# Set-CrmRecordState -EntityLogicalName "invoice" -conn $conn -Id $invoice.invoiceid -StateCode "Paid" -StatusCode "Complete"

# working
$conn.UpdateStateAndStatusForEntity("invoice", $invoice.invoiceid, $stateCodePaid, $statusCodeComplete)

When I use UpdateStateAndStatusForEntity...

$conn.UpdateStateAndStatusForEntity("invoice", $invoice.invoiceid, $stateCodePaid, $statusCodeComplete)

... instead of Set-CrmRecordState, is it possible to set the state of an invoice

Set-CrmRecordState -EntityLogicalName "invoice" -conn $conn -Id $invoice.invoiceid -StateCode "Paid" -StatusCode "Complete"

Is it possible that this is caused by the fakt that Set-CrmRecordStateuseds string instead of int?

thuld avatar Jun 03 '16 10:06 thuld

Hi, As you said, Set-CrmRecordState takes actual status string to its parameter, not status code (int), Did the last sample you explains here work?

kenakamu avatar Jun 23 '16 06:06 kenakamu

Hi,

no the last example is not working.

Daniel

thuld avatar Jun 23 '16 09:06 thuld

Interesting. This works in my environment. Set-CrmRecordState -EntityLogicalName "invoice" -conn $conn -Id $invoice.invoiceid -StateCode "Paid" -StatusCode "Complete"

is it possible that your account is not set to English? In that case you need to use the language.

kenakamu avatar Jun 24 '16 03:06 kenakamu

Really odd....I run the follwoing code and the invoice was not update, the method returned False

$conn.UpdateStateAndStatusForEntity("invoice", $invoice.invoiceid, $stateCodePaid, $statusCodeComplete)

Maybe this is caused by the fact that I try to update an invoice in status "Paid" status-reason "Partial"?

Regarding the language queston: My user language and base-language are English.

thuld avatar Jun 24 '16 13:06 thuld

If it works with other combination, it could be.

kenakamu avatar Jun 30 '16 08:06 kenakamu

I tried to work with the function and noticed that it is language sensitive on the one hand and not working with integers on the other hand. If I use one of the following calls, I also get "ScriptHalted".

Set-CrmRecordState account 1bf8d93d-1f18-e511-80da-c4346bc43d94 1 -1 -conn $CrmConn
Set-CrmRecordState account 1bf8d93d-1f18-e511-80da-c4346bc43d94 Inactive Inactive -conn $CrmConn
Set-CrmRecordState -conn $CrmConn -EntityLogicalName account -Id CB51976E-B859-4EF4-9F3D-A01035A1B9C6 -StateCode 1 -StatusCode 1
Set-CrmRecordState -conn $CrmConn -EntityLogicalName 'account' -Id CB51976E-B859-4EF4-9F3D-A01035A1B9C6 -StateCode 'Inactive' -StatusCode 'Inactive'

This is the only call that works: Set-CrmRecordState account 1bf8d93d-1f18-e511-80da-c4346bc43d94 Inaktiv Inaktiv -conn $CrmConn

I have to write "inaktiv" because my user is set to german in CRM. I would perfer to set the numeric values rather than text. Because numeric values are more flexible and longer lasting also. Especially I would really like to set -1 as status, to get the default.

FlorianGrubert avatar Jul 04 '16 08:07 FlorianGrubert

yes. I will update the function to accept number in next version.

kenakamu avatar Jul 05 '16 14:07 kenakamu

Just updated the function to accept integer instead of string. Please try and let me know your opinion. The thing is, I didn't change parameter type, not adding new parameter, but leave the parameter type as string but try parse to int32 to see if you passed int.

kenakamu avatar Jul 21 '16 16:07 kenakamu

Closing for now.

kenakamu avatar Aug 24 '16 10:08 kenakamu

Hello, I'm still seeing this same error over a year later.... It doesn't seem to matter whether I use integers or strings for the parameters.

I'm trying to use it to deactivate user accounts in multiple organizations for a user who has left the company...

Are there any plans to fix this? Activating and deactivation user accounts would be very useful thing to have for administrators! I'm using CRM 2011 rollup 18 on-premise.

For example:

Example 1:

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

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

Example 2:

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

Example 3:

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 01:11 kmacmillan25