Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Create Contacs+Accounts and link them. From XML to Visual Studio to CRM

Posted on by Microsoft Employee

Hello everyone. Im fairly new to programming and everything around it and I cant break a "wall" to continue. Code so far:

xml: 

<contacts>
    <contact>
        <firstname>f1</firstname>
        <lastname>l1</lastname>
        <email>f1@email.com</email>
  <customer type="Lookup" entityType="account" searchField="name" allowCreate="true">
   <name>company1</name>
   <street>street1</street>
   <city>city1</city>
  </customer>
    </contact>
    <contact>
        <firstname>f2</firstname>
        <lastname>l2</lastname>
        <email>f2@email.com</email>
  <customer type="Lookup" entityType="account" searchField="name" allowCreate="false">
   <name>company2</name>
  </customer>
    </contact>
</contacts>

note: company 2 exists in crm already.

namespace createContact
{
    class Program
    {
        static void Main(string[] args)
        {
            CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString);
            IOrganizationService crmService = crmConn.OrganizationServiceProxy;
            Entity con = new Entity("contact");
            string[] info = new string[] { "firstname", "lastname", "emailaddress1" };
            int i = 0;
            using (XmlReader reader = XmlReader.Create(@"path"))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        switch (reader.Name.ToString())
                        {
                            case "firstname":
                                con[info[i]] = reader.ReadString();
                                i++;
                                break;
                            case "lastname":
                                con[info[i]] = reader.ReadString();
                                i++;
                                break;
                            case "email":
                                con[info[i]] = reader.ReadString();
                                i = 0;
                                crmService.Create(con);
                                break;
                        }
                    }
                }
            }
        }
    }
}
so far my Code only creates the Contacs. I did extend it already but it was more mess than a success.
My idea was to get to the type "Lookup" somehow and create an account from the Information in the child nodes. Then link the accounts to the contacs. (company2 exists already. I just Need to link that to the second contact.
Any Ideas what I Need to look up to do this?

*This post is locked for comments

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Create Contacs+Accounts and link them. From XML to Visual Studio to CRM

    In addition to that you can use retrievemultiple method to check account exists or not, if not can create it and Create method returns guid of the new record created, once you have the GUID you can use it for contact create.

    for setting entity reference you can sue

    con["lookupfield"]=new EntityReference("entityname",GUID);

    Hope it will help.

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: Create Contacs+Accounts and link them. From XML to Visual Studio to CRM

    Hi learningsmcrm,

    Your code is overcomplicating record creation. Look at this sample to learn how to create, retrieve, update, and delete records using late bounding. Also, check out this article on how to use the Entity class for creating, updating and deleting records.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans