+ Start a Discussion
Crm98Crm98 

Need help with create a Batch Apex

Hi Guys,
Hoping someone can help me with building a batch apex based on these requirements:

The batch need to check daily against a mailinglist__c object (Status = sending, mailingdatetime = today)--mailinglist object is reference to newsletter_issue__c object.

If the mailinglist object has status "sending" and Time is today, then initiate the batch and pull in region, newsletter, newsletter issue, type, mailingdatetime,

The batch will query all opportunities, where the opp.region = mailinglist.region, opp.newsletter = mailinglist.newletter, opp.type = mailinglist.type, Stage = "Active" and IncludeinMailing__c = Null.

Create a list of opportunity that match these requirements and then update all opportunity with the mailinglist newsletter issue (id).

Create a list of all opportunity updated with the mailinglist.newsletter issue and then create an asset for each of the opportunity.

Assets will contain reference to mailinglist detail and opportunity detail..that will be extract by SF.com reports.

The batch apex will use Database.Stateful to count the number of asset created and opportuntiy updated.

The batch finish method will create a task to provide a summary of the batch completed.
The batch finish method will update the mailinglist object status to ‘completed’ along with the endDatetime of batch completion.

My Code so far:

global class RunMailingAsset implements Database.Batchable<sObject>, Database.Stateful {

Public MailingList__c  RunMailing;
Public Newsletter__c  NewsletterIssue;
Date effectiveDate = today;
global Integer OppsUpdated = 0;
global Integer AssestCreated = 0;

global Database.QueryLocator start(Database.BatchableContext BC)
    {
String query = 'Select Id, Name, mailingDateTime__c, Newsletter__c, Manual_Run__c, Newsletter_Issue__c, Region__c, Run_type__c, Status__c, Type__c FROM MailingList__c Where Status__c = "sending" and mailingDateTime__c = :effectiveDate';
        return Database.getQueryLocator(query);
    }

global void execute(Database.BatchableContext BC, List<sobject> scope, Map<ID, Opportunity> newmap, Map<ID, Opportunity> oldmap)
    { 
List<opportunity> OppwmatchingCriteria = [Select ID, newsletter__c, region__c, type__c, Stage, Includeinmailing__c , accountId From Opportunity where newsletter__c = :Newsletter_Issue__c and region__c =  :region__c and type =:type and Stage = 'Active' and includeInMailing__c='null'];
for (sobject runMailing : scope) {
  for (Opportunity Opportunity : runMailing. OppwmatchingCriteria){
      opportunity.newsletter__c = runMailing.newsletter_issue__c;
     OppwmatchingCriteria.add(opportunity);              
}
//Increment opportunity updated counter 
OppsUpdated = OppsUpdated +1;
}
If (OppwmatchingCriteria.size()==0)
return;
 
List<Asset> ass = [Select ID, Name, Newsletter__c, Opportunity__c FROM Asset where Opportunity__c in : OppwmatchingCriteria];
Set<ID> Assetfound = new Set<ID>();
For (Asset a: ass)
{if(a.Mailinglist__c= newmap.get(a.id).MailingList__c)
Assetfound.add(a.Opportunity__c);
}
MAP<ID, Opportunity> OppsWithNoAssetforRS = new MAP<ID,Opportunity> ();
For (ID opid: OppwmatchingCriteria)
{
if(!Assetfound.contains(opid))
{
OppsWithNoAssetforRS.put(opid,newmap.get(opid));
}
If(OppsWithNoAssetforRS.size()>0)
{
List<Asset> newAssets = new List<Asset>();
For(opportunity opa: OppsWithNoAssetforRS.value())
{
newAssets.add (name = (mailinglist.name + Date.today()), opportunity__c= op.id, mailinglist__c = runmailing.id, Account__c=op.AccountId);
}
If (newAssets.size>0)
insert newAssets;
AssetCreated = AssetCreated +1;
}
 
global void finish(Database.BatchableContext BC)
    {  RunMailing.EndTime__c = DateTime.now();
RunMailing.Status__c=’Completed’;
Update RunMailing;
 
System.debug(OppsUpdated+ ' records updated.');
System.debug(AssestCreated+ ' records updated.!');
 
    }