< Back
Set-CrmRecord
Post
NAME Set-CrmRecord
SYNOPSIS
Updates a new CRM record by specifying EntityLogicalName, record's Id (guid) and field name/value sets.
SYNTAX
Set-CrmRecord [-conn <CrmServiceClient>] [-CrmRecord] <PSObject> [-Upsert <SwitchParameter>] [-PrimaryKeyField
<String>] [<CommonParameters>]
Set-CrmRecord [-conn <CrmServiceClient>] [-EntityLogicalName] <String> [-Id] <Guid> [-Fields] <Hashtable> [-Upsert
<SwitchParameter>] [-PrimaryKeyField <String>] [<CommonParameters>]
DESCRIPTION
The Set-CrmRecord cmdlet lets you update a record of your CRM organization.
There are two ways to update a record.
1. Pass EntityLogicalName and record's Id and field name/value set. Use @{"field logical name"="value"} syntax to
update Fields, and make sure you specify correct type of value for the field.
You can use Get-CrmEntityAttributeMetadata cmdlet and check AttributeType to see the field type. In addition, for
CRM specific types, you can use New-CrmMoney, New-CrmOptionSetValue or New-CrmEntityReference cmdlets.
2. Get a record object by using Get-CrmRecord/Get-CrmRecords cmdlets, then assign new value for fields. if a field
you want to assign value is not available, you need to use first method to update the field.
PARAMETERS
-conn <CrmServiceClient>
A connection to your CRM organizatoin. Use $conn = Get-CrmConnection <Parameters> to generate it.
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-CrmRecord <PSObject>
A record object which is obtained via Get-CrmRecord/Get-CrmRecords. When you pass CrmRecord, then you don't
use EntityLogicalName/Id.
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-EntityLogicalName <String>
A logicalname for an Entity to update. i.e.)accout, contact, lead, etc..
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Id <Guid>
An Id (guid) of the record
Required? true
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Fields <Hashtable>
A List of field name/value pair. Use @{"field logical name"="value"} syntax to create Fields, and make sure
you specify correct type of value for the field.
You can use Get-CrmEntityAttributeMetadata cmdlet and check AttributeType to see the field type. In addition,
for CRM specific types, you can use New-CrmMoney, New-CrmOptionSetValue or New-CrmEntityReference cmdlets.
Required? true
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Upsert [<SwitchParameter>]
Spcify Upsert when you want to create a record if it does not exist in the system yet. It tries to retrieve a
record by using Id first, and if exists, update otherwise create.
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-PrimaryKeyField <String>
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
-------------------------- Example 1 --------------------------
Set-CrmRecord -conn $conn -EntityLogicalName account -Id 52a17637-5617-e511-80dc-c4346bc4fc6c -Fields
@{"name"="updated name";"telephone1"="555-5555"}
This example updates an account record by using Id and new value for name/telephone1 fields.
Though any of field value is same as current record, it still tries to update the field.
name_Property : [name, updated name]
name : updatedname
accountid_Property : [accountid, 52a17637-5617-e511-80dc-c4346bc4fc6c]
accountid : 52a17637-5617-e511-80dc-c4346bc4fc6c
original : {[name_Property, [name, updated name]], [name, updatedname], [accountid_Property, [accountid,
52a17637-5617-e511-80dc-c4346bc4fc6c]], [accountid, 52a17637-5617-e511-80dc-c4346bc4fc6c]}
logicalname : account
-------------------------- Example 2 --------------------------
PS C:\\>$fetch = @"
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" no-lock="true">
<entity name="account">
<attribute name="name" />
</entity>
</fetch>
"@
PS C:\\>(Get-CrmRecordsByFetch $fetch).CrmRecords | % { $_.name = $_.name + " updated!"; Set-CrmRecord -CrmRecord
$_}
This example retrieves and stores account records by using FetchXML and pipe results (CrmRecords). In the next
pipe, it does foreach operation (%) and assign new value for name. Then it updates each record using Set-CrmRecord.
-------------------------- Example 3 --------------------------
Set-CrmRecord account 52a17637-5617-e511-80dc-c4346bc4fc6c @{"industrycode"=New-CrmOptionSetValue -Value 1}
This example updates an account record by using Id and IndustryCode filed by specifying OptionSetValue.
When ommiting parameter names, you do not provide $conn, cmdlets automatically finds it.
-------------------------- Example 4 --------------------------
PS C:\\>$account = Get-CrmRecord account b202caab-6c16-e511-80d6-c4346bc43dc0 name
PS C:\\>$account
PS C:\\>$account.name = $account.name + " updated!"
PS C:\\>Set-CrmRecord $account
PS C:\\>Get-CrmRecord account b202caab-6c16-e511-80d6-c4346bc43dc0 name
This example retrieves and store an account record to $account object, then assign new value for name field. Then
update it by using Set-CrmRecord cmdlet.
Finally retrieves it again to display the result.
When omitting parameter names, you do not provide $conn, cmdlets automatically finds it.
PS C:\\>$account = Get-CrmRecord account b202caab-6c16-e511-80d6-c4346bc43dc0 name
PS C:\\>$account
name_Property : [name, Adventure Works (sample)]
name : Adventure Works (sample)
accountid_Property : [accountid, b202caab-6c16-e511-80d6-c4346bc43dc0]
accountid : b202caab-6c16-e511-80d6-c4346bc43dc0
original : {[name_Property, [name, Adventure Works (sample)]], [name, Adventure Works (sample)],
[accountid_Property, [accountid,
b202caab-6c16-e511-80d6-c4346bc43dc0]], [accountid, b202caab-6c16-e511-80d6-c4346bc43dc0]}
logicalname : account
PS C:\\>$account.name = $account.name + " updated!"
PS C:\\>Set-CrmRecord $account
PS C:\\>Get-CrmRecord account b202caab-6c16-e511-80d6-c4346bc43dc0 name
name_Property : [name, Adventure Works (sample) updated!]
name : Adventure Works (sample)updated!
accountid_Property : [accountid, b202caab-6c16-e511-80d6-c4346bc43dc0]
accountid : b202caab-6c16-e511-80d6-c4346bc43dc0
original : {[name_Property, [name, Adventure Works (sample) updated!]], [name, Adventure Works (sample)
updated!], [accountid_Property, [accountid,
b202caab-6c16-e511-80d6-c4346bc43dc0]], [accountid, b202caab-6c16-e511-80d6-c4346bc43dc0]}
logicalname : account
RELATED LINKS
SYNOPSIS
Updates a new CRM record by specifying EntityLogicalName, record's Id (guid) and field name/value sets.
SYNTAX
Set-CrmRecord [-conn <CrmServiceClient>] [-CrmRecord] <PSObject> [-Upsert <SwitchParameter>] [-PrimaryKeyField
<String>] [<CommonParameters>]
Set-CrmRecord [-conn <CrmServiceClient>] [-EntityLogicalName] <String> [-Id] <Guid> [-Fields] <Hashtable> [-Upsert
<SwitchParameter>] [-PrimaryKeyField <String>] [<CommonParameters>]
DESCRIPTION
The Set-CrmRecord cmdlet lets you update a record of your CRM organization.
There are two ways to update a record.
1. Pass EntityLogicalName and record's Id and field name/value set. Use @{"field logical name"="value"} syntax to
update Fields, and make sure you specify correct type of value for the field.
You can use Get-CrmEntityAttributeMetadata cmdlet and check AttributeType to see the field type. In addition, for
CRM specific types, you can use New-CrmMoney, New-CrmOptionSetValue or New-CrmEntityReference cmdlets.
2. Get a record object by using Get-CrmRecord/Get-CrmRecords cmdlets, then assign new value for fields. if a field
you want to assign value is not available, you need to use first method to update the field.
PARAMETERS
-conn <CrmServiceClient>
A connection to your CRM organizatoin. Use $conn = Get-CrmConnection <Parameters> to generate it.
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-CrmRecord <PSObject>
A record object which is obtained via Get-CrmRecord/Get-CrmRecords. When you pass CrmRecord, then you don't
use EntityLogicalName/Id.
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-EntityLogicalName <String>
A logicalname for an Entity to update. i.e.)accout, contact, lead, etc..
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Id <Guid>
An Id (guid) of the record
Required? true
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Fields <Hashtable>
A List of field name/value pair. Use @{"field logical name"="value"} syntax to create Fields, and make sure
you specify correct type of value for the field.
You can use Get-CrmEntityAttributeMetadata cmdlet and check AttributeType to see the field type. In addition,
for CRM specific types, you can use New-CrmMoney, New-CrmOptionSetValue or New-CrmEntityReference cmdlets.
Required? true
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Upsert [<SwitchParameter>]
Spcify Upsert when you want to create a record if it does not exist in the system yet. It tries to retrieve a
record by using Id first, and if exists, update otherwise create.
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-PrimaryKeyField <String>
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
-------------------------- Example 1 --------------------------
Set-CrmRecord -conn $conn -EntityLogicalName account -Id 52a17637-5617-e511-80dc-c4346bc4fc6c -Fields
@{"name"="updated name";"telephone1"="555-5555"}
This example updates an account record by using Id and new value for name/telephone1 fields.
Though any of field value is same as current record, it still tries to update the field.
name_Property : [name, updated name]
name : updatedname
accountid_Property : [accountid, 52a17637-5617-e511-80dc-c4346bc4fc6c]
accountid : 52a17637-5617-e511-80dc-c4346bc4fc6c
original : {[name_Property, [name, updated name]], [name, updatedname], [accountid_Property, [accountid,
52a17637-5617-e511-80dc-c4346bc4fc6c]], [accountid, 52a17637-5617-e511-80dc-c4346bc4fc6c]}
logicalname : account
-------------------------- Example 2 --------------------------
PS C:\\>$fetch = @"
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" no-lock="true">
<entity name="account">
<attribute name="name" />
</entity>
</fetch>
"@
PS C:\\>(Get-CrmRecordsByFetch $fetch).CrmRecords | % { $_.name = $_.name + " updated!"; Set-CrmRecord -CrmRecord
$_}
This example retrieves and stores account records by using FetchXML and pipe results (CrmRecords). In the next
pipe, it does foreach operation (%) and assign new value for name. Then it updates each record using Set-CrmRecord.
-------------------------- Example 3 --------------------------
Set-CrmRecord account 52a17637-5617-e511-80dc-c4346bc4fc6c @{"industrycode"=New-CrmOptionSetValue -Value 1}
This example updates an account record by using Id and IndustryCode filed by specifying OptionSetValue.
When ommiting parameter names, you do not provide $conn, cmdlets automatically finds it.
-------------------------- Example 4 --------------------------
PS C:\\>$account = Get-CrmRecord account b202caab-6c16-e511-80d6-c4346bc43dc0 name
PS C:\\>$account
PS C:\\>$account.name = $account.name + " updated!"
PS C:\\>Set-CrmRecord $account
PS C:\\>Get-CrmRecord account b202caab-6c16-e511-80d6-c4346bc43dc0 name
This example retrieves and store an account record to $account object, then assign new value for name field. Then
update it by using Set-CrmRecord cmdlet.
Finally retrieves it again to display the result.
When omitting parameter names, you do not provide $conn, cmdlets automatically finds it.
PS C:\\>$account = Get-CrmRecord account b202caab-6c16-e511-80d6-c4346bc43dc0 name
PS C:\\>$account
name_Property : [name, Adventure Works (sample)]
name : Adventure Works (sample)
accountid_Property : [accountid, b202caab-6c16-e511-80d6-c4346bc43dc0]
accountid : b202caab-6c16-e511-80d6-c4346bc43dc0
original : {[name_Property, [name, Adventure Works (sample)]], [name, Adventure Works (sample)],
[accountid_Property, [accountid,
b202caab-6c16-e511-80d6-c4346bc43dc0]], [accountid, b202caab-6c16-e511-80d6-c4346bc43dc0]}
logicalname : account
PS C:\\>$account.name = $account.name + " updated!"
PS C:\\>Set-CrmRecord $account
PS C:\\>Get-CrmRecord account b202caab-6c16-e511-80d6-c4346bc43dc0 name
name_Property : [name, Adventure Works (sample) updated!]
name : Adventure Works (sample)updated!
accountid_Property : [accountid, b202caab-6c16-e511-80d6-c4346bc43dc0]
accountid : b202caab-6c16-e511-80d6-c4346bc43dc0
original : {[name_Property, [name, Adventure Works (sample) updated!]], [name, Adventure Works (sample)
updated!], [accountid_Property, [accountid,
b202caab-6c16-e511-80d6-c4346bc43dc0]], [accountid, b202caab-6c16-e511-80d6-c4346bc43dc0]}
logicalname : account
RELATED LINKS