You need to sign in to do that
Don't have an account?

How to send the failure information in a email for the Batch process??
Hi
I have written batch process everything is fine only the problem is I would like to send the Failure information in the email but it is sending as a null will to you in Detail.
I have two objects 1.Site (location 1 and location 2) 2. Inventory(child Object) location 1 inventory and location 2 inventory.
I have another object Stock Transfer. Now I want to transfer the quantity from location 1 to Location 2. I will select location 1 and location 2 then another Object Stock Transfer line from this I can transfer any no items with the Quantity from location1 to location 2.
If stock is available in Location 1 then quantity is transferred to Location 2. If Quantity is not available then I want to send the information in email what lines are not transferred from Location 1 to Location 2.
global class StockTransferLineSitetoSite implements Database.Batchable<sObject>
{
global final String query;
public date today1;
String notTsl{set;get;}
integer failures{get;set;}
//Constroctor
global StockTransferLineSitetoSite (String q)
{
query=q;
failures=0;
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Stock_Transfer_Line__c> scope)
{
if(scope!=null)
{
/* this.today1=System.today();
Stock_Transfer__c[] inventoryList = [select id,Name from Stock_Transfer__c];
for(Stock_Transfer__c i : inventoryList)
{
Stock_Transfer_Line__c [] stl=[select Stock_Transfer__r.Source_Location__c,
Stock_Transfer__r.Destination_location__c,
Item__c,
Qty_Transferred__c,
Request_Date__c
from Stock_Transfer_Line__c
where
Request_date__c=t AND Stock_Transfer__c =:i.id];*/
for(Stock_Transfer_Line__c j : scope)
{
if(j.Posted_To_Inventory__c==false)
{
Inventory__c[] sinv=[select id,
name,
Quantity__c from Inventory__c
where id=:j.Inventory_No__c];
if(sinv[0].Quantity__c>=j.Qty_Transferred__c)
{
sinv[0].Quantity__c-=j.Qty_Transferred__c;
List<Inventory__c> dinv;
if(j.Lot_ID__c!=null)
{
dinv=[select id,
name,
Quantity__c from Inventory__c
where Site__c=:j.Stock_Transfer__r.Destination_Location__c AND
Item__c=:j.Item__c AND
Lot_ID__c=:j.Lot_ID__c];
if(dinv.size()!=0)
{
dinv[0].Quantity__c+=j.Qty_Transferred__c;
j.Posted_To_Inventory__c=true;
update dinv[0];
update sinv[0];
update j;
}
else
{
Item__c item=[select id,name from Item__c where id=:j.Item__c];
Inventory__c inv=new Inventory__c(Name=item.Name,
Item__c=j.Item__c,
Lot_ID__c=j.Lot_ID__c,
Quantity__c=j.Qty_Transferred__c,
Site__c=j.Stock_Transfer__r.Destination_Location__c);
update sinv[0];
insert inv;
j.Posted_To_Inventory__c=true;
update j;
}
}
else if(j.Serial_ID__c!=null)
{
Item__c item=[select id,name from Item__c where id=:j.Item__c];
Inventory__c inv=new Inventory__c(Name=item.Name,
Item__c=j.Item__c,
Serial_ID__c=j.Serial_ID__c,
Quantity__c=j.Qty_Transferred__c,
Site__c=j.Stock_Transfer__r.Destination_Location__c);
j.Posted_To_Inventory__c=true;
update j;
insert inv;
delete sinv[0];
}
else
{
dinv=[select id,
Name,
Quantity__c from Inventory__c
where Site__c=:j.Stock_Transfer__r.Destination_Location__c AND
Item__c=:j.Item__c AND
Lot_ID__c=null AND Serial_ID__c=null];
if(dinv.size()!=0)
{
dinv[0].Quantity__c+=j.Qty_Transferred__c;
j.Posted_To_Inventory__c=true;
update dinv[0];
update sinv[0];
update j;
}
else
{
Item__c item=[select id,name from Item__c where id=:j.Item__c];
Inventory__c inv=new Inventory__c(Name=item.Name,
Item__c=j.Item__c,
Serial_ID__c=j.Serial_ID__c,
Lot_ID__c=j.Lot_ID__c,
Quantity__c=j.Qty_Transferred__c,
Site__c=j.Stock_Transfer__r.Destination_Location__c);
insert inv;
j.Posted_To_Inventory__c=true;
update sinv[0];
update j;
}
}
}
else
{
notTsl=':'+j.Name +':'+j.Stock_Transfer__c;
failures++;
j.Additional_Information__c=notTsl; (in comments it is showing the Stock Transfer Line name and Stock Transfer ID )
update j;
}
}
}
}
notTsl='Hello World';
}
global void finish(Database.BatchableContext BC)
{
AsyncApexJob a = [SELECT id, ApexClassId,
JobItemsProcessed, TotalJobItems,
NumberOfErrors, CreatedBy.Email
FROM AsyncApexJob
WHERE id = :BC.getJobId()];
// Get the ID of the AsyncApexJob representing this batch job
// from Database.BatchableContext.
// Query the AsyncApexJob object to retrieve the current job's information.
/*
AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
from AsyncApexJob where Id =:BC.getJobId()];
*/
// Send an email to the Apex job's submitter notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'mahendar@field-connect.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Schedule Exection mail');
String s='hello these are not transfred lines'+notTsl;
mail.setPlainTextBody('Not Transfered lines are '+s);(But in email it is sending as null)
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Any help is Appreciated.
You need to implement your class as stateful. Use the example below
Regards
-H
All Answers
You need to implement your class as stateful. Use the example below
Regards
-H
Hi
you can use
Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
if(resultMail[0].isSuccess())
{
}
Thanks HenryAG :)
I will give party. Thank you very much.