function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kris WebsterKris Webster 

API Query HELP

I am trying to query salesforce data based on information that I am pulling from our HR system via a REST API callout to the HR system. 

Basically I am tring to find all contacts in my Salesforce org where the first name = the first name in the API Call. I know that first name is not specific enough, but I am starting here and once I can get records to match I will use a more unique identifier. 

Here is my code. I NEED HELP WITH LINE 28! I have left out authorization lines for privacy sake. 
 
request.setMethod('GET');
    HttpResponse response = http.send(request);
    // If the request is successful, parse the JSON response.
    if (response.getStatusCode() == 200) {
        // Deserializes the JSON string into collections of posts.
        Map<String,Object> wrapper = (Map<String,Object>) JSON.deserializeUntyped(response.getBody());
        if (wrapper.containsKey('data')) {
            Map<String, Object> wrapper2 = (Map<String,Object>) wrapper.get('data');
            if(wrapper2.containsKey('data')) {
                List<Object> people = (List<Object>)wrapper2.get('data');
                for (Object peopleWrapper : people) {
                    Map<String,Object> Employees = (Map<String,Object>) peopleWrapper;
                    if(Employees.containsKey('last_name')){
                    String ZenefitsLastName = (String) Employees.get('last_name');
                    String ZenefitsFirstName = (String) Employees.get('first_name');
                    String ZenefitseEmployeeId = (String) Employees.get('id');
                        
                        List<Contact> contactList = [SELECT Id, FirstName, LastName FROM Contact WHERE FirstName = Employees.get('first_name') LIMIT 200];
                        
					//we now need to find all contacts in SF that match the contacts in Employees
                    //once we have matches we need to synce each ID 
                   
                        
         
                            system.debug('Employees ' + Employees);
                            system.debug('last name ' + ZenefitsLastName);
                        	system.debug('Employee id ' + ZenefitseEmployeeId);
                        	system.debug('first name ' + ZenefitsFirstName);
                            system.debug('Contact list ' + contactList);
                            
                   			}
               			}
            		}
            
            return null;
        }
        return null;
    }
    return null;
}

 
Best Answer chosen by Kris Webster
Andrew GAndrew G
Hi Kris

Try adjusting your query to :
List<Contact> contactList = [SELECT Id, FirstName, LastName FROM Contact WHERE FirstName = :ZenefitsFirstName LIMIT 200];
That would answer your question for the query.

However, I would be concerned about the structure of your code, in that you have the SELECT query inside a For Loop - which will cause issues with Too Many SOQLs.

I would consider a format like:
request.setMethod('GET');
    List<String> uniqueIds = new List<String>();
    HttpResponse response = http.send(request);
    // If the request is successful, parse the JSON response.
    if (response.getStatusCode() == 200) {
        // Deserializes the JSON string into collections of posts.
        Map<String,Object> wrapper = (Map<String,Object>) JSON.deserializeUntyped(response.getBody());
        if (wrapper.containsKey('data')) {
            Map<String, Object> wrapper2 = (Map<String,Object>) wrapper.get('data');
            if(wrapper2.containsKey('data')) {
                List<Object> people = (List<Object>)wrapper2.get('data');

                for (Object peopleWrapper : people) {
                    //create a list of uniqueIds for the peopleWrappers
                    //use the first name value if you wish to test with that first

                }
            }
        }
    }
    if (uniqueIds.size()>0) {
        List<Contact> contactsToUpdate = new List<Contact>();
    	for (Contact contact : SELECT Id, FirstName, LastName FROM Contact WHERE FirstName IN :uniqueIds){
 	        //we now process the contacts
            //.....
            contactsToUpdate.add(contact);
        }
        update contactsToUpdate ;
    }
}

Regards

Andrew
 

All Answers

Kris WebsterKris Webster
the line I need help with is 

List<Contact> contactList = [SELECT Id, FirstName, LastName FROM Contact WHERE FirstName = Employees.get('first_name') LIMIT 200];
Andrew GAndrew G
Hi Kris

Try adjusting your query to :
List<Contact> contactList = [SELECT Id, FirstName, LastName FROM Contact WHERE FirstName = :ZenefitsFirstName LIMIT 200];
That would answer your question for the query.

However, I would be concerned about the structure of your code, in that you have the SELECT query inside a For Loop - which will cause issues with Too Many SOQLs.

I would consider a format like:
request.setMethod('GET');
    List<String> uniqueIds = new List<String>();
    HttpResponse response = http.send(request);
    // If the request is successful, parse the JSON response.
    if (response.getStatusCode() == 200) {
        // Deserializes the JSON string into collections of posts.
        Map<String,Object> wrapper = (Map<String,Object>) JSON.deserializeUntyped(response.getBody());
        if (wrapper.containsKey('data')) {
            Map<String, Object> wrapper2 = (Map<String,Object>) wrapper.get('data');
            if(wrapper2.containsKey('data')) {
                List<Object> people = (List<Object>)wrapper2.get('data');

                for (Object peopleWrapper : people) {
                    //create a list of uniqueIds for the peopleWrappers
                    //use the first name value if you wish to test with that first

                }
            }
        }
    }
    if (uniqueIds.size()>0) {
        List<Contact> contactsToUpdate = new List<Contact>();
    	for (Contact contact : SELECT Id, FirstName, LastName FROM Contact WHERE FirstName IN :uniqueIds){
 	        //we now process the contacts
            //.....
            contactsToUpdate.add(contact);
        }
        update contactsToUpdate ;
    }
}

Regards

Andrew
 
This was selected as the best answer
Kris WebsterKris Webster
@andrew how would I go about adding the list of uniqueIds for the peopleWrappers? Sorry I am a littel lost 
Andrew GAndrew G
Hi Kris

When I used the term "UniqueIds", it can be whatever you are using as your key to find the contacts.
So if we look at the example I posted, you could go 
for (Object peopleWrapper : people) 
{
        //create a list of uniqueIds for the peopleWrappers
        //use the first name value if you wish to test with that first
		if(peopleWrapper.containsKey('last_name')){
			uniqueIds.add(peopleWrapper.get('last_name'));
 		}
}
It would depend on the structure of your deserialized JSON

It would be better if the key was something more unique than Last Name, so if you have an HR record Id which you could set up as a field on the contact record. I would assume there is some common key between the two systems.

Regards

Andrew