+ Start a Discussion
Sainath VenkatSainath Venkat 

Batch apex to create or update records

Can anyone help me out in writing batch apex that will create or update records. I have two objects
1) Staging_Event__c
2) Contact
Both objects are having same field Siscode__c, so on inserting records in Staging_Event__c, I need to check if any contact records are having same Siscode__c or not, if yes then I need to update else I need to create Contact.

Can anyone help me out on writing batch apex here if possible.
Rounak SharmaRounak Sharma
hi sainath,
Please check this url it will help you.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_batch
Please let me know if you still have any issues
 
Team NubesEliteTeam NubesElite
Hi Salnath,
Please check this code and make changes according your requirement 
global class FirstBatchClass implements DataBase.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
    string query = 'SELECT Id, Name FROM Account';
    return Database.getQueryLocator(query);
}
    global void execute(Database.BatchableContext BC, List<Account> accList)
    {
        for(Account acc : accList)
        {
         acc.Name = acc.Name = 'FirstBatchApex';   
        }
    try
    {
        update accList;
    }
    catch(Exception e)
    {
        system.debug(e);
    }        
    }
global void finish(Database.BatchableContext BC)
{
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(new String[] {'xyz@gmail.com'});
    mail.setSenderDisplayName('Batch Apex');
    mail.setSubject('Batch Apex Status');
    mail.setPlainTextBody('First Batch Apex Process is Complete');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}



Thank You
www.nubeselite.com

Developement | Training | Consulting

Please Mark this as solution if your problem resolved.