function readOnly(count){ }
Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
public class InsertObj implements Database.Batchable<sObject>, Database.Stateful { public Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator( 'SELECT ID,name,stagename from opportunity' ); } public void execute(Database.BatchableContext bc, List<opportunity> scope){ List<PIET__c> lstP= new List<PIET__c>(); for (opportunity opp: scope) { PIET__c p = new PIET__c(); p.name = opp.name; p.oppId__c=opp.Id; lstP.add(p); } if(lstP.size()>0){ insert lstP; } } public void finish(Database.BatchableContext bc){ } } Test Class @isTest public class InsertObjTest { @isTest Public Static Void UnitTest(){ list<Opportunity> lstopp = new list<opportunity>(); for(integer i=0;i<5;i++){ opportunity opp = new opportunity(); opp.Name = 'test opp '+ i; opp.CloseDate = system.today(); opp.StageName = 'Closed Won'; lstOpp.add(opp); } insert lstOpp; InsertObj lp = new InsertObj(); database.executeBatch(lp); } }
public class InsertCustomObjectReocrds implements Database.Batchable<sObject>, Database.Stateful { public Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator( 'SELECT Id, Name, Stagename, CloseDate from Opportunity' ); } public void execute(Database.BatchableContext bc, List<opportunity> oppList){ List<PIET__c> listPIET = new List<PIET__c>(); for (Opportunity opp: oppList) { PIET__c pObj = new PIET__c(); pObj.Name = opp.Name; pObj.Opportunity__c = opp.Id; listPIET.add(pObj); } if(!listPIET.isEmpty()){ insert listPIET; system.debug('PIETList ---->' + listPIET); } } public void finish(Database.BatchableContext bc){ AsyncApexJob job = [SELECT Id, Status,NumberOfErrors, JobItemsProcessed,TotalJobItems FROM AsyncApexJob WHERE Id=:bc.getJobId()]; system.debug('job==>'+job); } }
public class InsertObj implements Database.Batchable<sObject>, Database.Stateful { public Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator( 'SELECT ID,name,stagename from opportunity' ); } public void execute(Database.BatchableContext bc, List<opportunity> scope){ List<PIET__c> lstP= new List<PIET__c>(); for (opportunity opp: scope) { PIET__c p = new PIET__c(); p.name = opp.name; p.Opportunity__c=opp.Id; lstP.add(p); } if(lstP.size()>0){ insert lstP; } } public void finish(Database.BatchableContext bc){ } }
Try Below Batch Class Please Mark It As Best Answer If It Helps
Thank You!
p.oppId__c=opp.Id;
and there is need to write a test class
and how to execute this batch class??
This line is For p.oppId__c=opp.Id; if Your Custom Object Have Lookup RelationShip With Opportunity.
If Your Custom object have opportunity lookup Field Then Change This p.oppId__c To p.opprtunitylookupFieldname In Your Org.
Greetings!
Here is your batch class code, try this.
---------------------------------------------------------------------------
If this helped you, please mark it as the best answer.
Thank you!
Regards,
Suraj Tripathi
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!