/
Executing Salesforce Partner API Methods

Executing Salesforce Partner API Methods

Use the SalesforceSession Binding property to execute API methods. In the following unit test example, the method ConvertLead is used to convert a Salesforce Lead to a Contact (Lead to Account and Opportunity conversions are also possible):


public void ConvertLead()
{
    SalesforceSession salesforceSession = SessionTest.GetActiveSession();
    Lead lead = null;
    LeadService leadService = null;
    ContactService contactService = null;
    Contact contact = null;
	try
	{
		lead = new Lead();
		lead.FirstName = "John";
		lead.LastName = "Smith";
		lead.Company = "Smith Co";
        lead.Status = "Closed - Converted";
        leadService = new LeadService(salesforceSession);
        SaveResult saveResult = leadService.Insert(lead);


        Assert.IsTrue(saveResult.success);
        // Lead will now have an Id
        Assert.IsTrue(leadService.ValidEntityId(lead.Id));
        LeadConvert leadConvert = new LeadConvert();
        leadConvert.leadId = lead.Id;
        leadConvert.overwriteLeadSource = false;
        leadConvert.doNotCreateOpportunity = true;
        leadConvert.convertedStatus = lead.Status;
        leadConvert.sendNotificationEmail = false;
        //leadConvert.contactId = contactId;
        //leadConvert.accountId = accountId;
        //leadConvert.opportunityName = opportunityName;

        LeadConvertResult[] lcr = salesforceSession.Binding.convertLead(new LeadConvert[] { leadConvert });
        for (int i = 0; i < lcr.Length; i++)
        {
			if (lcr[i].success)
          	{
				Console.WriteLine("Conversion succeeded.\n");
				LeadConvertResult result = lcr[i];
				contactService = new ContactService(salesforceSession);
				contact = contactService.GetByEntityId(result.contactId);
				
				Assert.IsTrue(contactService.ValidEntityId(result.contactId));
			}
            else
			{
				Console.WriteLine("Failed: " + lcr[i].errors[0].message);

				Assert.Fail();
			}
		}
	}
	finally
	{
		if (contactService != null && contact != null && contact.Id != null)
		{
			try { contactService.DeleteContact(contact); }
			catch (Exception) { }
		}
	}
}