• Nelson Chisoko 8
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
Hello Guys

I currently have a apex rest class exposed which accepts HTTP POST in JSON from an external system. I basically want to process this block into a related list of Job objects linked to a contact.
{
 "jobHistory": [
        {
            "employer": "Example Inc.",
            "position": "Junior receptionist",
            "industry": "Construction",
            "type": "Permanent",
            "fromDate": "12/2010",
            "toDate": "01/2014",
            "current": false,
            "dutues": "Lorem ipsum",
            "reasonForChange": "Lorem ipsum"
        }
    ]
}

Now I can deal with JSON objects such as Location, where I can simply create a Location object and the JSON such as 
{
 "location": {
        "street1": "99 Acme street",
        "street2": null,
        "suburb": "Lonehill",
        "city": "Johannesburg",
        "province": "Gauteng",
        "postalCode": "2129",
        "country": "South Africa"
    }
}

is automatically processed and I get a nice clean Location object I can use for whatever I want to use. Now the problem is when I have the jobHistory one, which is an array of objects. I have the Jobs class which will create the jobs from the jobHistory section {employer etc...}. But I cant seem to get this right.

How do I process the jobHistory JSON code into Job objects which I can then relate to a created contact using apex? Below is my complete code from creation of the Contact object. But I am stuck processing jobHistory in the JSON message.

JSON POST
{
    "email": "joe@example.com",
    "fName": "John",
    "mName": null,
    "lName": "Doe",
    "maidenName": "Dingle",
    "dob": "30/12/1990",
    "gender": "male",
    "idNumber": "9012305006082",
    "passportNumber": "9012305006082",
    "race": "Black",
    "nationality": "RSA",
    "about": "Lorem ipsum..",
    "interests": "Lorem ipsum..",
    "location": {
        "street1": "99 Acme street",
        "street2": null,
        "suburb": "Lonehill",
        "city": "Johannesburg",
        "province": "Gauteng",
        "postalCode": "2129",
        "country": "South Africa"
    },
    "tel": "+27123123412",
    "cell": "+27123123412",
    "careerObjectives": "Lorem ipsum",
    "currentJob": {
        "employer": "Acme Inc.",
        "position": "Senior bottle washer"
    },
    "jobHistory": [
        {
            "employer": "Example Inc.",
            "position": "Junior receptionist",
            "industry": "Construction",
            "type": "Permanent",
            "fromDate": "12/2010",
            "toDate": "01/2014",
            "current": false,
            "dutues": "Lorem ipsum",
            "reasonForChange": "Lorem ipsum"
        }
    ]
}

Apex Class
@RestResource(urlMapping = '/cs/api/v1/candidate')
global with sharing class CandidateRestController {
	global class Location {
		public String street1;
		public String street2;
		public String suburb;
		public String city;
		public String province;
		public String postalCode;
		public String country;
	}
	global class CurrentJob {
		public String employer;
		public String position;
	}
	global class JHistory {
		public List<Job> jobs;			
		public JHistory parse(String json) {
			return (JHistory) System.JSON.deserialize(json, JHistory.class);
		}
	}
	global class Job {
		public String employer;
		public String position;
		public String industry;
		public String type;
		public String fromDate;
		public String toDate;
		public Boolean current;
		public String dutues;
		public String reasonForChange;
	}

	@HttpPost
	global static String createCandidate(
		String email, String fName, String mName, String lName, String maidenName, 
		String dob, String gender, String idNumber, String passportNumber, String race, 
		String nationality, String about, String interests, Location location,  
		String tel, String cell, String careerObjectives, CurrentJob currentJob, 
		JHistory jobHistory

	) {
		String Id;
		try {			
			Contact contact = new Contact(
				Email = email, FirstName = fName, Middle_Name__c = mName, LastName = lName, 
				Maiden_Name__c = maidenName, Dob__c = dob, Gender__c = gender, 
				ID_Number__c = idNumber, Passport_Number__c = passportNumber, Race__c = race, 
				Nationality__c = nationality, About__c = about, Interests__c = interests, 
				Street_1__c = location.street1, Street_2__c = location.street2, 
				Suburb__c = location.suburb, City__c = location.city, Province__c = location.province, 
				Postal_Code__c = location.postalCode, Country__c = location.country, Tel__c = tel, 
				Cell__c = cell, Career_Objectives__c = careerObjectives, Employer__c = currentJob.employer, 
				Position__c = currentJob.position
			);
			insert contact;
            Id = contact.Id;
} catch(System.DmlException e) {
			for (integer i = 0; i < e.getNumDml(); i++) {
				System.debug(e.getDmlMessage(i));
			}
		}
		return Id;
	}
}

 
Hello Guys

Im a bit of a newbie here so I need your help. I am currently working on a POC on Salesforce and I want to know what the best way is to go about connecting to that salesforce account, creating, updating, or deleting records via a third party api. I dont want to build a java app or anything to do so. It should be like a remote call to do what I need to do to the objects.

Any reference to documentation I can look at or guides will be appreciated
Hello Guys

Im a bit of a newbie here so I need your help. I am currently working on a POC on Salesforce and I want to know what the best way is to go about connecting to that salesforce account, creating, updating, or deleting records via a third party api. I dont want to build a java app or anything to do so. It should be like a remote call to do what I need to do to the objects.

Any reference to documentation I can look at or guides will be appreciated