Search code examples
jsonsalesforceapex

Salesforce - Remove unwanted JSON Data for REST Class


Here is some sample json data that I am parsing:

{
  "lead_id": "TeSter-123-ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz-0123456789-AaBbCcDdEeFfGgHhIiJjKkLl",
  "user_column_data": [
    {
      "column_name": "First Name",
      "string_value": "FirstName",
      "column_id": "FIRST_NAME"
    },
    {
      "column_name": "Last Name",
      "string_value": "LastName",
      "column_id": "LAST_NAME"
    },
    {
      "column_name": "User Phone",
      "string_value": "+16505550123",
      "column_id": "PHONE_NUMBER"
    },
    {
      "column_name": "User Email",
      "string_value": "[email protected]",
      "column_id": "EMAIL"
    },
    {
      "column_name": "City",
      "string_value": "Mountain View",
      "column_id": "CITY"
    },
    {
      "column_name": "Region",
      "string_value": "California",
      "column_id": "REGION"
    },
    {
      "column_name": "Company Name",
      "string_value": "CompanyName",
      "column_id": "COMPANY_NAME"
    }
  ],
  "api_version": "1.0",
  "form_id": 15016502442,
  "campaign_id": 12582701989,
  "google_key": "test",
  "is_test": true,
  "gcl_id": "TeSter-123-ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz-0123456789-AaBbCcDdEeFfGgHhIiJjKkLl",
  "adgroup_id": 20000000000,
  "creative_id": 30000000000

And here is my rest class:

@RestResource(urlMapping='/Lead/*')
global with sharing class MyRestResource {
  
  @HttpPost
    global static String doPost(List<Map<String,String>>user_column_data) {
        Map<String, String> user_data = new Map<String,String>();
        
        for( Map<String,String> field_data : user_column_data){
        user_data.put( field_data.get('column_id'), field_data.get('string_value') );
        }
        
        Lead newLead = new Lead();
        newLead.Status = 'New';
        newLead.Company = user_data.get('COMPANY_NAME');
        newLead.Phone = user_data.get('PHONE_NUMBER');
        newLead.Email = user_data.get('EMAIL');
        newLead.FirstName = user_data.get('FIRST_NAME');
        newLead.LastName = user_data.get('LAST_NAME');
        try{
            insert newLead;
        }
        catch (exception e){
            System.debug('Failure');
        }
        return newLead.Id;
        
    }
}

After deleting:

"lead_id": "TeSter-123-ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz-0123456789-AaBbCcDdEeFfGgHhIiJjKkLl",

......

"api_version": "1.0",
  "form_id": 15016502442,
  "campaign_id": 12582701989,
  "google_key": "test",
  "is_test": true,
  "gcl_id": "TeSter-123-ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz-0123456789-AaBbCcDdEeFfGgHhIiJjKkLl",
  "adgroup_id": 20000000000,
  "creative_id": 30000000000

The record is created within the org through workbench.

I'm not too sure how but how can I ignore any JSON element that doesn't have a columnID so I don't have parse errors.

My idea would be to only capture the information within the brackets from user_column_data but I'm not entirely proficient with Java/Apex so I've been running into a couple errors.


Solution

  • Solution:

    @RestResource(urlMapping='/Lead/*')
    global with sharing class GoogleCampaignRestResource {
    
        public class User_column_data {
            public String column_name; 
            public String string_value;
            public String column_id;
        }
    
        public class LeadWrapper {
            public String lead_id;
            public List<User_column_data> user_column_data;
            public String api_version;
            public Long form_id;
            public Long campaign_id;
            public String google_key;
            public Boolean is_test;
            public String gcl_id;
            public Long adgroup_id;
            public Long creative_id;
    
            public Map<String, String> getDataMap() {
                Map<String, String> dataMap = new Map<String, String>();
    
                for (User_column_data data : this.user_column_data) {
                    dataMap.put(data.column_id, data.string_value);
                }
    
                return dataMap;
            }
        }
    
        @HttpPost
        global static String doPost() {
            RestRequest request = RestContext.request;
            RestResponse response = RestContext.response;
             
            String json = request.requestBody.toString();
            LeadWrapper leadData = (LeadWrapper) System.JSON.deserialize(json, LeadWrapper.class);
    
            Map<String, String> user_data = leadData.getDataMap();
             
            Lead newLead = new Lead(
                Status = 'New',
                Company = user_data.get('COMPANY_NAME'),
                Phone = user_data.get('PHONE_NUMBER'),
                Email = user_data.get('EMAIL'),
                FirstName = user_data.get('FIRST_NAME'),
                LastName = user_data.get('LAST_NAME'),
                LeadSource = 'Google AdWords'
            );
            insert newLead;
    
            return newLead.Id;
        }
    }