I have a JSON loop-back file coming from Python that I need apex code to filter through the JSON and tell me the value of a field or if it is null so that it can then auto match whether a lead needs to be created/update/ or an opportunity need to be updated/ or an account. I have my code working to taken information for one object but not for multiple. The source data raw data is not grouped with Lead, Opps, Contacts, or Accounts. It is all one record that will need to shorted based on if a field has data or if it is null.

@RestResource (urlMapping='/Leads')
global with sharing class LeadRest {

    global static String doPost(Lead[] Leads) {
        Lead[] LeadsToInsert = new List<Lead>();
        Lead[] LeadsToUpdate = new List<Lead>();

        for(Lead ldr : Leads) {
            if (ldr.id == null){
            else {

        update LeadsToUpdate;
        insert LeadsToInsert;

        return Leads[0].LastName;

    global static Lead[] doGet() {
        Lead[] Leads = [Select

                            from Lead limit 10   
        return Leads;
  • As a side note, you probably don't need any Apex at all to do these operations if you're not building custom business logic around them. You can access basic DML operations and queries in Python via the REST API.
    – David Reed
    Jul 18, 2019 at 16:49
  • Hi David can you expand more on DML operations and queries in Python via REST API? Jul 18, 2019 at 16:58

2 Answers 2


You can just upsert, it does the same thing as filtering if Id is null then insert, else update.

upsert leads;
  • Leads are fine I need the code to be updated to work with multiple Objects. All the fields names on the objects are same. Jul 18, 2019 at 16:59

The basic DML operation, as well as performing queries, are actions that are available via the built-in REST API. If you desire, you may access these resources directly in your Python application. You may want to build your own REST endpoints for the purpose of building additional business logic around the underlying database mechanism, but you're not required to.

The relevant REST resources are

There are existing Python libraries that offer wrappers around these resources:

  • Is this method going to cause my coupling structure to change from Data Coupling to Content Coupling? Meaning Python will be the controlling factor of my salesforce data. Jul 18, 2019 at 18:49
  • @KhylinEdwards I am not sure that I understand your terminology. There would be a tight dependency between your Python code and your Salesforce schema. However, that's already the case, because your custom REST services are passing around sObject instances directly rather than some intermediary form.
    – David Reed
    Jul 18, 2019 at 18:51
  • Trying to process a json that contains data from an external system. I need salesforce to query the json to find the values that will then allow salesforce to know if it is a new lead, or an update to a lead, update Opportunity, or Contact Update. Jul 18, 2019 at 18:57
  • @KhylinEdwards Right now, your code is not processing generic JSON. You're processing JSON that can directly be deserialized by the platform into Leads. If you're intending to process an intermediary JSON format, you'll have to represent that format as an Apex class you can deserialize, or use generic deserializedUntyped() processing. EIther way, I don't see that represented in your question.
    – David Reed
    Jul 19, 2019 at 1:10
  • Yes this is was a POC approach. I do want the code to be deserialize. I was trying to use the SF outofthebox functionality to deserialize it on it's own. My over all goal is: To take in a JSON Loopback from python either create a new lead or update an existing one, create a new opp or update an existing one, create a new contact or update an existing , etc, etc adding more objects into SF apex code because the JSON file will already be sending the data we just dont want to use all of it right now but in the future we might want add a field in an SF obj that the json is sending now. Jul 19, 2019 at 2:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .