Skip to content

Create Examples

David Yack edited this page Jan 27, 2017 · 3 revisions

C# example of creating one record and then multiple related ones.

In this example an Account is created, so is the Primary Contact, an Opportunity and a Task is created on the Opportunity. All this is done in one call to the API.

 dynamic account = new ExpandoObject();
 account.name = "Test Account " + DateTime.Now.ToString();

 dynamic primaryContact = new ExpandoObject();
 primaryContact.firstname = "John";
 primaryContact.lastname = "Smith;";
 account.primarycontactid = primaryContact;

 dynamic opportunity = new ExpandoObject();
 opportunity.name = "OP for " + account.name;
 account.opportunity_customer_accounts = new object[] { opportunity };
 dynamic task = new ExpandoObject();

 task.subject = "Task for opportunity";
 opportunity.Opportunity_Tasks = new object[] { task };

 Guid createdID = await api.Create("accounts", account);

JavaScript/NodeJS example of creating a task and associating it with an Opportunity

Notice the use of odata.bind to set the N:1 to the opportunity - the name regardingobjectid_opportunity was found using the API metadata and reviewing the navigation properties

var newTask = {"subject":"Follow-up Task",
               "scheduledstart":"2016-05-26T06:00:00.000Z",
               "prioritycode":2,
               "regardingobjectid_opportunity@odata.bind":"/opportunities(BE0D0283-5BF2-E311-945F-6C3BE5A8DD64)"};

crmAPI.Create("tasks", newTask)
		.then(function(result) {
}, function(error){ console.log(error)});

JavaScript/NodeJS example of creating a note with an image

   var note = {
          subject:'test',
          filename:'qrcode.jpg',
          documentbody:'base64 image data here',
          mimetype:"image/jepg"

      };
      crmAPI.Create("annotations",note).then(function(result) {console.log(result)},
                function(error){console.log(error)});
    });