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
Subhodeep Dey 1Subhodeep Dey 1 

Issue in batch class writing

HI All,

I have created a batch class below in which CONTACT is parent object and alu_Application__c is a child object the retion field name is Applicant__c, I need to update the value of contact Prefered Name with alu_Application__c Prefered Name. I tried to excutethe same but it's not working.

global class ContactUpdate implements Database.Batchable<sObject>
{    
    global string query;
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        system.debug('Check1');
        query = 'SELECT Id,Preferred_Name__c, Applicant__c FROM alu_Application__c';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<alu_Application__c> scope)
    {    
        system.debug('scope'+scope);
        try    
        {
            List<Contact> appCon = new List<Contact>();
            for (alu_Application__c ap : scope)
            {    
                system.debug('ap'+ap);
                Contact con = new Contact();
                con.Id = ap.Applicant__c;
                con.Preferred_Name__c = ap.Preferred_Name__c;
                appCon.add(con);
            }
            system.debug('appCon'+appCon);
            if(appCon.size() > 0)
            {
                update appCon;
                system.debug('appCon'+appCon);
            }
        }
        catch(Exception e)
        {
            system.debug('Error ');
        }
    }  
    global void finish(Database.BatchableContext BC)
    {
        system.debug('Check');
    }
}

Please helip me ASAP, It's really an urgent task for me!

Thanks,
 
Best Answer chosen by Subhodeep Dey 1
Pradeep SinghPradeep Singh
Change your query to SELECT Id,Preferred_Name__c, Applicant__c FROM alu_Application__c where Applicant__c != Null and then check how many records you get. May be there are no such records.

All Answers

Pradeep SinghPradeep Singh
Hi, try updating you code 
Contact con = new Contact();
con.Id = ap.Applicant__c;

TO 
Contact con = new Contact(Id = ap.Applicant__c);

Let me know if you face same problem.
Subhodeep Dey 1Subhodeep Dey 1

Hello Pradeep,

Thanks for the quick response,  But I am still facing the same issue.

 

User-added image

Pradeep SinghPradeep Singh
Go to setup > search for apex jobs and check the status of your job.
Subhodeep Dey 1Subhodeep Dey 1

User-added image

It's saying completed without any failuer.

Pradeep SinghPradeep Singh
Change your query to SELECT Id,Preferred_Name__c, Applicant__c FROM alu_Application__c where Applicant__c != Null and then check how many records you get. May be there are no such records.
This was selected as the best answer