function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
guru87guru87 

Creating a new contact based on an account using the ForceTK library

Dear Salesforce experts,

 

Below, I have a javascript function that adds/updates an account via the ForceTK library. This function creates a new account and contact based on an input form.

 

The issue I am facing is that I am trying to create a contact under the new account that was created, but wasn’t sure how to query the Id of the account that was created in order to set the AccountId field on the Contact. I have highlighted this line of code and it would be great if you could provide some advice as to what the proper syntax is.

 

function addUpdateAccount(e){

                e.preventDefault();

                var cId = $j('#accountId').val();

                var record = Accounts.findRecordById(cId);

                var cRecord;   

                if(record == null) { //new record

                    record = Accounts.create(['Id']);

                    cRecord = Contacts.create();

                }

                record.Name = $j('#name').val();

                record.Status__c = $j('#status').val();

                record.Phone = $j('#phone').val();

                record.Description = $j('#description').val();

               

                Accounts.sync(record);

               

                cRecord.FirstName = $j('#fName').val();

                cRecord.LastName = $j('#lName').val();

                cRecord.Email = $j('#email').val();

                cRecord.AccountId = record.Id;

               

                Contacts.sync(cRecord,successCallback);

                $j('#success').html('Save Successful');

            }

 

Thank you in advance and look forward to your responses.

 

Best Regards,