In this earlier post I showed how to loop through all the contacts in CRM 2011. Next thing I wanted to do was to update a field on each Account. So I needed to figure out how to update data, not just read it. Here is the code to do that:
$assembly = [Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions") $url="http://your.crm.com/Instance/xrmservices/2011/OrganizationData.svc/AccountSet(guid'GUIDofAccount')" $webclient = new-object System.Net.WebClient $webclient.UseDefaultCredentials = $true $webclient.Headers.Add("Accept", "application/json") $webclient.Headers.Add("Content-Type", "application/json; charset=utf-8"); $webclient.Headers.Add("X-HTTP-Method", "MERGE") $stringToUpload="{`"AccountNumber`":`"123456`"}" $resultString=$webclient.UploadString($url,$stringToUpload)
Hi Jeff
Great post and I have found your site incredibly useful (thank you)
I’m having an issue using the PowerShell code to update a field in CRM 2011 – below is the code I’m using
$assembly = [Reflection.Assembly]::LoadWithPartialName(“System.Web.Extensions”)
$url=”http://TestServer/TestCRM/xrmservices/2011/OrganizationData.svc/LeadSet?$filter=LeadId eq guid’$GUIDofAccount'”
$webclient = new-object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
$webclient.Headers.Add(“Accept”, “application/json”)
$webclient.Headers.Add(“Content-Type”, “application/json; charset=utf-8”);
$webclient.Headers.Add(“X-HTTP-Method”, “MERGE”)
$stringToUpload=”{`”new_Contacted`”:`”Yes`”}”
$resultString=$webclient.UploadString($url,$stringToUpload)
But when I execute the query I get the following error
Exception calling “UploadString” with “2” argument(s): “The remote server returned an error: (405) Method Not Allowed.”
At C:\scripts\CRMUpdateLead.ps1:48 char:1
+ $resultString=$webclient.UploadString($url,$stringToUpload)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
The issue would appear to relate to the formatting of $stringToUpload=”{`”new_Contacted`”:`”Yes`”}”
But I’m struggling to resolve or figure out the correct syntax – Can you help?
Hi Jeff,
Resolved the problem now 🙂
Would you know the correct syntax to update / change the ownerid of record ?
Regards
Mark
Hi Jeff
After spending some time, I have managed to figure out the correct syntax to update a lookup field 🙂
For reference here is the syntax
$stringToUpload=”{`”new_Record`”:{`”Id`”:`”Test`”,`”LogicalName`”:`”newcode`”,`”Name`”:`”Test`”}}”
Thank you for the updates! I have been working on different types of projects, so I have not had a chance to go back and look at these older posts. I hope you found everything you needed.