private void upsertSample() { // TODO Auto-generated method stub // Verify that we are already authenticated, if not // call the login function to do so if (!loggedIn) { if (!login()) { return; } } try { partner.DescribeSObjectResult dsr = binding.describeSObject("Account"); System.Collections.Hashtable fieldMap = this.makeFieldMap(dsr.fields); if (!fieldMap.ContainsKey("External_Id__c")) { Console.WriteLine("\n\nNOTICE: To run the upsert sample you need \nto add a custom field named \nExternal_Id to the account object. \nSet the size of the field to 8 characters \nand be sure to check the 'External Id' checkbox."); } else { //First, we need to make sure the test accounts do not exist. partner.QueryResult qr = binding.query("Select Id From Account Where External_Id__c = '11111111' or External_Id__c = '22222222'"); if (qr.size > 0) { partner.sObject[] accounts = (partner.sObject[])qr.records; //Get the ids String[] ids = new String[accounts.Length]; for (int i = 0; i < ids.Length; i++) { ids[i] = accounts[i].Id; } //Delete the accounts binding.delete(ids); } //Create a new account using create, we wil use this to update via upsert //We will set the external id to be ones so that we can use that value for the upsert partner.sObject newAccount = new partner.sObject(); newAccount.type = "Account"; newAccount.Any = new System.Xml.XmlElement[] { GetNewXmlElement("Name", "Account to update"), GetNewXmlElement("External_Id__c", "11111111") }; binding.create(new partner.sObject[] { newAccount }); //Now we will create an account that should be updated on insert based //on the external id field. partner.sObject updateAccount = new partner.sObject(); updateAccount.type = "Account"; updateAccount.Any = new System.Xml.XmlElement[] { GetNewXmlElement("Website", "http://www.website.com"), GetNewXmlElement("External_Id__c", "11111111") }; // This account is meant to be new partner.sObject createAccount = new partner.sObject(); createAccount.type = "Account"; createAccount.Any = new System.Xml.XmlElement[] { GetNewXmlElement("Name", "My Company, Inc"), GetNewXmlElement("External_Id__c", "22222222") }; //We have our two accounts, one should be new, the other should be updated. try { // Invoke the upsert call and save the results. // Use External_Id custom field for matching records partner.UpsertResult[] upsertResults = binding.upsert("External_Id__c", new partner.sObject[] { createAccount, updateAccount }); for (int i = 0; i < upsertResults.Length; i++) { partner.UpsertResult result = upsertResults[i]; if (result.success) { Console.WriteLine("\nUpsert succeeded."); Console.WriteLine((result.created ? "Inserted" : "Updated") + " account, id is " + result.id); } else { Console.WriteLine("The Upsert failed because: " + result.errors[0].message); } } } catch (System.Web.Services.Protocols.SoapException ex) { Console.WriteLine("Error from web service: " + ex.Message); } } Console.WriteLine("\nPress the RETURN key to continue..."); Console.ReadLine(); } catch (System.Web.Services.Protocols.SoapException ex) { Console.WriteLine("Error merging account: " + ex.Message + "\nHit return to continue..."); Console.ReadLine(); } }
private String createAndDeleteAnAccount(String accountName) { String returnId = null; partner.sObject acct = new partner.sObject(); acct.type = "Account"; acct.Any = new System.Xml.XmlElement[] { GetNewXmlElement("Name", (accountName == null) ? "QueryAll Sample" : accountName) }; try { //We are only creating one account so we can index the return array directly partner.SaveResult sr = binding.create(new partner.sObject[] { acct })[0]; if (sr.success) { acct.Id = sr.id; //Ok, now we will delete that account partner.DeleteResult dr = binding.delete(new String[] { acct.Id })[0]; if (!dr.success) { Console.WriteLine("The web service would not let us delete the account: \n" + dr.errors[0].message + "\nHit return to continue..."); Console.ReadLine(); } else { returnId = acct.Id; } } else { Console.WriteLine("The web service would not let us create the account: \n" + sr.errors[0].message + "\nHit return to continue..."); Console.ReadLine(); } } catch (System.Web.Services.Protocols.SoapException e) { Console.WriteLine("Error creating test account: " + e.Message + "\nHit return to continue..."); Console.ReadLine(); } return returnId; }
private void mergeSample() { // TODO Auto-generated method stub // Verify that we are already authenticated, if not // call the login function to do so if (!loggedIn) { if (!login()) { return; } } try { partner.sObject masterAccount = new partner.sObject(); masterAccount.type = "Account"; masterAccount.Any = new System.Xml.XmlElement[] { GetNewXmlElement("Name", "MasterAccount") }; partner.SaveResult masterAccountSaveResult; masterAccountSaveResult = binding.create(new partner.sObject[] { masterAccount })[0]; masterAccount.Id = masterAccountSaveResult.id; System.Xml.XmlElement[] masterAny = masterAccount.Any; System.Array.Resize(ref masterAny, (masterAccount.Any.Length + 1)); masterAny[masterAny.Length - 1] = GetNewXmlElement("Description", "Old description"); masterAccount.Any = masterAny; partner.sObject accountToMerge = new partner.sObject(); accountToMerge.type = "Account"; accountToMerge.Any = new System.Xml.XmlElement[] { GetNewXmlElement("Name", "AccountToMerge"), GetNewXmlElement("Description", "Duplicate account") }; partner.SaveResult accountToMergeSaveResult = binding.create(new partner.sObject[] { accountToMerge })[0]; //Attach a note, which will get re-parented partner.sObject note = new partner.sObject(); note.type = "Note"; note.Any = new System.Xml.XmlElement[] { GetNewXmlElement("ParentId", accountToMergeSaveResult.id), GetNewXmlElement("Body", "This note will be moved to the MasterAccount during merge"), GetNewXmlElement("Title", "Test note to be reparented.") }; partner.SaveResult noteSave = binding.create(new partner.sObject[] { note })[0]; partner.MergeRequest mr = new partner.MergeRequest(); //Perform an update on the master record as part of the merge: masterAccount.Any[masterAccount.Any.Length - 1] = GetNewXmlElement("Description", "Was merged"); mr.masterRecord = masterAccount; mr.recordToMergeIds = (new String[] { accountToMergeSaveResult.id }); partner.MergeResult result = binding.merge(new partner.MergeRequest[] { mr })[0]; Console.WriteLine("Merged " + result.success + " got " + result.updatedRelatedIds.Length + " updated child records\nHit return to continue."); Console.ReadLine(); } catch (System.Web.Services.Protocols.SoapException e) { Console.WriteLine("Error merging account: " + e.Message + "\nHit return to continue..."); Console.ReadLine(); } }
private void updateAccountSample() { //Verify that we are already authenticated, if not //call the login function to do so if (!loggedIn) { if (!login()) return; } try { //check to see if there are any accounts created in this session if (accounts == null) { Console.WriteLine("\nUpdate operation not completed. You will need to create an account during this session to run the update sample."); Console.Write("\nHit return to continue..."); Console.ReadLine(); return; } //create the account object to hold our changes partner.sObject updateAccount = new partner.sObject(); //need to have the id so that web service knows which account to update updateAccount.Id = accounts[0]; //set a new value for the name property updateAccount.Any = new System.Xml.XmlElement[] { this.GetNewXmlElement("Name", "New Account Name from Update Sample") }; updateAccount.type = "Account"; //create one that will throw an error partner.sObject errorAccount = new partner.sObject(); errorAccount.Id = "S:DLFKJLFKJ"; errorAccount.Any = new System.Xml.XmlElement[] { this.GetNewXmlElement("Name", "Error") }; errorAccount.fieldsToNull = new string[] { "Name" }; errorAccount.type = "Account"; //call the update passing an array of object partner.SaveResult[] saveResults = binding.update(new partner.sObject[] { updateAccount, errorAccount }); //loop through the results, checking for errors for (int j = 0; j < saveResults.Length; j++) { Console.WriteLine("Item: " + j); if (saveResults[j].success) Console.WriteLine("An account with an id of: " + saveResults[j].id + " was updated.\n"); else { Console.WriteLine("Item " + j.ToString() + " had an error updating."); Console.WriteLine(" The error reported was: " + saveResults[j].errors[0].message + "\n"); } } } catch (Exception ex) { Console.WriteLine("\nFailed to succesfully update an account, error message was: \n" + ex.Message); } Console.WriteLine("\nHit return to continue..."); Console.ReadLine(); }
private void processSample() { // Verify that we are already authenticated, if not // call the login function to do so if (!loggedIn) { if (!login()) { return; } } try { //First step is to create an account that matches the approval process criteria partner.sObject acct = new partner.sObject(); acct.type = "Account"; acct.Any = new System.Xml.XmlElement[] { GetNewXmlElement("Name", "API Approval Sample") }; acct.Id = binding.create(new partner.sObject[] { acct })[0].id; // Next step is to submit the account for approval using a ProcessSubmitRequest partner.ProcessSubmitRequest psr = new partner.ProcessSubmitRequest(); psr.objectId = acct.Id; psr.comments = "This approval request was initiated from the API."; partner.ProcessResult p_res = binding.process(new partner.ProcessRequest[] { psr })[0]; if (p_res.success) { //Since the submission was successful we can now approve or reject it with // a ProcessWorkItmeRequest partner.ProcessWorkitemRequest pwr = new partner.ProcessWorkitemRequest(); pwr.action = "Approve"; pwr.comments = "This request was approved from the API."; pwr.workitemId = p_res.newWorkitemIds[0]; p_res = binding.process(new partner.ProcessRequest[] { pwr })[0]; if (p_res.success) { Console.WriteLine("Successfully submitted and then approved an approval request."); } else { Console.WriteLine("Error approving the work item: " + p_res.errors[0].message); } } else { Console.WriteLine("Error submitting the account for approval: " + p_res.errors[0].message); } } catch (System.Web.Services.Protocols.SoapException e) { Console.WriteLine("The API returned a fault: " + e.Message); } Console.WriteLine("\nHit the enter key to continue..."); Console.ReadLine(); }
/// <summary> /// Use Partner API to create new SFDC records /// </summary> private static void CreatePartnerRecords() { Console.WriteLine("Creating an account record with the Partner API ..."); //set query endpoint to value returned by login request EndpointAddress apiAddr = new EndpointAddress(serverUrl); partner.SessionHeader header = new partner.SessionHeader(); header.sessionId = sessionId; //create service client to call API endpoint using (partner.SoapClient queryClient = new partner.SoapClient("Soap1", apiAddr)) { partner.sObject account = new partner.sObject(); account.type = "Account"; //create XML containers for necessary XML document and elements XmlDocument rootDoc = new XmlDocument(); XmlElement[] accountFields = new XmlElement[3]; XElement[] test = new XElement[2]; test[0] = new XElement("node1", "value"); //add fields accountFields[0] = rootDoc.CreateElement("Name"); accountFields[0].InnerText = "DevForce06"; accountFields[1] = rootDoc.CreateElement("AccountNumber"); accountFields[1].InnerText = "1004441239"; accountFields[2] = rootDoc.CreateElement("AnnualRevenue"); accountFields[2].InnerText = "4000000"; //set object property to array account.Any = accountFields; partner.SaveResult[] results; queryClient.create( header, //sessionheader null, //calloptions null, //assignmentruleheader null, //mruheader null, //allowfieldtruncationheader null, //disablefeedtrackingheader null, //streamingenabledheader null, //allornothingheader null, //debuggingheader null, //packageversionheader null, //emailheader new partner.sObject[] { account }, //new accounts out results //result of create operation ); //only added one item, so looking at first index of results object if (results[0].success) { Console.WriteLine("Account successfully created."); } Console.ReadLine(); } }
private void emptyRecycleBinSample() { //Verify that we are already authenticated, if not //call the login function to do so if (!loggedIn) { if (!login()) return; } try { Console.Write("\nEmpty the recycle bin? (Y/N) "); String response = Console.ReadLine(); if (response.ToLower().Equals("y")) { // We will create some accounts and then delete them and use // the ids for the emptyRecycleBin method. partner.sObject[] accounts = new partner.sObject[10]; for (int i = 0; i < accounts.Length; i++) { accounts[i] = new partner.sObject(); accounts[i].Any = new System.Xml.XmlElement[1]; accounts[i].Any[0] = GetNewXmlElement("Name", "Test account " + i); accounts[i].type = "Account"; } // save the new accounts String[] createdItemIds = new String[10]; partner.SaveResult[] sr = binding.create(accounts); for (int i = 0; i < sr.Length; i++) { if (sr[i].success) { createdItemIds[i] = sr[i].id; } } // now, delete them partner.DeleteResult[] dr = binding.delete(createdItemIds); String[] deletedItemIds = new String[sr.Length]; for (int i = 0; i < dr.Length; i++) { if (dr[i].success) { deletedItemIds[i] = dr[i].id; } } //call the emptyRecycleBin method partner.EmptyRecycleBinResult[] results = binding.emptyRecycleBin(deletedItemIds); for (int i = 0; i < results.Length; i++) { if (results[i].success) { Console.WriteLine("Lead with id: " + results[i].id + " was successfully queued for permanant delete."); } else { Console.WriteLine("A problem occurred queueing item with id: " + results[i].id + " for permanent delete."); } } Console.WriteLine("Empty recycle bin finished."); Console.ReadLine(); } } catch (Exception ex3) { Console.WriteLine("ERROR: emptying recycle bin.\n" + ex3.Message); } }
private void createTaskSample() { //Verify that we are already authenticated, if not //call the login function to do so if (!loggedIn) { if (!login()) return; } try { //create an array to create 4 items in one call partner.sObject[] taskarray = new partner.sObject[3]; for (int x = 0; x < taskarray.Length; x++) { //Declare a new task object to hold our values partner.sObject task = new partner.sObject(); int index = 0; System.Xml.XmlElement[] tsk = new System.Xml.XmlElement[5]; if (contacts != null) tsk = new System.Xml.XmlElement[tsk.Length + 1]; if (accounts != null) tsk = new System.Xml.XmlElement[tsk.Length + 1]; //make sure we get some errors on records 3 and 4 if (x > 1) { tsk = new System.Xml.XmlElement[tsk.Length + 1]; tsk[index++] = GetNewXmlElement("OwnerId", "DSF:LJKSDFLKJ"); } //Set the appropriate values on the task tsk[index++] = GetNewXmlElement("ActivityDate", GetDateTimeSerialized(DateTime.Now)); tsk[index++] = GetNewXmlElement("Description", "Get in touch with this person"); tsk[index++] = GetNewXmlElement("Priority", "Normal"); tsk[index++] = GetNewXmlElement("Status", "Not Started"); tsk[index++] = GetNewXmlElement("Subject", "Setup Call"); //The two lines below illustrate associating an object with another object. If //we have created an account and/or a contact prior to creating the task, we will //just grab the first account and/or contact id and place it in the appropriate //reference field. WhoId can be a reference to a contact or a lead or a user. //WhatId can be a reference to an account, campaign, case, contract or opportunity if (contacts != null) tsk[index++] = GetNewXmlElement("WhoId", contacts[0]); if (accounts != null) tsk[index++] = GetNewXmlElement("WhatId", accounts[0]); task.type = "Task"; taskarray[x] = task; } //call the create method passing the array of tasks as sobjects partner.SaveResult[] sr = binding.create(taskarray); for (int j = 0; j < sr.Length; j++) { if (sr[j].success) { Console.WriteLine("A task was create with an id of: " + sr[j].id); if (accounts != null) Console.WriteLine(" - the task was associated with the account you created with an id of " + accounts[0] + "."); if (tasks == null) { tasks = new string[] { sr[j].id }; } else { string[] tempTasks = null; tempTasks = new string[tasks.Length + 1]; for (int i = 0; i < tasks.Length; i++) tempTasks[i] = tasks[i]; tempTasks[tasks.Length] = sr[j].id; tasks = tempTasks; } } else { //there were errors during the create call, go through the errors //array and write them to the screen for (int i = 0; i < sr[j].errors.Length; i++) { //get the next error partner.Error err = sr[j].errors[i]; Console.WriteLine("Errors were found on item " + j.ToString()); Console.WriteLine("Error code is: " + err.statusCode.ToString()); Console.WriteLine("Error message: " + err.message); } } Console.WriteLine("\nCreate task successful."); } Console.Write("\nHit return to continue..."); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("\nFailed to succesfully create a task, error message was: \n" + ex.Message); Console.Write("\nHit return to continue..."); Console.ReadLine(); } }
private void createContactSample() { //Verify that we are already authenticated, if not //call the login function to do so if (!loggedIn) { if (!login()) return; } try { partner.sObject[] cons = new partner.sObject[1]; partner.sObject contact; for (int j = 0; j < cons.Length; j++) { contact = new partner.sObject(); int index = 0; System.Xml.XmlElement[] cont = new System.Xml.XmlElement[17]; if (accounts != null) { cont = new System.Xml.XmlElement[cont.Length + 1]; cont[index++] = GetNewXmlElement("AccountId", accounts[0]); } cont[index++] = GetNewXmlElement("AssistantName", "Jane"); cont[index++] = GetNewXmlElement("AssistantPhone", "777.777.7777"); cont[index++] = GetNewXmlElement("Department", "Purchasing"); cont[index++] = GetNewXmlElement("Description", "International IT Purchaser"); cont[index++] = GetNewXmlElement("Email", "*****@*****.**"); cont[index++] = GetNewXmlElement("Fax", "555.555.5555"); cont[index++] = GetNewXmlElement("MailingCity", "San Mateo"); cont[index++] = GetNewXmlElement("MailingCountry", "US"); cont[index++] = GetNewXmlElement("MailingState", "CA"); cont[index++] = GetNewXmlElement("MailingStreet", "1129 B Street"); cont[index++] = GetNewXmlElement("MailingPostalCode", "94105"); cont[index++] = GetNewXmlElement("MobilePhone", "888.888.8888"); cont[index++] = GetNewXmlElement("FirstName", "Joe"); cont[index++] = GetNewXmlElement("LastName", "Blow"); cont[index++] = GetNewXmlElement("Salutation", "Mr."); cont[index++] = GetNewXmlElement("Phone", "999.999.9999"); cont[index++] = GetNewXmlElement("Title", "Purchasing Director"); contact.Any = cont; contact.type = "Contact"; cons[j] = contact; } partner.SaveResult[] sr = binding.create(cons); for (int j = 0; j < sr.Length; j++) { if (sr[j].success) { Console.WriteLine("A contact was create with an id of: " + sr[j].id); if (accounts != null) Console.WriteLine(" - the contact was associated with the account you created with an id of " + accounts[0] + "."); if (contacts == null) { contacts = new string[] { sr[j].id }; } else { string[] tempContacts = null; tempContacts = new string[contacts.Length + 1]; for (int i = 0; i < contacts.Length; i++) tempContacts[i] = contacts[i]; tempContacts[contacts.Length] = sr[j].id; contacts = tempContacts; } } else { //there were errors during the create call, go through the errors //array and write them to the screen for (int i = 0; i < sr[j].errors.Length; i++) { //get the next error partner.Error err = sr[j].errors[i]; Console.WriteLine("Errors were found on item " + j.ToString()); Console.WriteLine("Error code is: " + err.statusCode.ToString()); Console.WriteLine("Error message: " + err.message); } } Console.Write("\nHit return to continue..."); Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine("\nFailed to execute query succesfully, error message was: \n" + ex.Message); Console.Write("\nHit return to continue..."); Console.ReadLine(); } }
private void createAccountSample() { //Verify that we are already authenticated, if not //call the login function to do so if (!loggedIn) { if (!login()) return; } try { partner.sObject account; partner.sObject[] accs = new partner.sObject[2]; for (int j = 0; j < accs.Length; j++) { account = new partner.sObject(); System.Xml.XmlElement[] acct = new System.Xml.XmlElement[14]; int index = 0; if (accounts == null) acct[index++] = GetNewXmlElement("AccountNumber", "0000000"); else acct[index++] = GetNewXmlElement("AccountNumber", "000000" + (accounts.Length + 1)); //account.setAnnualRevenue(new java.lang.Double(4000000.0)); acct[index++] = GetNewXmlElement("BillingCity", "Wichita"); acct[index++] = GetNewXmlElement("BillingCountry", "US"); acct[index++] = GetNewXmlElement("BillingState", "KA"); acct[index++] = GetNewXmlElement("BillingStreet", "4322 Haystack Boulevard"); acct[index++] = GetNewXmlElement("BillingPostalCode", "87901"); acct[index++] = GetNewXmlElement("Description", "World class hay makers."); acct[index++] = GetNewXmlElement("Fax", "555.555.5555"); acct[index++] = GetNewXmlElement("Industry", "Farming"); acct[index++] = GetNewXmlElement("Name", "Golden Straw"); acct[index++] = GetNewXmlElement("NumberOfEmployees", "40"); acct[index++] = GetNewXmlElement("Ownership", "Privately Held"); acct[index++] = GetNewXmlElement("Phone", "666.666.6666"); acct[index++] = GetNewXmlElement("Website", "www.oz.com"); account.type = "Account"; account.Any = acct; accs[j] = account; } //create the object(s) by sending the array to the web service partner.SaveResult[] sr = binding.create(accs); for (int j = 0; j < sr.Length; j++) { if (sr[j].success) { Console.Write(System.Environment.NewLine + "An account was create with an id of: " + sr[j].id); //save the account ids in a class array if (accounts == null) { accounts = new string[] { sr[j].id }; } else { string[] tempAccounts = null; tempAccounts = new string[accounts.Length + 1]; for (int i = 0; i < accounts.Length; i++) tempAccounts[i] = accounts[i]; tempAccounts[accounts.Length] = sr[j].id; accounts = tempAccounts; } } else { //there were errors during the create call, go through the errors //array and write them to the screen for (int i = 0; i < sr[j].errors.Length; i++) { //get the next error partner.Error err = sr[j].errors[i]; Console.WriteLine("Errors were found on item " + j.ToString()); Console.WriteLine("Error code is: " + err.statusCode.ToString()); Console.WriteLine("Error message: " + err.message); } } } Console.WriteLine("\nHit return to continue..."); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("\nFailed to create account, error message was: \n" + ex.Message); Console.Write("\nHit return to continue..."); Console.ReadLine(); } }
private void createLeadSample() { //Verify that we are already authenticated, if not //call the login function to do so if (!loggedIn) { if (!login()) return; } try { partner.sObject[] leads = new partner.sObject[1]; partner.sObject lead; lead = new partner.sObject(); int index = 0; System.Xml.XmlElement[] leadEls = new System.Xml.XmlElement[17]; leadEls[index++] = GetNewXmlElement("AnnualRevenue", System.Xml.XmlConvert.ToString(1000000.0)); leadEls[index++] = GetNewXmlElement("AnnualRevenueSpecified", System.Xml.XmlConvert.ToString(true)); leadEls[index++] = GetNewXmlElement("City", "San Francisco"); leadEls[index++] = GetNewXmlElement("Company", "Acme Anvils"); leadEls[index++] = GetNewXmlElement("Country", "United States"); leadEls[index++] = GetNewXmlElement("CurrencyIsoCode", "USD"); leadEls[index++] = GetNewXmlElement("Description", "This is a lead that can be converted."); leadEls[index++] = GetNewXmlElement("DoNotCall", System.Xml.XmlConvert.ToString(false)); leadEls[index++] = GetNewXmlElement("DoNotCallSpecified", System.Xml.XmlConvert.ToString(true)); Console.Write("\nPlease enter an email for the lead we are creating."); string email = Console.ReadLine(); if (email == null) leadEls[index++] = GetNewXmlElement("Email", "*****@*****.**"); else leadEls[index++] = GetNewXmlElement("Email", email); leadEls[index++] = GetNewXmlElement("Fax", "5555555555"); leadEls[index++] = GetNewXmlElement("FirstName", "Wiley"); leadEls[index++] = GetNewXmlElement("HasOptedOutOfEmail", System.Xml.XmlConvert.ToString(false)); leadEls[index++] = GetNewXmlElement("HasOptedOutOfEmailSpecified", System.Xml.XmlConvert.ToString(true)); leadEls[index++] = GetNewXmlElement("Industry", "Blacksmithery"); leadEls[index++] = GetNewXmlElement("LastName", "Coyote"); leadEls[index++] = GetNewXmlElement("LeadSource", "Web"); leadEls[index++] = GetNewXmlElement("MobilePhone", "4444444444"); leadEls[index++] = GetNewXmlElement("NumberOfEmployees", System.Xml.XmlConvert.ToString(30)); leadEls[index++] = GetNewXmlElement("NumberOfEmployeesSpecified", System.Xml.XmlConvert.ToString(true)); leadEls[index++] = GetNewXmlElement("NumberofLocations__c", System.Xml.XmlConvert.ToString(1.0)); leadEls[index++] = GetNewXmlElement("NumberofLocations__cSpecified", System.Xml.XmlConvert.ToString(true)); leadEls[index++] = GetNewXmlElement("Phone", "6666666666"); leadEls[index++] = GetNewXmlElement("PostalCode", "94105"); leadEls[index++] = GetNewXmlElement("Rating", "Hot"); leadEls[index++] = GetNewXmlElement("Salutation", "Mr."); leadEls[index++] = GetNewXmlElement("State", "California"); leadEls[index++] = GetNewXmlElement("Status", "Working"); leadEls[index++] = GetNewXmlElement("Street", "10 Downing Street"); leadEls[index++] = GetNewXmlElement("Title", "Director of Directors"); leadEls[index++] = GetNewXmlElement("Website", "www.acmeanvils.com"); lead.Any = leadEls; lead.type = "Lead"; leads[0] = lead; partner.SaveResult[] sr = binding.create(leads); for (int j = 0; j < sr.Length; j++) { if (sr[j].success) { Console.WriteLine("A lead was create with an id of: " + sr[j].id); } else { //there were errors during the create call, go through the errors //array and write them to the screen for (int i = 0; i < sr[j].errors.Length; i++) { //get the next error partner.Error err = sr[j].errors[i]; Console.WriteLine("Errors were found on item " + j.ToString()); Console.WriteLine("Error code is: " + err.statusCode.ToString()); Console.WriteLine("Error message: " + err.message); } } Console.Write("\nHit return to continue..."); Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine("\nFailed to execute query succesfully, error message was: \n" + ex.Message); Console.Write("\nHit return to continue..."); Console.ReadLine(); } }
/// <summary> /// Use Partner API to create new SFDC records /// </summary> private static void CreatePartnerRecords() { Console.WriteLine("Creating an account record with the Partner API ..."); //set query endpoint to value returned by login request EndpointAddress apiAddr = new EndpointAddress(serverUrl); partner.SessionHeader header = new partner.SessionHeader(); header.sessionId = sessionId; //create service client to call API endpoint using (partner.SoapClient queryClient = new partner.SoapClient("Soap1", apiAddr)) { partner.sObject account = new partner.sObject(); account.type = "Account"; //create XML containers for necessary XML document and elements XmlDocument rootDoc = new XmlDocument(); XmlElement[] accountFields = new XmlElement[3]; XElement[] test = new XElement[2]; test[0] = new XElement("node1", "value"); //add fields accountFields[0] = rootDoc.CreateElement("Name"); accountFields[0].InnerText = "DevForce06"; accountFields[1] = rootDoc.CreateElement("AccountNumber"); accountFields[1].InnerText = "1004441239"; accountFields[2] = rootDoc.CreateElement("AnnualRevenue"); accountFields[2].InnerText = "4000000"; //set object property to array account.Any = accountFields; partner.SaveResult[] results; queryClient.create( header, //sessionheader null, //calloptions null, //assignmentruleheader null, //mruheader null, //allowfieldtruncationheader null, //disablefeedtrackingheader null, //streamingenabledheader null, //allornothingheader null, //debuggingheader null, //packageversionheader null, //emailheader new partner.sObject[] { account }, //new accounts out results //result of create operation ); //only added one item, so looking at first index of results object if (results[0].success) { Console.WriteLine("Account successfully created."); } Console.ReadLine(); } }