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
MIHIR TAYADEMIHIR TAYADE 

Batch Apex- If Contact Birthdate YEAR is after2000,and its Related Acc. SLA is “Gold”,then Contact’s LeadSource should be “Web”. If Contact Birthdate YEAR is before 2000,and its Acc. SLA is “Silver”,then Contact’s LeadSource source should be-PhoneEnquiry

aanojas lanceraanojas lancer
I don't get it, can you elaborate more about this question?
MIHIR TAYADEMIHIR TAYADE
Write a Batch Apex 
1st condition: - If Contact Birthdate YEAR is after 2000, and its Related Account SLA is “Gold”, then Contact’s LeadSource should be “Web”. 
2nd condition: - If Contact Birthdate YEAR is before 2000,and its Acc. SLA is “Silver”,then Contact’s LeadSource source should be "PhoneEnquiry".
we have to write Batch Apex for the above condition.......
Arun Kumar 1141Arun Kumar 1141
Hello Mihir,

Here is your batch class for the above requirement.
Global class LeadSourceBatch implements Database.Batchable<sObject>{

    Global List<Contact>
        start(Database.BatchableContext bc){
            List<Contact> conList = [Select Id,Name,AccountId,Birthdate,LeadSource,Account.SLA__c from Contact where Birthdate != null AND Account.SLA__c !=null];
            return conList;
        }
    
    Global void execute(Database.BatchableContext bc, List<Contact> conList){

        List<Contact> updateList = new List<Contact>();
        
        for(Contact con:conList){
            if(con.Birthdate.year() > 2000 && con.Account.SLA__c=='Gold'){
                con.LeadSource = 'Web';
                updateList.add(con);
            }else if(con.Birthdate.year() < 2000 && con.Account.SLA__c=='Silver'){
                con.LeadSource = 'PhoneEnquiry';
                updateList.add(con);
            }
        }
        update updateList;
    }
    
    Global void finish(Database.BatchableContext bc){
}
}

Hope this will help you!
Thanks.​​​​​​​
SubratSubrat (Salesforce Developers) 
Hello Mihir ,

To achieve the desired logic in a Batch Apex class, you can query for Contacts based on the specified conditions, update their LeadSource field accordingly, and then perform a bulk update operation. Here's an example implementation:
global class UpdateContactLeadSourceBatch implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        // Query for contacts based on the specified conditions
        return Database.getQueryLocator([
            SELECT Id, LeadSource, Account.SLA__c, Birthdate
            FROM Contact
            WHERE (DATE.YEAR(Birthdate) > 2000 AND Account.SLA__c = 'Gold')
                OR (DATE.YEAR(Birthdate) < 2000 AND Account.SLA__c = 'Silver')
        ]);
    }

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

        // Iterate through the contacts and update their LeadSource field accordingly
        for (Contact contact : scope) {
            if (DATE.YEAR(contact.Birthdate) > 2000 && contact.Account.SLA__c == 'Gold') {
                contact.LeadSource = 'Web';
            } else if (DATE.YEAR(contact.Birthdate) < 2000 && contact.Account.SLA__c == 'Silver') {
                contact.LeadSource = 'PhoneEnquiry';
            }
            contactsToUpdate.add(contact);
        }

        // Perform a bulk update operation
        update contactsToUpdate;
    }

    global void finish(Database.BatchableContext bc) {
        // Optional: Add any post-processing logic here
    }
}
To execute this batch class, you can use the Database.executeBatch method. Here's an example of how you can invoke the batch:
UpdateContactLeadSourceBatch batch = new UpdateContactLeadSourceBatch();
Database.executeBatch(batch);

If this helps , please mark this as Best Answer.
Thank you.