Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

Help creating Account/Primary Contact using Client SDK

Jump to Best Answer
  • 1.  Help creating Account/Primary Contact using Client SDK

    Posted Aug 09, 2019 09:38 AM
    All,

    I am trying to create an Account and associated Primary Contact using javascript and the CRM client SDK.  I can create the Account and it sets the Primary Contact correctly, however the Contact record is NOT associated to the Account (i.e. Contact doesn' t show up in the Account's Contacts subgrid. Account Name field is not set to the Account it was created from when I open the Contact record). Is this the expected behavior?  I was expecting that the Contact would be associated to the Account.  Is there a way to accomplish this with one call or do I need to create both entities independently and updated associations?

    Here is the code I am using and some screen shots of what I am seeing:
    var data = {
                    "name": jsonCompany.name,
                    "address1_line1": jsonCompany.street,
                    "address1_city": jsonCompany.city,
                    "address1_stateorprovince": jsonCompany.state,
                    "address1_postalcode": postalCode,
                    "telephone1": jsonCompany.phone,
                    "numberofemployees": jsonCompany.location_employee_count,
                    "sic": jsonCompany.primary_sic_code_id,
                    "hsl_SIC@odata.bind": "/hsl_sics(" + hslSicId + ")",
                    "address1_latitude": jsonCompany.latitude,
                    "address1_longitude": jsonCompany.longitude,
                    "hsl_ic_creditscore": jsonCompany.credit_rating_code,
                    "hsl_ic_squarefootage": jsonCompany.labels.square_footage,
                    "hsl_abinumber": jsonCompany.infogroup_id,
                    "accountnumber": ein,
                    "transactioncurrencyid@odata.bind": "/transactioncurrencies(" + _defaultCurrencyId + ")",
                    "primarycontactid": {
                        "firstname": jsonCompany.primary_contact.first_name,
                        "lastname": jsonCompany.primary_contact.last_name,
                        "hsl_jobtitlelist": jobTitle,
                        "jobtitle": secondaryTitle
                    }
                };
    
                Xrm.WebApi.createRecord("account", data).then(
                    function success(result) {
                        viewAccount(result.id);
    
                        if (_busyDialog && _busyDialog.isOpen())
                            _busyDialog.close();
                    },
                    function (error) {
                        alert(error.message);
                    });


    Account record:

    Contact record:


    ------------------------------
    Neal Schlatter
    .NET Developer
    UniFirst Corporation
    Indianapolis IN
    ------------------------------
    Conference-CRMUG_200x200


  • 2.  RE: Help creating Account/Primary Contact using Client SDK
    Best Answer

    TOP CONTRIBUTOR
    Posted Aug 09, 2019 10:36 AM
    Neal,

    I can't say that this is a bug. Reference of newly created contact was added to primary contact field of account created - so nothing is wrong here.
    My recommendation if you want to make everything work in one call - add a realtime workflow that will handle insert of account, check primary contact field and if it is not blank and account of it is blank - populate it with account record.

    ------------------------------
    Andrew Butenko
    ------------------------------

    Conference-CRMUG_200x200


  • 3.  RE: Help creating Account/Primary Contact using Client SDK

    Posted Aug 09, 2019 11:33 AM
    Hi Andrew,

    That makes sense. It sounds like this is the expected behavior. Since the Contact is being created through the Account's navigation property, I was expecting the Contact to be associated to that Account. I actually really like your proposed solution. For now, I am making the association on the client. Thanks!

    var accountData = {
                    "name": jsonCompany.name,
                    "address1_line1": jsonCompany.street,
                    "address1_city": jsonCompany.city,
                    "address1_stateorprovince": jsonCompany.state,
                    "address1_postalcode": postalCode,
                    "telephone1": jsonCompany.phone,
                    "numberofemployees": jsonCompany.location_employee_count,
                    "sic": jsonCompany.primary_sic_code_id,
                    "hsl_SIC@odata.bind": "/hsl_sics(" + hslSicId + ")",
                    "address1_latitude": jsonCompany.latitude,
                    "address1_longitude": jsonCompany.longitude,
                    "hsl_ic_creditscore": jsonCompany.credit_rating_code,
                    "hsl_ic_squarefootage": jsonCompany.labels.square_footage,
                    "hsl_abinumber": jsonCompany.infogroup_id,
                    "accountnumber": ein,
                    "transactioncurrencyid@odata.bind": "/transactioncurrencies(" + _defaultCurrencyId + ")"
                };
    
                Xrm.WebApi.createRecord("account", accountData).then(
                    function success(result) {
                        var accountId = result.id;
    
                        var contactData = {
                            "parentcustomerid_account@odata.bind": "/accounts(" + accountId + ")",
                            // not possible or doing wrong?
                            //"account_primary_contact@odata.bind": ["/accounts(" + accountId + ")"],
                            "firstname": jsonCompany.primary_contact.first_name,
                            "lastname": jsonCompany.primary_contact.last_name,
                            "hsl_jobtitlelist": jobTitle,
                            "jobtitle": secondaryTitle
                        };
    
                        Xrm.WebApi.createRecord("contact", contactData).then(
                            function success(result) {
                                var contactId = result.id;
    
                                var accountPrimaryContactData = {
                                    "primarycontactid@odata.bind": "/contacts(" + contactId + ")" 
                                };
    
                                Xrm.WebApi.updateRecord("account", accountId, accountPrimaryContactData).then(
                                    function success(result) {
                                        viewAccount(accountId);
    
                                        if (_busyDialog && _busyDialog.isOpen())
                                            _busyDialog.close();
                                    },
                                    function (error) {
                                        alert(error.message);
                                    });
                            },
                            function (error) {
                                alert(error.message);
                            });
                    },
                    function (error) {
                        alert(error.message);
                    });


    ------------------------------
    Neal Schlatter
    .NET Developer
    UniFirst Corporation
    Indianapolis IN
    ------------------------------

    Conference-CRMUG_200x200


If you've found this thread useful, dive deeper into User Group community content by role