You need to sign in to do that
Don't have an account?
reddy s 1
Batch Apex (If Condition)
Hi Everyone,
I want to update the account name field with account name+year in created date, if the records updated with name&year these records should not updated again and again only new records should update with name&year.
Facing issues with if condition, can you please help me out this along with test class.
Here is my code :
public class batchAccountConUpdate implements database.Batchable<Sobject>{
public database.QueryLocator start(database.BatchableContext bc){
//fetch the Account record and its related Contact records
return database.getQueryLocator('select id,name,createdDate,(select id,name from Contacts) from Account');
}
public void execute(database.BatchableContext bc, List<Account> scope){
List<Account> acc=new List<Account>();
List<Contact> con=new List<Contact>();
for(Account a:scope){
//Converting DateTime to Date method
DateTime dt = a.CreatedDate;
Date myDate = date.newinstance(dt.year(), dt.month(), dt.day());
System.debug('My date is-----' + myDate);
DateTime dt1 =a.CreatedDate;
Date myDate1= dt1.date();
System.debug('My date is-----' + myDate1);
// if(a.Name != a.Name+myDate1.year() && a.Name != a.Name+a.CreatedDate){
if(a.Name.contains( myDate1.year() )){
a.name=a.Name+myDate1.year();
a.Description='Account name was Updated with the year in Created Date';
acc.add(a);
for(Contact c : a.contacts){
c.FirstName=a.Name;
c.LastName=c.Name;
c.Department='through batch apex';
con.add(c);
}
}
}
update acc;
Update con;
}
public void finish(database.BatchableContext bc){
system.debug('>>>>>>Finish');
}
}
Thanks in Advance.
I want to update the account name field with account name+year in created date, if the records updated with name&year these records should not updated again and again only new records should update with name&year.
Facing issues with if condition, can you please help me out this along with test class.
Here is my code :
public class batchAccountConUpdate implements database.Batchable<Sobject>{
public database.QueryLocator start(database.BatchableContext bc){
//fetch the Account record and its related Contact records
return database.getQueryLocator('select id,name,createdDate,(select id,name from Contacts) from Account');
}
public void execute(database.BatchableContext bc, List<Account> scope){
List<Account> acc=new List<Account>();
List<Contact> con=new List<Contact>();
for(Account a:scope){
//Converting DateTime to Date method
DateTime dt = a.CreatedDate;
Date myDate = date.newinstance(dt.year(), dt.month(), dt.day());
System.debug('My date is-----' + myDate);
DateTime dt1 =a.CreatedDate;
Date myDate1= dt1.date();
System.debug('My date is-----' + myDate1);
// if(a.Name != a.Name+myDate1.year() && a.Name != a.Name+a.CreatedDate){
if(a.Name.contains( myDate1.year() )){
a.name=a.Name+myDate1.year();
a.Description='Account name was Updated with the year in Created Date';
acc.add(a);
for(Contact c : a.contacts){
c.FirstName=a.Name;
c.LastName=c.Name;
c.Department='through batch apex';
con.add(c);
}
}
}
update acc;
Update con;
}
public void finish(database.BatchableContext bc){
system.debug('>>>>>>Finish');
}
}
Thanks in Advance.
Greetings!
First, create a checkbox in the Account object name - DateConcated
and the default value is unchecked.
In the start method, query on those accounts whose DateConcated__c field is false.
In the execute method, concatenate the created date in name and update DateConcated__c to true;
So, next time previously updated records will not be fetched.
If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi