I am creating a simple console Application in .Net to create a New Account in CRM.For that I am using Main Data WebService and it also created but my question is that i have to set Parent Customer in Account and For that i have to set parentcustomerid in that but i am not getting how can i set it..My code is below.
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;
}
}
}
Here I can create the new account using above code but i want to set the parent custome in Account so how can i set?
Try this:
Lookup parentAccount = new Lookup();
parentAccount.type = EntityName.account.ToString();
parentAccount.Value = new Guid("GUID of The Parent Account");
newAccount.parentaccountid = parentAccount;