NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN")), "true")
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  purl = NOT_CRAN
)

First, load the rdynamicscrm package and authenticate.

suppressMessages(suppressWarnings(library(dplyr)))
library(rdynamicscrm)
rdynamicscrm_setup <- readRDS(here::here("tests", "testthat", "rdynamicscrm_setup.rds"))
suppressMessages(dyn_auth(url = rdynamicscrm_setup$url, 
                          username = rdynamicscrm_setup$username, 
                          password = rdynamicscrm_setup$password))
suppressMessages(suppressWarnings(library(dplyr)))
library(rdynamicscrm)
dyn_auth(url = "https://test.ztcrm.org/",
         username = "test@live.com", 
         password = "{PASSWORD_HERE}")

The dyn_auth() function will obtain a cipher and secret key that will be embedded in the header of each SOAP API call to securely access the CRM instance. After obtaining these authentication credentials, you can check your connectivity by looking at the information returned about the current user. It should be information about you!

# pull down information of person logged in
# it's a simple easy call to get started 
# and confirm a connection to the APIs
me <- dyn_whoami()
my_info <- dyn_retrieve(me$UserId, entity_name = "systemuser", 
                        all_attributes = TRUE)
sprintf("Disabled?: %s", my_info$isdisabled)

Create

MS Dynamics CRM has entities and those entities contain records. One default entity is the "contact" entity. This example shows how to create two records in the contact entity.

n <- 2
new_contacts <- tibble(firstname = rep("Test", n),
                       lastname = paste0("Contact-Create-", 1:n))
created_records <- dyn_create(new_contacts, entity_name="contact")
created_records

Query

MS Dynamics CRM has proprietary form of SQL called FetchXML. FetchXML is a powerful tool that allows you to return the attributes of records on almost any entity in MS Dynamics CRM including accounts, contacts, custom entities and more!

For simple "SELECT" queries you only need to specify the entity and the fields.

queried_records <- dyn_query(entity_name = "contact",
                             attributes = c("modifiedon", "donotbulkemail"), top=3)
queried_records

Below is an example where we grab the two contact records we just created using FetchXML to specifically target those records.

my_fetchxml <- '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
                  <entity name="contact" >
                    <attribute name="firstname" />
                    <attribute name="lastname" />
                    <filter type="or">
                      <condition attribute="lastname" operator="eq" value="Contact-Create-1" />
                      <condition attribute="lastname" operator="eq" value="Contact-Create-2" />
                    </filter>
                  </entity>
                </fetch>'  
queried_records <- dyn_query(fetchxml = my_fetchxml)
queried_records

Update

After creating records you can update them using dyn_update(). Updating a record requires you to pass the MS Dynamics CRM Id of the record. MS Dynamics CRM creates a GUID (globally unique identifier) on each record and uses that to know which record to attach the update information you provide. Simply include a field or column in your update dataset called "Id" and the information will be matched. Here is an example where we update each of the records we created earlier with a new first name called "TestTest".

# Update some of those records
queried_records <- queried_records %>%
  mutate(firstname = "TestTest") %>% 
  rename(id=contactid)

updated_records <- dyn_update(queried_records, entity_name="contact")
updated_records

Delete

Records can easily be deleted individually or in bulk by passing a vector of IDs to the dyn_delete() function. Here we will delete the two records that we created in this example.

ids_to_delete <- updated_records$id
deleted_records <- dyn_delete(ids_to_delete, entity_name="contact")
deleted_records


StevenMMortimer/rdynamicscrm documentation built on July 9, 2019, 11:37 a.m.