如何使用主数据Web服务在动态CRM中设置parentaccountid

本文关键字:CRM 动态 设置 parentaccountid 服务 何使用 数据 Web | 更新日期: 2023-09-27 18:27:04

我在.Net中创建了一个简单的控制台应用程序,以在CRM中创建一个新帐户。为此,我使用Main Data WebService,它也创建了,但我的问题是,我必须在Account中设置Parent Customer,为此,我必须在此设置parentcustomerid,但我不知道如何设置它。我的代码如下。

   namespace ConsoleWebService
   {
      class MainDataService
      {
         static void Main(string[] args)
         {
           Console.WriteLine("New Account GUID="+CreateAccount("Server","New Account"));
           Console.ReadKey();
          }
         private static string CreateAccount(string organizationName, string accountName)
         {
           try
           {
            CrmSdk.CrmService myCrm = new CrmSdk.CrmService();
            myCrm.Url = GetCrmServiceForOrganization(organizationName);
            CrmSdk.CrmAuthenticationToken myToken = new CrmSdk.CrmAuthenticationToken();
            myToken.AuthenticationType = 0;
            myToken.OrganizationName = organizationName;
            myCrm.CrmAuthenticationTokenValue = myToken;
            myCrm.Credentials = System.Net.CredentialCache.DefaultCredentials;
            CrmSdk.account newAccount = new CrmSdk.account();
            newAccount.name = accountName;
            newAccount.address1_country = "India";
            newAccount.address1_city = "Mumbai";
            Guid newAccountId = myCrm.Create(newAccount);
            return newAccountId.ToString();
          }
          catch (System.Web.Services.Protocols.SoapException soapEx)
         {
            Console.WriteLine("SOAP exception: " + soapEx.Detail.InnerText + " " + soapEx.ToString());
            return soapEx.Detail.InnerText + "  " + soapEx.ToString();
          }
         catch (Exception ex)
         {
            Console.WriteLine("General exception: " + ex.ToString());
            return "General exception: " + ex.ToString();
         }
    }
    private static string GetCrmServiceForOrganization(string organizationName)
    {
        string urlResult = "";
        CrmSdk.Discovery.CrmDiscoveryService myCrm = new CrmSdk.Discovery.CrmDiscoveryService();
        myCrm.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
        CrmSdk.Discovery.RetrieveOrganizationsRequest myRequest = new CrmSdk.Discovery.RetrieveOrganizationsRequest();
        CrmSdk.Discovery.RetrieveOrganizationsResponse myResponse = (CrmSdk.Discovery.RetrieveOrganizationsResponse)myCrm.Execute(myRequest);
        foreach (CrmSdk.Discovery.OrganizationDetail tDetail in myResponse.OrganizationDetails)
        {
            Console.WriteLine("Organization = " + tDetail.OrganizationName);
            if (String.Compare(tDetail.OrganizationName, organizationName, true) == 0)
            {
                return tDetail.CrmServiceUrl;
            }
        }
        return urlResult;
    }
}

}

在这里,我可以使用上面的代码创建新帐户,但我想在account中设置父客户,那么我该如何设置呢?

如何使用主数据Web服务在动态CRM中设置parentaccountid

试试这个:

Lookup parentAccount = new Lookup();
parentAccount.type = EntityName.account.ToString();
parentAccount.Value = new Guid("GUID of The Parent Account");
newAccount.parentaccountid = parentAccount;