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
salmanmanekiasalmanmanekia 

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.

Best Answer chosen by Admin (Salesforce Developers) 
SRKSRK

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

Navatar_DbSupNavatar_DbSup

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. 

salmanmanekiasalmanmanekia

Its part of the requirement to use the batch apex with scheduler.

SRKSRK

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 });
}


 

This was selected as the best answer
salmanmanekiasalmanmanekia

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 !

SRKSRK

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 ??

salmanmanekiasalmanmanekia

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 :)

salmanmanekiasalmanmanekia

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...

SRKSRK

I look into it on monday
i hope it's not urget

salmanmanekiasalmanmanekia

actually it is sort of urgent..

monday is the deadline for this :)

SRKSRK

Hi sry for late replay
did u try to execute the apex bathc job from sytem consol ??