0

I am looking on button click to create a few contacts from the JSON response I receive from by API request. As it stands I have set up the button and it is parsing the JSON string back to a variable.

global with sharing class PullDirectors {

   webservice static String getDirectors(String regNumber) {
      //test
      //C_House CH = new C_House();
      C_House.CompaniesHouseCalloutsResponse CHresp = new C_House.CompaniesHouseCalloutsResponse();
      C_House.CompaniesHouseCalloutsRequest CHreq = new C_House.CompaniesHouseCalloutsRequest();
      C_House.CompaniesHouse.OfficerList CH = new C_House.CompaniesHouse.OfficerList();
      JSON2ApexX.Items item = new JSON2ApexX.Items();
      // On success, return a message to the calling program.
      String test;
      test = CHreq.getRequestOfficersList(regNumber);

      System.debug('CH : ' + test);
      return test;
   }

}

I have also used a tool to pull the seperate class to build out the variables from the response.

public class JSON2ApexX {

    public class JSON2Apex {
        public Integer inactive_count;
        public Integer total_results;
        public String kind;
        public Integer active_count;
        public String etag;
        public Links links;
        public List<Items> items;
        public Integer start_index;
        public Integer resigned_count;
        public Integer items_per_page;
    }

    public class Address {
        public String postal_code;
        public String locality;
        public String address_line_1;
        public String address_line_2;
        public String region;
    }

    public class Date_of_birth {
        public Integer month;
        public Integer year;
    }

    public class Links_Z {
        public Officer officer;
    }

    public class Address_U {
        public String postal_code;
        public String address_line_1;
        public String premises;
        public String locality;
    }

    public class Links {
        public String self;
    }

    public class Address_V {
        public String locality;
        public String address_line_2;
        public String postal_code;
        public String address_line_1;
    }

    public class Items {
        public Address address;
        public String nationality;
        public String name;
        public String appointed_on;
        public Links_Z links;
        public String officer_role;
        public String country_of_residence;
        public Date_of_birth date_of_birth;
        public String occupation;
        public String resigned_on;
    }

    public class Address_Y {
        public String postal_code;
        public String locality;
        public String address_line_1;
    }

    public class Address_Z {
        public String region;
        public String locality;
        public String postal_code;
        public String address_line_1;
    }

    public class Address_W {
        public String postal_code;
        public String region;
        public String country;
        public String address_line_1;
        public String address_line_2;
        public String locality;
        public String premises;
    }

    public class Officer {
        public String appointments;
    }

    public class Address_X {
        public String postal_code;
        public String locality;
        public String country;
        public String address_line_1;
        public String address_line_2;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }

}

I guess my question is where do I go from here, this is my first time using JSON in salesforce.

Also the JSON response is below:

{
  "total_results": 5,
  "inactive_count": 0,
  "kind": "officer-list",
  "etag": "d73856cd714b6ebc4c2cad780a9eb1167c051352",
  "items_per_page": 35,
  "links": {
    "self": "/company/08004359/officers"
  },
  "start_index": 0,
  "active_count": 2,
  "resigned_count": 3,
  "items": [
    {
      "address": {
        "address_line_1": "Sutherland House",
        "premises": "2nd Floor",
        "address_line_2": "70-78 West Hendon Broadway",
        "locality": "London",
        "postal_code": "NW9 7BT",
        "country": "England"
      },
      "links": {
        "officer": {
          "appointments": "/officers/5dBgxqK6s5gZCN_ujFIQ3k_sGKk/appointments"
        }
      },
      "name": "COHEN, Benjamin Jacob",
      "officer_role": "director",
      "appointed_on": "2015-12-10",
      "nationality": "British",
      "country_of_residence": "England",
      "date_of_birth": {
        "year": 1984,
        "month": 1
      },
      "occupation": "Director"
    },
    {
      "occupation": "Director",
      "date_of_birth": {
        "year": 1975,
        "month": 5
      },
      "nationality": "American",
      "appointed_on": "2016-08-05",
      "country_of_residence": "Usa",
      "officer_role": "director",
      "name": "WEISER, Yekutiel Yehuda Benjamin",
      "links": {
        "officer": {
          "appointments": "/officers/s_SJanjN94plC5v2LnyyOWdiUto/appointments"
        }
      },
      "address": {
        "postal_code": "NW9 7BT",
        "country": "England",
        "address_line_1": "Sutherland House",
        "locality": "London",
        "premises": "2nd Floor",
        "address_line_2": "70-78 West Hendon Broadway"
      }
    },
    {
      "name": "STERNGOLD, Esther",
      "links": {
        "officer": {
          "appointments": "/officers/3ULDNVmQlgZK7rvwoWXJ7YDDGNE/appointments"
        }
      },
      "address": {
        "address_line_1": "35-37 Brent Street",
        "country": "United Kingdom",
        "locality": "London",
        "postal_code": "NW4 2EF",
        "premises": "Foframe House"
      },
      "occupation": "Consultant",
      "date_of_birth": {
        "month": 8,
        "year": 1962
      },
      "country_of_residence": "England",
      "resigned_on": "2012-09-19",
      "nationality": "British",
      "appointed_on": "2012-03-23",
      "officer_role": "director"
    },
    {
      "officer_role": "director",
      "nationality": "American",
      "appointed_on": "2012-03-26",
      "country_of_residence": "United States Of America",
      "resigned_on": "2014-04-10",
      "date_of_birth": {
        "year": 1978,
        "month": 11
      },
      "occupation": "Executive",
      "address": {
        "postal_code": "NW4 2EF",
        "premises": "Foframe House",
        "locality": "London",
        "address_line_1": "35-37 Brent Street",
        "country": "England"
      },
      "name": "WAJSFELD, Shaya",
      "links": {
        "officer": {
          "appointments": "/officers/ua4tQuACZgnozd0mDAQxkyBiOno/appointments"
        }
      }
    },
    {
      "address": {
        "locality": "London",
        "address_line_1": "35-37 Brent Street",
        "country": "United Kingdom",
        "postal_code": "NW4 2EF",
        "premises": "Foframe House"
      },
      "links": {
        "officer": {
          "appointments": "/officers/824nhQl7PjZyL79H9OC5kiAJI8A/appointments"
        }
      },
      "name": "WEISER, Yekutiel Yehuda Benjamin",
      "date_of_birth": {
        "year": 1975,
        "month": 5
      },
      "occupation": "Company Director",
      "officer_role": "director",
      "country_of_residence": "Usa",
      "resigned_on": "2016-08-04",
      "appointed_on": "2012-03-23",
      "nationality": "American"
    }
  ]
}
2

Here is a skeleton that you could expand on that assumes the CHreq.getRequestOfficersList returns the JSON that your JSON2ApexX can then parse and map to Account objects to insert:

String jsonString = CHreq.getRequestOfficersList(regNumber);
JSON2ApexX.JSON2Apex parsed = JSON2ApexX.parse(jsonString);

Account[] inserts = new Account[] {};
for (JSON2ApexX.Items item : parsed.items) {
     inserts.add(new Account(
             Name = item.name,
             BillingStreet = item.address.address_line_1,
             BillingPostalCode = item.address.postal_code,
             ...
             ));
}
insert inserts;

One hard part is mapping the data fields from the JSON to the available standard Account fields. (You could also choose to add your own custom fields to Account.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.