public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "salesforce/objects/createRecord")] HttpRequest req,
            ILogger log)
        {
            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject <QueryRequest>(requestBody);

            ForceClient client = new ForceClient(data);
            ////var result = await client.GetObjectBasicInfo(data.ObjectTypeName);
            //CreateRecord<T>(string sObjectTypeName, T sObject, Dictionary<string, string> customHeaders = null)
            var result = await client.CreateRecord(data.sObjectTypeName, data.sObject, null);

            return((ActionResult) new OkObjectResult(result));
        }
Beispiel #2
0
        private static async Task RunTest()
        {
            AuthInfo             authInfo = GetAuthInfo();
            AuthenticationClient auth     = new AuthenticationClient(authInfo.ApiVersion);

            try
            {
                await auth.UsernamePasswordAsync(authInfo.ClientId, authInfo.ClientSecret, authInfo.Username, authInfo.Password, authInfo.TokenRequestEndpoint);

                Console.WriteLine("Successfully connected to Salesforce");
            }
            catch (ForceAuthException ex)
            {
                Console.WriteLine("ForceAuthException: " + ex.Message);
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine("General Exception at login: "******"SELECT Id, Name, SystemModstamp, Account.Id, Account.Name, Account.SystemModstamp FROM Contact", false);

                List <CustomAccount> customAccounts = await client.Query <CustomAccount>("SELECT Id, CustomerPriority__c FROM Account", false);

                //Using a dynamic object
                dynamic dynoObject = new ExpandoObject();
                dynoObject.Name        = "Test Account";
                dynoObject.Description = "Test Account description";
                CreateResponse resp = await client.CreateRecord <dynamic>("Account", dynoObject);
            }
            catch (ForceApiException ex)
            {
                Console.WriteLine("ForceApiException: " + ex.Message);
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine("General Exception: " + ex.Message);
                return;
            }

            return;
        }
Beispiel #3
0
        public async Task CrudChain()
        {
            ForceClient client = await forceClientFixture.GetForceClient();

            //create new object
            SfAccount newAccount  = new SfAccount();
            string    accountName = string.Format("Test Object {0}", Guid.NewGuid().ToString());

            newAccount.Name = accountName;

            CreateResponse createResp = await client.CreateRecord <SfAccount>(SfAccount.SObjectTypeName, newAccount);

            Assert.True(!string.IsNullOrEmpty(createResp.Id), "Failed to create new object");

            //get newly created
            string    newAccountId = createResp.Id;
            SfAccount account      = await client.GetObjectById <SfAccount>(SfAccount.SObjectTypeName, newAccountId);

            Assert.True(account != null, "Failed to retrieve new object");

            //update object
            string description = string.Format("Test Description {0}", Guid.NewGuid().ToString());

            account.Description = description;
            await client.UpdateRecord <SfAccount>(SfAccount.SObjectTypeName, account.Id, account);

            //get newly updated
            SfAccount udpatedAccount = await client.GetObjectById <SfAccount>(SfAccount.SObjectTypeName, newAccountId);

            Assert.True(udpatedAccount != null, "Failed to retrieve udpated object");
            Assert.Equal(description, udpatedAccount.Description);

            //delete
            await client.DeleteRecord(SfAccount.SObjectTypeName, newAccountId);

            //use queryall to find deleted record
        }
Beispiel #4
0
        private static async Task RunTest()
        {
            AuthInfo             authInfo = GetAuthInfo();
            AuthenticationClient auth     = new AuthenticationClient(authInfo.ApiVersion);

            try
            {
                await auth.UsernamePasswordAsync(authInfo.ClientId, authInfo.ClientSecret, authInfo.Username, authInfo.Password, authInfo.TokenRequestEndpoint);

                Console.WriteLine("Successfully connected to Salesforce");
            }
            catch (ForceAuthException ex)
            {
                Console.WriteLine("ForceAuthException: " + ex.Message);
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine("General Exception at login: "******"SELECT Id, Name, SystemModstamp, Account.Id, Account.Name, Account.SystemModstamp FROM Contact", false);

                List <CustomAccount> customAccounts = await client.Query <CustomAccount>("SELECT Id, CustomerPriority__c FROM Account", false);

                //Using a dynamic object
                dynamic dynoObject = new ExpandoObject();
                dynoObject.Name        = "Test Account";
                dynoObject.Description = "Test Account description";
                CreateResponse resp = await client.CreateRecord <dynamic>("Account", dynoObject);

                // Asynchronous large result sets and batching:
                // Query<T> method will retrieve the full result set before returning.
                // In cases where you are working with large result sets, you may want to retrieve the batches asynchronously for better performance.

                // First create the async enumerable. At this point, no query has been executed.
                // batchSize can be omitted to use the default (usually 2000), or given a custom value between 200 and 2000.
                IAsyncEnumerable <SfContact> contactsEnumerable = client.QueryAsync <SfContact>("SELECT Id, Name FROM Contact ", batchSize: 200);

                // Get the enumerator, in a using block for proper disposal
                using (IAsyncEnumerator <SfContact> contactsEnumerator = contactsEnumerable.GetEnumerator())
                {
                    // MoveNext() will execute the query and get the first batch of results.
                    // Once the inital result batch has been exhausted, the remaining batches, if any, will be retrieved.
                    while (await contactsEnumerator.MoveNext())
                    {
                        SfContact contact = contactsEnumerator.Current;
                        // process your results
                    }
                }
            }
            catch (ForceApiException ex)
            {
                Console.WriteLine("ForceApiException: " + ex.Message);
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine("General Exception: " + ex.Message);
                return;
            }

            return;
        }
Beispiel #5
0
        public async Task CreateAnUpdateMultiple()
        {
            ForceClient client = await forceClientFixture.GetForceClient();

            //create new object
            SfAccount firstAccount = new SfAccount()
            {
            };
            string firstAccountName = string.Format("Test Object {0}", Guid.NewGuid().ToString());

            firstAccount.Name = firstAccountName;

            CreateResponse createResp = await client.CreateRecord <SfAccount>(SfAccount.SObjectTypeName, firstAccount);

            string firstAccountId = createResp.Id;

            Assert.True(!string.IsNullOrEmpty(createResp.Id), "Failed to create new object");

            //get new object
            firstAccount = await client.GetObjectById <SfAccount>(SfAccount.SObjectTypeName, firstAccountId);

            //create second new object for testing update multiple
            SfAccount secondAccount     = new SfAccount();
            string    secondAccountName = string.Format("Test Object {0}", Guid.NewGuid().ToString());

            secondAccount.Name = secondAccountName;

            CreateResponse secondCreateResp = await client.CreateRecord <SfAccount>(SfAccount.SObjectTypeName, secondAccount);

            string secondAccountId = secondCreateResp.Id;

            Assert.True(!string.IsNullOrEmpty(secondCreateResp.Id), "Failed to create second new object");

            //get new object
            secondAccount = await client.GetObjectById <SfAccount>(SfAccount.SObjectTypeName, secondAccountId);

            //test update multiple
            string firstUpdatedDescription  = string.Format("Test Description {0}", Guid.NewGuid().ToString());
            string secondUpdatedDescription = string.Format("Test Description {0}", Guid.NewGuid().ToString());

            firstAccount.Description  = firstUpdatedDescription;
            secondAccount.Description = secondUpdatedDescription;

            try
            {
                List <UpdateMultipleResponse> responses = await client.UpdateRecords(new List <SObject>() { firstAccount, secondAccount }, true);

                Assert.True(responses.All(r => r.Success), "Failed to update multiple objects");
            }
            catch (Exception ex)
            {
                throw ex;
            }

            //get newly updated objects
            string    secondNewAccountId  = secondCreateResp.Id;
            SfAccount firstUpdatedAccount = await client.GetObjectById <SfAccount>(SfAccount.SObjectTypeName, firstAccountId);

            SfAccount secondUpdatedAccount = await client.GetObjectById <SfAccount>(SfAccount.SObjectTypeName, secondAccountId);

            Assert.True(firstUpdatedAccount != null && secondUpdatedAccount != null, "Failed to retrieve multiple updated objects");
            Assert.Equal(firstUpdatedDescription, firstUpdatedAccount.Description);
            Assert.Equal(secondUpdatedDescription, secondUpdatedAccount.Description);

            //delete
            await client.DeleteRecord(SfAccount.SObjectTypeName, firstAccountId);

            await client.DeleteRecord(SfAccount.SObjectTypeName, secondNewAccountId);

            //use queryall to find deleted record
        }