4. Dezember 2009 11:10
4. Dezember 2009 12:05
Anbei eine einfache Möglichkeit, Firmen und Kontakte nachträglich im CRM zu verbinden.
Dazu jeweils bei der Firma und beim Kontakt ein Feld anlegen, in dem die Zuordnung der Kontakte zu Firmen hinterlegt ist. Ist der Inhalt der beiden Felder gleich, werden die Kontakte den Firmen zugeordnet.
Zuerst die beiden neuen Felder über das CRM in der Datenbank anlegen:
Feld in der Firma = IDImportFirma
Feld im Kontakt = IDImportFirma
Anschließend folgenden Befehl im SQL-Server eingegeben und ausführen, um die Verknüpfung zu erstellen:
HINWEIS: Eventuell muss der Präfix, hier new_, der verknüpften Felder angepasst werden.
DECLARE @IDImportFirma nvarchar(50)
DECLARE @contactID nvarchar(50)
DECLARE CursorContact CURSOR FOR
SELECT new_IDImportFirma, contactID FROM Contact WHERE new_IDImportFirma IS NOT NULL AND new_IDImportFirma <> ''
OPEN CursorContact
FETCH NEXT FROM CursorContact INTO @IDImportFirma, @contactID
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE Contact SET AccountID =
(SELECT AccountID FROM Account WHERE new_IDImportFirma = @IDImportFirma)
WHERE new_IDImportFirma = @IDImportFirma
FETCH NEXT FROM CursorContact INTO @IDImportFirma, @contactID
END
CLOSE CursorContact
DEALLOCATE CursorContact
4. Dezember 2009 13:49
using System;
using System.Collections;
using System.Net;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
namespace CrmAccountContactRelation
{
class Program
{
static void Main(string[] args)
{
// Create authentification token for crm-webservice
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = CrmAccountContactRelation.Resource1.CompanyName.ToString();
// Create crm-webservice
CrmService crmService = new CrmService();
crmService.Url = CrmAccountContactRelation.Resource1.ServerUrl + CrmAccountContactRelation.Resource1.WebService;
crmService.CrmAuthenticationTokenValue = token;
crmService.Credentials = CredentialCache.DefaultCredentials;
// Build query for 'contact'-entities
QueryExpression query = new QueryExpression();
query.EntityName = EntityName.contact.ToString();
query.ColumnSet = new AllColumns();
query.ColumnSet.AddColumn("parentcustomerid");
// Get 'contact'-collection
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.ReturnDynamicEntities = true;
request.Query = query;
RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmService.Execute(request);
BusinessEntityCollection contacts = response.BusinessEntityCollection;
// Build query for 'account'-entities
query.EntityName = EntityName.account.ToString();
query.ColumnSet = new ColumnSet(new string[] { "accountid", "accountnumber" });
// Get 'account'-collection
request.Query = query;
response = (RetrieveMultipleResponse)crmService.Execute(request);
BusinessEntityCollection accounts = response.BusinessEntityCollection;
// Create Dynamic Property
CustomerProperty parentCustomer = new CustomerProperty();
parentCustomer.Name = "parentcustomerid";
parentCustomer.Value = new Customer();
parentCustomer.Value.type = EntityName.account.ToString();
Key accountKey;
int count = 0;
// Compare IDs of 'contacts' and 'accounts'
foreach (DynamicEntity contact in contacts.BusinessEntities)
{
if ( contact.Properties.Contains(CrmAccountContactRelation.Resource1.contactKey) )
{
foreach (DynamicEntity account in accounts.BusinessEntities)
{
if ( account.Properties.Contains("accountnumber") &&
account.Properties["accountnumber"].ToString() ==
contact.Properties[CrmAccountContactRelation.Resource1.contactKey].ToString() )
{
// Set parentCustomer property for contact
accountKey = (Key)account.Properties["accountid"];
parentCustomer.Value.Value = accountKey.Value;
contact.Properties.Add(parentCustomer);
// Update entity
Update(crmService, contact);
count++;
break;
}
}
}
}
string s = "";
s += "Finished...(";
s += count.ToString();
s += " of ";
s += contacts.BusinessEntities.Count.ToString();
s += " updated)";
Console.WriteLine(s);
Console.ReadKey();
}
private static void Update(CrmService service, DynamicEntity entity)
{
TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();
UpdateRequest updateRequest = new UpdateRequest();
UpdateResponse updateResponse = new UpdateResponse();
updateDynamic.Entity = entity;
updateRequest.Target = updateDynamic;
try
{
try
{
Console.WriteLine("Update: " + entity.Properties["fullname"].ToString());
}
catch (Exception ex)
{
Console.WriteLine("Update: " + ex.Message);
}
updateResponse = (UpdateResponse)service.Execute(updateRequest);
}
catch (System.Web.Services.Protocols.SoapException sex)
{
throw new Exception(sex.Detail.InnerXml.ToString());
}
}
}
}
4. Dezember 2009 13:58
4. Dezember 2009 15:14
6. Dezember 2009 12:04