I want to create a batch apex that creates a student record for every contact that is linked with a account having checkbox eligible true. Student data should be taken from contact. My student record is not getting created. There are record for contact matching the select query and no errors being displayed, yet the student record is not created. Below is something I'm trying:

global class ContactsBatch implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('Select id,hasAccount__c,accountid  from contact where accountid in (select id from account) and hasAccount__c=true');

    global void execute(Database.BatchableContext bc, List<Contact> scope) {

        List <Student__c> slist = new List <Student__c>();
        for (Contact c : scope) {
            Student__c s = new Student__c();
            s.Email__c = c.Email;
            s.Contact__c = c.FirstName;

        insert slist;


    global void finish(Database.BatchableContext bc) {
  • 2
    Welcome to SFSE! Please take the tour and read How to Ask. The purpose of this site isn't to be a free coding service, but rather one that helps you grow as a programmer/admin by helping you overcome specific problems. This site works best when you can show us the code you've written so far and point to a specific part of it that you're struggling with. If you do have code you can share with us, you should edit your question to include it (and use the {} button to format it). If you're new to Salesforce, or a feature thereof, then trailhead.salesforce.com is a better place to start. – Derek F Jan 15 at 2:06
  • 2
    Thank you for editing your post to include your code. The next thing we need from you is some help identifying the issue. What is not working? When you look at debug logs, are exceptions being thrown? Are there records in your database that match the query, if you run it in Developer Console? Again, please edit to add information. – David Reed Jan 15 at 5:13
  • If you're not seeing any errors, what do your System.debug() lines tell you? – David Reed Jan 15 at 5:18
  • Maybe there are required fields on the Student__c object that you're not putting in? Are you sure that query is returning results? where accountid in (select id from account) - not sure what you're trying to achieve there and think it can be left out of the query (or do something like AccountId != null instead) – Brian Miller Jan 15 at 10:52
  • If you create a Student__c object through the UI, are you able to create one by simply only filling in the Email__c and Contact__c field? – Kris Goncalves Jan 15 at 12:51

It looks like your issue is with your getQueryLocator.

Please adjust to the following:

global Database.QueryLocator start(Database.BatchableContext bc) {
  return Database.getQueryLocator('Select id,email,firstname,hasAccount__c,accountid  from contact where accountid in (select id from account) and hasAccount__c=true');

Note why: you are creating Student__c records where email and firstname are blank because your c.FirstName and c.Email fields are nonexistent, therefore adding blank student records to a list. SF will not insert blank records.

Stretch: in an effort to improve readability for programmers coming after you, you can try the following syntax (or something else that works for you) for queries that the teams I have worked on have implemented:

global Database.QueryLocator start(Database.BatchableContext bc) {
  return Database.getQueryLocator(
    'SELECT Id, Email, FirstName, hasAccount__c, AccountId' +
    'FROM Contact' +
    'WHERE AccountId IN' +
      '(SELECT Id' +
      'FROM Account)' +
    'AND hasAccount__c = true');

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