You need to sign in to do that
Don't have an account?
Apex Code for Record Creation and Modification
Hi All,
I am new to writing code in APEX. I have an Object 'UnProc' with 10 fields and it is 'POPULATED'. I have two more objects ('Account and Contact') with 5 fields each and these Objects have to be populated. I have to write an APEX code using the batch apex and scheduler so that i can create new records for 'Account' and 'Contact' takin the records which are already there in the 'UnProc'. After the Account and Contact fields get populated with the UnProc records. The UnProc record has to be deleted.
I have already read the batch apex and scheduler related docs on saleforce.com and have understood and created the class for them. But i am stuck at the implementation of my application. Pseoudo code or some lines of code can be useful.
Try below ref code
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator('Select id,name,<all other 10 fields> from UnProc);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
list<account> inacc = new list<account>();
list<contact> incon = new list<contact>();
list<UnProc> delUnProc = new list<UnProc>();
for(sObject s : scope)
{
UnProc temp = (UnProc)s;
Account tempacc = new Account();
Contact tempcon = new Contact();
tempacc.accfield1 = temp.UnProcfield1;
tempacc.accfield2 = temp.UnProcfield2;
tempacc.accfield3 = temp.UnProcfield3;
tempacc.accfield4 = temp.UnProcfield4;
tempacc.accfield5 = temp.UnProcfield5;
tempcon.confield1 = temp.UnProcfield6;
tempcon.confield2 = temp.UnProcfield7;
tempcon.confield3 = temp.UnProcfield8;
tempcon.confield4 = temp.UnProcfield9;
tempcon.confield5 = temp.UnProcfield10;
inacc.add(tempacc);
incon.add(tempcon);
delUnProc.add(temp);
}
insert inacc;
insert incon;
delete delUnProc;
}
global void finish(Database.BatchableContext BC)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {'test@test.com});
mail.setReplyTo('batch@acme.com');
mail.setSenderDisplayName('Batch Processing');
mail.setSubject('Batch Process Completed');
mail.setPlainTextBody('Batch Process has completed');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
All Answers
Hi,
You can write a trigger on Unproc Object to achieve this. Try the below code as reference (Made Changes Accordingly):
trigger CreateAccountContact on Unproc__c(after insert)
{
list<account> ac=new list<account>();
list<contact> con=new list<contact>();
list<id> un=new list<id>();
for(Unproc__c u : trigger.new)
{
con.add(new contact(lastname=u.Contact_Last_Name__c,email=u.Contact_Email__c));
ac.add(new account(name=u.Account_Name__c,website=u.WebSite__c));
un.add(u.id);
}
insert ac;
insert con;
delete [select id from Unproc__c where id in : un];
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Its part of the requirement to use the batch apex with scheduler.
Try below ref code
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator('Select id,name,<all other 10 fields> from UnProc);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
list<account> inacc = new list<account>();
list<contact> incon = new list<contact>();
list<UnProc> delUnProc = new list<UnProc>();
for(sObject s : scope)
{
UnProc temp = (UnProc)s;
Account tempacc = new Account();
Contact tempcon = new Contact();
tempacc.accfield1 = temp.UnProcfield1;
tempacc.accfield2 = temp.UnProcfield2;
tempacc.accfield3 = temp.UnProcfield3;
tempacc.accfield4 = temp.UnProcfield4;
tempacc.accfield5 = temp.UnProcfield5;
tempcon.confield1 = temp.UnProcfield6;
tempcon.confield2 = temp.UnProcfield7;
tempcon.confield3 = temp.UnProcfield8;
tempcon.confield4 = temp.UnProcfield9;
tempcon.confield5 = temp.UnProcfield10;
inacc.add(tempacc);
incon.add(tempcon);
delUnProc.add(temp);
}
insert inacc;
insert incon;
delete delUnProc;
}
global void finish(Database.BatchableContext BC)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {'test@test.com});
mail.setReplyTo('batch@acme.com');
mail.setSenderDisplayName('Batch Processing');
mail.setSubject('Batch Process Completed');
mail.setPlainTextBody('Batch Process has completed');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
Thank you. Thats exactly what it looks like what i have been looking for :) ..I havent gone through your code yet..But it s good to have some code :). Will go through your code and comeback to you with (if i have ) further questions and 'solution to the post'. Thanks one again SRK !
Hi Ankit
don't mind i just want to understnad one thing
u give a sample trigger code
& u just write
delete [select id from Unproc__c where id in : un];
u r deleting the same record on which u r run a after insert trigger do u thing it work ??
Thanks Ankit. Its helpful.
But i have to use scheduler and batch apex. So, according to my understanding no to Trigger :). I do not know if Trigger and Scheduler can be used together. But Anyways, your code looks simple and easy to understand. Is ther any way it can be mapped for scheduler and Batch APEX..
I hope you understand what i am trying to say :)
hi srk, i have tried your code, there seems to be no error but when i deploy it with ant i.e the mogration tool it would not work . i mean that the account and contact objects are not populated with the records...any ideas...
I look into it on monday
i hope it's not urget
actually it is sort of urgent..
monday is the deadline for this :)
Hi sry for late replay
did u try to execute the apex bathc job from sytem consol ??