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
nelrib88nelrib88 

max of 2000 soql query issue

i have a problem which im finding hard to fix and is kind of urgent.  i have a third party app that connects to salesforce and uploads campiagns from salesforce to my database.  These capaigns can range from a couple contacts/leads to 100,000 and i would like to find out how its possible to upload a campaign from salesforce to my database.  im currently using the restapi to fetch the contacts and create a list on my side the problem is that im only getting 2000 records.  is it possible to create somethign along the lines of what i already have but scale it to any amount of contacts.

 

        HttpClient httpclient = new HttpClient();
        GetMethod get = new GetMethod(sfSession.getInstanceUrl() + "/services/data/v20.0/query");

        // set the token in the header
        get.setRequestHeader("Authorization", "OAuth " + sfSession.getAccessToken());

        // set the SOQL as a query param
        NameValuePair[] params = new NameValuePair[1];

        params[0] = new NameValuePair("q", "select Contact.Id, Contact.FirstName, Contact.LastName, Contact.Salutation, Contact.Title, Contact.Email, Contact.MailingStreet, Contact.MailingCity, Contact.MailingState, Contact.MailingPostalCode, Contact.MailingCountry, Contact.Phone, Contact.Fax, Contact.MobilePhone, Contact.HomePhone, Contact.LeadSource, Contact.Birthdate from CampaignMember where CampaignId = '" + campaignId + "' and Contact.Id <> NULL");
        get.setQueryString(params);

        try
        {
            httpclient.executeMethod(get);

            if (get.getStatusCode() == HttpStatus.SC_OK)
            {
                try
                {
                    JSONObject response = new JSONObject(new JSONTokener(new InputStreamReader(get.getResponseBodyAsStream())));
                    System.out.println("Auth response: " + response.toString(2));
                    JSONArray results = response.getJSONArray("records");

                    for (int i = 0; i < results.length(); i++)
                    {
                        JSONObject result = results.getJSONObject(i).getJSONObject("Contact");
                        String salesforceContactid = result.getString("Id");
                        Contacts contact = null;

                        try
                        {
                            contact = (Contacts) em.createQuery("select c from Contacts c where c.salesforceContact.salesforceContactid = :salesforceContactid")
                                    .setParameter("salesforceContactid", salesforceContactid)
                                    .getSingleResult();
                        }
                        catch (NoResultException e)
                        {
                            contact = new Contacts();
                            contact.setLists(new LinkedList<Lists>());
                            contact.setCreateDate(new Date());
                        }

                        contact.setLastModified(new Date());

                        if (!result.isNull("FirstName")) contact.setFirstname(result.getString("FirstName").substring(0, Math.min(result.getString("FirstName").length(), 40)));
                        if (!result.isNull("LastName")) contact.setLastname(result.getString("LastName").substring(0, Math.min(result.getString("LastName").length(), 40)));
                        if (!result.isNull("Salutation")) contact.setSalutation(result.getString("Salutation").substring(0, Math.min(result.getString("Salutation").length(), 10)));
                        if (!result.isNull("Title")) contact.setTitle(result.getString("Title").substring(0, Math.min(result.getString("Title").length(), 40)));
                        if (!result.isNull("Email")) contact.setEmail(result.getString("Email").substring(0, Math.min(result.getString("Email").length(), 80)));
                        if (!result.isNull("MailingStreet")) contact.setMailingaddressline1(result.getString("MailingStreet").substring(0, Math.min(result.getString("MailingStreet").length(), 80)));
                        if (!result.isNull("MailingCity")) contact.setCity(result.getString("MailingCity").substring(0, Math.min(result.getString("MailingCity").length(), 40)));
                        if (!result.isNull("MailingState")) contact.setProvince(result.getString("MailingState").substring(0, Math.min(result.getString("MailingState").length(), 40)));
                        if (!result.isNull("MailingPostalCode")) contact.setPostalcode(result.getString("MailingPostalCode").substring(0, Math.min(result.getString("MailingPostalCode").length(), 40)));
                        if (!result.isNull("MailingCountry")) contact.setCountry(result.getString("MailingCountry").substring(0, Math.min(result.getString("MailingCountry").length(), 40)));
                        if (!result.isNull("Phone")) contact.setBusinessphone(result.getString("Phone").substring(0, Math.min(result.getString("Phone").length(), 40)));
                        if (!result.isNull("Fax")) contact.setBusinessfax(result.getString("Fax").substring(0, Math.min(result.getString("Fax").length(), 40)));
                        if (!result.isNull("MobilePhone")) contact.setMobilephone(result.getString("MobilePhone").substring(0, Math.min(result.getString("MobilePhone").length(), 40)));
                        if (!result.isNull("HomePhone")) contact.setHomephone(result.getString("HomePhone").substring(0, Math.min(result.getString("HomePhone").length(), 40)));
                        if (!result.isNull("LeadSource")) contact.setLeadsource(result.getString("LeadSource").substring(0, Math.min(result.getString("LeadSource").length(), 40)));
                        if (!result.isNull("Birthdate")) contact.setBirthdate(result.getString("Birthdate").substring(0, Math.min(result.getString("Birthdate").length(), 20)));

                        // no point in creating contacts with no email
                        if (contact.getEmail() == null || contact.getEmail().isEmpty())
                        {
                            continue;
                        }

                        if (contact.getContactid() == null)
                        {
                            contact.setCreateDate(new Date());
                            // create contact
                            em.persist(contact);

                            // create mapping to sales force id
                            SalesforceContact salesforceContact = new SalesforceContact();
                            salesforceContact.setSalesforceUser(session.getUser().getSalesforceUser());
                            salesforceContact.setSalesforceContactid(salesforceContactid);
                            salesforceContact.setSalesforceType(SalesforceType.Contact);
                            salesforceContact.setContact(contact);
                            em.persist(salesforceContact);

                            // set mapping
                            contact.setSalesforceContact(salesforceContact);
                            em.merge(contact);
                        }
                        else
                        {
                            // update contact info
                            em.merge(contact);
                        }

                        // allow account access to the contact
                        if (!contact.hasAccess(session.getAccount()))
                        {
                            ContactAccount contactAccount = new ContactAccount();
                            contactAccount.setAccount(session.getAccount());
                            contactAccount.setContact(contact);
                            contactAccount.setDate(new Date());
                            em.persist(contactAccount);

                            contact.getContactAccounts().put(session.getAccount(), contactAccount);
                            em.merge(contact);
                        }

                        if (!contact.getLists().contains(list))
                        {
                            contact.getLists().add(list);
                            list.setTotalcontacts(list.getTotalcontacts() + 1);
                        }

                        // update list count
                        em.merge(list);
                    }
                }
                catch (JSONException e)
                {
                    e.printStackTrace(System.err);
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace(System.err);
        }

 thanks,

NR

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

part of the query response includes a flag that indicates if its the entire result set, and if its no, the URL to the next page of results. (which will then indicate if that's the end, and if not, the url to the following page of results and so on), you need to check this flag and fetch the rest of the results.