Im trying to write a VS2010 SQLCLR database project to update our CRM2011 from a legacy SQL system. I have been following and trying what seems to be hundreds of snippets of code, but after weeks have still not managed to gat it working.
The 'name' field on each account is a six digit number (LicenceNo) and is unique. What I am trying to do in this exercise is pass a LicenceNo and the 'new_companyname' value be changed to "Updated". Here is my code below, if some one could read through it and show me what I am doing wrong. I'm at the end on my wits. I am using CRMService
using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using SqltoCRMTest.crmSdk; public partial class StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] public static void UpdateCustomer(SqlString LicenceNo) { CrmAuthenticationToken token = new CrmAuthenticationToken(); token.AuthenticationType = 0; token.OrganizationName = "xxxxx"; using (CrmService crmService = new CrmService()) { crmService.Url = "https://xxxx.xxx:xx/MSCRMServices/2007/CrmService.asmx"; crmService.Credentials = new System.Net.NetworkCredential("xxx", xxxx", "xxx"); crmService.CrmAuthenticationTokenValue = token; crmService.UnsafeAuthenticatedConnectionSharing = true; crmService.PreAuthenticate = true; ConditionExpression condition1 = new ConditionExpression(); condition1.AttributeName = "name"; condition1.Operator = ConditionOperator.Equal; condition1.Values = new string[] {(string) LicenceNo }; FilterExpression filter = new FilterExpression(); filter.FilterOperator = LogicalOperator.And; filter.Conditions = new ConditionExpression[] { condition1 }; ColumnSet resultSetColumns = new ColumnSet(); resultSetColumns.Attributes = new string[] { "accountid"}; // Put everything together in an expression. QueryExpression qryExpression = new QueryExpression() ; qryExpression.ColumnSet = resultSetColumns; // set a filter to the query qryExpression.Criteria = filter; // Set the table to query. qryExpression.EntityName = EntityName.account.ToString(); // Return distinct records. qryExpression.Distinct = true; // Execute the query. BusinessEntityCollection accountResultSet = crmService.RetrieveMultiple(qryExpression); string pId; foreach (account aAccount in accountResultSet.BusinessEntities) { // Update account account = new account(); account.new_companyname = "Updated"; account.accountid = new Guid(aAccount.accountid.Value.ToString()); } } } };
Dont ask me .. i dont know