simple-salesforce convert lead - python

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();
}
}

Related

Graphene/Flask/SQLAlchemy - What is the recommended method to retrieve data from a route entry point?

Given a basic project structure as follows:
/
app.py <-- Flask app startup and basic routing
models.py
schema.py <-- Contains Graphene/SQLAlchemy schema definitions for queries/mutations
Say in my app.py I have some basic routes setup like so:
#app.route('/members/list', methods=['GET'])
def members():
# What should I do here?
What is the "correct" way to retrieve data? I can see a few different approaches, but I'm not sure if there's a recommended way and I can't seem to find a straightforward answer. For example:
return jsonify(session.query(MembersModel).all()) I feel that this is probably the right way, but it feels weird plopping this down right at the route (feels like I'm missing some service layer architecture) or that I'm not using schema.py correctly. If I were to go this method, does this sit with in my schema.py? Or should I be making a different service-esque file elsewhere?
Running a GraphQL query directly by myself like schema.execute('{ allMembers { ... } }') via Graphene (as seen here) and then parsing my result back in a response. This feels ... wrong, having hardcoded GraphQL in my code when there's a better alternative in #1.
I have prior experience with Spring and I always did it with an MVC styled controller <-> service <-> dao, but I'm not sure what the Flask/Graphene/SQLAlchemy/GraphQL/SQLite equivalent is. I have this nagging feeling that I'm missing an obvious answer here, so if anyone could direct me to some resources or help out, I'd appreciate it.
Thanks!
Okay, after hours of reading I finally realized it: I'm not supposed to be playing between REST web api's and GraphQL like this (disregarding legacy systems/migrations/etc). Essentially, GraphQL loosely competes with REST sort of in the vein with how JSON competes with XML.
I was under the impression that GraphQL was comparable to a higher-level SQL, wherein GraphQL sat above my SQLite layer and abstracted away traditional SQL with some new-fangled terminology and abstractions like relays and connections. Instead, GraphQL competes at an even higher level as mentioned earlier.
So, when dealing with Flask, GraphQL and Graphene, the most I should be doing is execute queries via the GraphiQL interface or POST'em to my server directly - and not do something like GET /my-path/some-resource just to manually hit a GraphQL query somewhere in the backend.
Of course, if I misinterpreted anything, please let me know!

G suite admin SDK for python insert

I am trying to find code examples on how G Suite is dealing with api calls in python. For example there is a method called insert: https://developers.google.com/admin-sdk/directory/v1/reference/users/insert#try-it which permits you to create new users under your enterprise.
The point is that they dont have an example on how you can do that and i find a bit difficult to figure it out through their documentation. Are there any know examples that i could consult?
This worked for me, also using the QuickStart guide.
First, build your user object with the minimum required fields.
Note, this is just a dictionary containing a basic representation of a user object.
The minimum fields to use look like this:
user = {"name": {"familyName": "Burton", "givenName": "Haniel",}, "password": "some_pass", "primaryEmail": "haniel#yourgsuitedomain.com",}
You can add or update additional fields like any other dictionary:
user["orgUnitPath"] = "/Imported"
Then, call the insert method like this in your main() program:
result = service.users().insert(body=user).execute()
Result should be a JSON representation returned by the Directory API with additional attributes that are automatically added by Google.
Some additional links in case anyone else finds it useful:
https://developers.google.com/resources/api-libraries/documentation/admin/directory_v1/python/latest/admin_directory_v1.users.html
I'm working on building a small script/app to handle user creation, updates, and password resets to automate provisioning from our student information system. Depending on how it goes I might post to GitHub and add links here to save others time.

todoist - Fetching the list of projects with python library

I am specifically trying to get the project id given the project name. I saw in the api the api.sync() is supposed to return to me all the projects as in array in a key which I was then planning to iterate through.
I tried using sync with the python library but my projects array is empty, is it some sort of promise mechanism if so how do I wait for success response in python language?
import todoist
api = todoist.TodoistAPI(token)
response = api.sync()
projects = response['projects']
for project in projects:
print(project['name'] + '-' + project['id'])
The python library automatically syncs so the sync method response rarely contains anything useful BUT that information is contained in the api class which has an overridden _find_object method. Therefore you can use the same notation as a dict to find elements - i.e. api['projects'].
I know this is an old question but I'd been having this problem and was struggling to find an answer so hopefully this will be useful to someone at some point!
The library takes care of the Sync for you. In case you already did a Sync in the past, it stores a hash into your $HOME/.todoist-sync/. I recommend you to try to clean this path and try again.

How to get general user information with SteamID64?

I am having problem with doing simple things using Steams 64 bit id.
How can i use SteamAPI to get the general information? like displaying name, username, location.
I used SteamAuth to make my social authentication on website, which only has the function, that gets the id.
Example:
steamid = GetSteamID64()
username = GetUsername()
displayname = GetDisplay()
...
Does SteamAPI on have features related to this? is there any library in python that could support such thing?
There are a whole lot of WebAPI methods to get details about a Steam user. You seem to be looking for GetPlayerSummaries.
You'll need an API key to use it.

Public API with Private Elements in Python

I'm working on a web mapping service and would like to provide my users with a Python API that they can use to create custom plugins. These plugins would be running on my server so I'm trying to lock down Python as much as possible.
To ensure that users can't access files they are not supposed to, I'm planning on running the plugins inside of a PyPy sandboxed interpreter.
The last large hurdle that I'm trying to overcome is how to overcome is with the API itself. I have a database API that will allow the user to make controlled queries to the database. For example, I have a select(column,condition) function that will allow users to retrieve items from the database but they will be restricted to a table. Then, there is a private function _query(sql) that takes raw SQL commands and executes them on the server
The problem is that Python (at least to my knowledge) provides no way of preventing users from calling the query function directly and nuking my database. In order to prevent this, I was thinking of breaking my API into two parts:
------------- pipe -------------- ------------
| publicAPI | -------> | privateAPI | ---> | database |
------------- -------------- ------------
So publicAPI would basically be a proxy API communicating with the main API via a pipe. publicAPI would only contain proxies to the public API functions, users would be unable to get a hold of the private elements (such as _query(sql)).
Do you think this is a viable solution? Am I making way too much work for myself by overlooking a simpler solution?
Thanks so much for your help!
This looks like a clean way to implement this to me. I believe it's also sometimes referred to as the "Facade" design pattern.
In python this is very easy to implement using explicit method delegation (a short snippet to give you a general idea):
class FacadingAPI():
def __init__(fullapi_instance):
self.select = fullapi_instance.select # method delegating
...
There are many ways that you can do this. I tend to have a tuple with all the function names that are available and then check if the function being called is in the tuple, if not then throw an error.
e.g.
funcs = ("available","functions","here")
if (calledFunction in funcs):
#do something
else:
#throw error
or you can take the approach that google have on their App Engine page
http://code.google.com/appengine/articles/rpc.html

Categories

Resources