I'm pretty new to the salesforce api. I've been employing the python module simple-salesforce in order to create leads. It works great, but it's really unclear to me how to do non-CRUDlike actions. For example, I want to programatically convert a lead
into an account
.
The salesforce GUI makes this easy. One would simply open the lead
, then click the convert
button. Does anyone out there know how to do this with simple-salesforce
?
UPDATE
I found this describing the creation of an APEX resource Is there any REST service available in Saleforce to Convert Leads into Accounts?
I'm hoping there is a more elegant way to achieve this but I'll post what I do with simple salesforce's apex support if that's what ends up happening.
It looks like the best way to deal with this problem is to create an APEX class as detailed in the linked post. After creating that class, you can use simple salesforce to query it like this:
conversion_result = sf.apexecute('Lead/{id}'.format(id=lead_result['id']), method='GET')
A tip to anyone trying this: please make sure you create the class in a sandbox account. I tried for a good 20 minutes to create the apex class in our production environment without realizing that salesforce doesn't let you do that.
After making the changes in your sandbox you need to upload them to production. Of course, the environments are not connected by default! Here is an explanation on how to allow uploads to your production environment.
UPDATE:
Here is the test class I created for the linked APEX class. Salesforce requires test classes with 75% coverage. This doesn't actually test any functionality, it just passes Salesforce's arbitrary requirements.
@isTest
class RestLeadConvertTest{
@isTest static void testIt(){
Lead lead = new Lead();
lead.LastName = 'salesforce';
lead.Company = 'unittest';
insert lead;
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/Lead/' + lead.Id; //Request URL
req.httpMethod = 'GET';//HTTP Request Type
RestContext.request = req;
RestContext.response= res;
RestLeadConvert.doGet();
}
}