You need to sign in to do that
Don't have an account?
santhosh konathala 17
Hi can anybody help how to write a Test class for the below class..I am not aware in writting a Test class.plz help me...
public with sharing class MTX_SupplierFromOracleAP{
List<MTX_SupplierOracleAP__c> suppliersWithStatus = new List<MTX_SupplierOracleAP__c>();
public void insertSupplier(){
List<MTX_SupplierOracleAP__c> oracleAPSuppliers = new List<MTX_SupplierOracleAP__c>();
oracleAPSuppliers = [Select Id , NewGSLNumber__c , SupplierNumber__c , SupplierSiteName__c , EntityNumber__c , NSFECapexSupplier__c ,
ActivatedDate__c , DeactivatedDate__c , Status__c , SupplierName__c ,
SupplierAddress__c , SupplierCity__c , SupplierCountry__c ,
SupplierTelephoneNumber__c , SupplierFaxNumber__c , NewSiteName__c , CurrencyIsoCode , SupplierEmail__c ,
VatRegistrationNumber__c , RemitContLastName__c , RemitContFirstName__c ,
RemittanceFaxNr__c , RemittanceEmail__c , RemittanceTelephoneNr__c , SupplierZipCode__c
From MTX_SupplierOracleAP__c ];
Set<String> GSLNumbers = new Set<String>();
Set<String> SupplierNumbers = new Set<String>();
Set<String> SupplierSiteNames = new Set<String>();
Set<String> EntityNumbers = new Set<String>();
Set<String> NSFECapexSuppliers = new Set<String>();
Set<String> uniqueSuppliers = new Set<String>();
for(MTX_SupplierOracleAP__c oracleAPSupplier : oracleAPSuppliers){
if(oracleAPSupplier.NewGSLNumber__c != null && oracleAPSupplier.NewGSLNumber__c != ''){
GSLNumbers.add(oracleAPSupplier.NewGSLNumber__c);
}
SupplierNumbers.add(oracleAPSupplier.SupplierNumber__c);
SupplierSiteNames.add(oracleAPSupplier.SupplierSiteName__c);
EntityNumbers.add(oracleAPSupplier.EntityNumber__c);
NSFECapexSuppliers.add(oracleAPSupplier.NSFECapexSupplier__c);
String uniqueSupplier = '';
uniqueSupplier = oracleAPSupplier.SupplierNumber__c +'-'+ oracleAPSupplier.SupplierSiteName__c +'-'+ oracleAPSupplier.EntityNumber__c+'-'+ oracleAPSupplier.NSFECapexSupplier__c ;
uniqueSuppliers.add(uniqueSupplier);
}
Set<String> existingGSLNumbers = new Set<String>();
for(Account acc : [Select GSLNumber__c from Account Where GSLNumber__c IN :GSLNumbers AND BranchCode__c != 'T01' ] ){
existingGSLNumbers.add(acc.GSLNumber__c);
}
Set<String> existingUniqueSuppliers = new Set<String>();
for(Account acc : [Select Id , AccountNumber ,SiteName__c , Company_Reg_Number__c , SupplierCategory__c
From Account
Where AccountNumber IN :SupplierNumbers AND
SiteName__c IN :SupplierSiteNames AND
Company_Reg_Number__c IN : EntityNumbers AND
SupplierCategory__c IN :NSFECapexSuppliers AND
BranchCode__c != 'T01' ] ){
String uniqueSupplier = '';
uniqueSupplier = acc.AccountNumber +'-'+ acc.SiteName__c +'-'+ acc.Company_Reg_Number__c +'-'+ acc.SupplierCategory__c ;
existingUniqueSuppliers.add(uniqueSupplier);
}
List<MTX_SupplierOracleAP__c> validSuppliers = new List<MTX_SupplierOracleAP__c>();
List<MTX_SupplierOracleAP__c> invalidSuppliers = new List<MTX_SupplierOracleAP__c>();
for(MTX_SupplierOracleAP__c oracleAPSupplier : oracleAPSuppliers){
String errorString = '' ;
if(oracleAPSupplier.NewGSLNumber__c == null || oracleAPSupplier.NewGSLNumber__c == '' ){
errorString += 'No GSL Number ,' ;
}
else if ( existingGSLNumbers.contains(oracleAPSupplier.NewGSLNumber__c)){
errorString += 'GSL Number is not unique ,' ;
}
String uniqueSupplier = '';
uniqueSupplier = oracleAPSupplier.SupplierNumber__c +'-'+ oracleAPSupplier.SupplierSiteName__c +'-'+ oracleAPSupplier.EntityNumber__c+'-'+ oracleAPSupplier.NSFECapexSupplier__c ;
if(existingUniqueSuppliers.contains(uniqueSupplier)){
errorString += 'Supplier has not unique combination ,' ;
}
if( oracleAPSupplier.ActivatedDate__c != null && oracleAPSupplier.DeactivatedDate__c != null
&& oracleAPSupplier.DeactivatedDate__c < oracleAPSupplier.ActivatedDate__c ){
errorString += 'Deactivated date should be greater than activated date ,' ;
}
oracleAPSupplier.Status__c = '' ;
if( oracleAPSupplier.DeactivatedDate__c != null ){
oracleAPSupplier.Status__c = 'void' ;
}
/*
if(oracleAPSupplier.SupplierCategory__c != 'NSFE'){
errorString += 'Supplier Category is not NSFE ,' ;
}
*/
if(oracleAPSupplier.SupplierName__c == null || oracleAPSupplier.SupplierName__c == ''){
errorString += 'No Supplier Name ,' ;
}
/*
if(oracleAPSupplier.EntityName__c == null){
errorString += 'No Entity Name ,' ;
}
*/
/*
if(oracleAPSupplier.Currency__c == null){
errorString += 'No Currency ,' ;
}
*/
if(oracleAPSupplier.SupplierAddress__c == null || oracleAPSupplier.SupplierAddress__c == ''){
errorString += 'No Address ,' ;
}
if(oracleAPSupplier.SupplierCity__c == null || oracleAPSupplier.SupplierCity__c == ''){
errorString += 'No City ,' ;
}
if(oracleAPSupplier.SupplierCountry__c == null || oracleAPSupplier.SupplierCountry__c == ''){
errorString += 'No Country ,' ;
}
String errorDescription ;
if(errorString != ''){
errorDescription = 'Not inserted with listed reason -- '+ errorString.removeEnd(',');
system.debug('-->Error'+errorDescription);
}
if(errorString == ''){
validSuppliers.add(oracleAPSupplier);
}
else{
oracleAPSupplier.ProcessDate__c = system.now();
oracleAPSupplier.ProcessStatus__c = 'Error' ;
oracleAPSupplier.ErrorDescription__c = errorDescription ;
invalidSuppliers.add(oracleAPSupplier);
}
}
system.debug('-->Valid Suppliers'+validSuppliers);
system.debug('-->inValid Suppliers'+invalidSuppliers);
suppliersWithStatus.addAll(invalidSuppliers);
insertValidSuppliers(validSuppliers);
update suppliersWithStatus ;
}
private void insertValidSuppliers(List<MTX_SupplierOracleAP__c> validSuppliers ){
List<String> supplierIds = new List<String>();
List<Account> supplierAccounts = new List<Account>();
Map<String , Contact> supplierContacts = new Map<String ,Contact>();
List<MTX_Address__c> supplierAddresses = new List<MTX_Address__c>();
for(MTX_SupplierOracleAP__c oracleAPSupplier : validSuppliers){
supplierIds.add(oracleAPSupplier.Id);
Account acc = new Account();
acc.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Supplier').RecordTypeId;
acc.Name = 'test oracle AP interface' ;
acc.AccountNumber = oracleAPSupplier.SupplierNumber__c ;
acc.GSLSiteNumber__c = oracleAPSupplier.SupplierSiteNumber__c ;
acc.SiteName__c = oracleAPSupplier.SupplierSiteName__c ;
acc.Company_Reg_Number__c = oracleAPSupplier.EntityNumber__c ;
//acc.CreatedDate = oracleAPSupplier.ActivatedDate__c ;
//acc.SupplierStatus__c = oracleAPSupplier.DeactivatedDate__c ;
acc.SupplierStatus__c = oracleAPSupplier.Status__c ;
acc.Phone = oracleAPSupplier.SupplierTelephoneNumber__c;
acc.Fax = oracleAPSupplier.SupplierFaxNumber__c ;
acc.SupplierName__c = oracleAPSupplier.SupplierName__c ;
acc.GSLNumber__c = oracleAPSupplier.NewGSLNumber__c ;
acc.SiteName__c = oracleAPSupplier.NewSiteName__c ;
//acc.SupplierPaymentType__c = oracleAPSupplier.PaymentTerms__c ;
acc.Currency__c = oracleAPSupplier.CurrencyIsoCode ;
acc.Email__c = oracleAPSupplier.SupplierEmail__c;
//acc.VAT_Number__c = oracleAPSupplier.VatRegistrationNumber__c ; //FIELD_CUSTOM_VALIDATION_EXCEPTION, VAT Number is not required if Country is BLANK: [VAT_Number__c]
//acc.SupplierCategory__c = oracleAPSupplier.NSFECapexSupplier__c ; Bad picklist value error
supplierAccounts.add(acc);
Contact con = new Contact();
//con.AccountId = acc.Id ;
con.LastName = oracleAPSupplier.RemitContLastName__c ;
con.FirstName= oracleAPSupplier.RemitContFirstName__c ;
con.Fax = oracleAPSupplier.RemittanceFaxNr__c ;
con.Email = oracleAPSupplier.RemittanceEmail__c ;
con.Phone = oracleAPSupplier.RemittanceTelephoneNr__c ;
//con.Mobile = oracleAPSupplier.SupplierMobileNr__c ;
//con.Name = oracleAPSupplier.SupplierContact__c;
supplierContacts.put(oracleAPSupplier.Id , con);
MTX_Address__c address = new MTX_Address__c();
address.AddressLine1__c = oracleAPSupplier.SupplierAddress__c;
address.City__c = oracleAPSupplier.SupplierCity__c ;
address.PostCode__c = oracleAPSupplier.SupplierZipCode__c ;
address.Country__c = oracleAPSupplier.SupplierCountry__c ;
supplierAddresses.add(address);
}
system.debug('account size is-->'+supplierAccounts.size());
system.debug('contact size is-->'+supplierContacts.size());
system.debug('address size is-->'+supplierAddresses.size());
Map<String,String> mapSupplierWithAccount = new Map<String,String>();
Database.SaveResult[] accountsResult = Database.insert( supplierAccounts , false ) ;
for (Integer i = 0; i < accountsResult.size(); i++ ) {
if(accountsResult[i].isSuccess()){
mapSupplierWithAccount.put(supplierIds[i],accountsResult[i].getId());
}
else {
String ErrorMessage = 'ERROR ::' ;
// Operation failed, so get all errors
for(Database.Error err : accountsResult[i].getErrors()) {
ErrorMessage += err.getMessage() + '::';
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
mapSupplierWithAccount.put(supplierIds[i],ErrorMessage);
}
}
system.debug('Accounts are -->'+mapSupplierWithAccount);
Map<String,String> mapSupplierWithAddress = new Map<String,String>();
Database.SaveResult[] addressResult = Database.insert( supplierAccounts , false ) ;
for (Integer i = 0; i < addressResult.size(); i++ ) {
if(addressResult[i].isSuccess()){
mapSupplierWithAddress.put(supplierIds[i],addressResult[i].getId());
}
else {
String ErrorMessage = 'ERROR ::' ;
// Operation failed, so get all errors
for(Database.Error err : addressResult[i].getErrors()) {
ErrorMessage += err.getMessage() + '::';
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
mapSupplierWithAddress.put(supplierIds[i],ErrorMessage);
}
}
system.debug('Addresses are -->'+mapSupplierWithAddress);
List<MTX_AccountAddress__c> accountAddresses = new List<MTX_AccountAddress__c>();
for(String supplierId : mapSupplierWithAccount.keySet()){
if( ! mapSupplierWithAccount.get(supplierId).contains('ERROR ::')){
if(! mapSupplierWithAddress.get(supplierId).contains('ERROR ::')){
MTX_AccountAddress__c accAddress = new MTX_AccountAddress__c();
accAddress.Account__c = mapSupplierWithAccount.get(supplierId) ;
accAddress.Address__c = mapSupplierWithAddress.get(supplierId) ;
accountAddresses.add(accAddress) ;
}
Contact con = supplierContacts.get(supplierId);
con.AccountId = mapSupplierWithAccount.get(supplierId) ;
supplierContacts.put(supplierId, con);
MTX_SupplierOracleAP__c supplier = new MTX_SupplierOracleAP__c(Id = supplierId);
supplier.ProcessDate__c = system.now();
supplier.ProcessStatus__c = 'Success' ;
//supplier.ErrorDescription__c = oracleSupplier.ErrorDescription__c ;
suppliersWithStatus.add(supplier);
}
else {
MTX_SupplierOracleAP__c supplier = new MTX_SupplierOracleAP__c(Id = supplierId);
supplier.ProcessDate__c = system.now();
supplier.ProcessStatus__c = 'Error' ;
supplier.ErrorDescription__c = mapSupplierWithAccount.get(supplierId) ;
suppliersWithStatus.add(supplier);
}
}
insert supplierContacts.values();
insert accountAddresses ;
}
public void sendEmail(){
// Reference the attachment page, pass in the account ID
PageReference supplierPage = Page.MTX_SupplierLogFile;
// Take the PDF content
Blob b = supplierPage .getContent();
OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress limit 1];
// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('supplierLog.txt');
efa.setBody(b);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Sets the paramaters of the email
email.setSubject( 'Test Email for Supplier Log');
email.setToAddresses( new List<String>{'VR00471039@techmahindra.com'});
email.setPlainTextBody( 'Hello PFA.');
email.setOrgWideEmailAddressId(owa.id);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
List<MTX_SupplierOracleAP__c> suppliersWithStatus = new List<MTX_SupplierOracleAP__c>();
public void insertSupplier(){
List<MTX_SupplierOracleAP__c> oracleAPSuppliers = new List<MTX_SupplierOracleAP__c>();
oracleAPSuppliers = [Select Id , NewGSLNumber__c , SupplierNumber__c , SupplierSiteName__c , EntityNumber__c , NSFECapexSupplier__c ,
ActivatedDate__c , DeactivatedDate__c , Status__c , SupplierName__c ,
SupplierAddress__c , SupplierCity__c , SupplierCountry__c ,
SupplierTelephoneNumber__c , SupplierFaxNumber__c , NewSiteName__c , CurrencyIsoCode , SupplierEmail__c ,
VatRegistrationNumber__c , RemitContLastName__c , RemitContFirstName__c ,
RemittanceFaxNr__c , RemittanceEmail__c , RemittanceTelephoneNr__c , SupplierZipCode__c
From MTX_SupplierOracleAP__c ];
Set<String> GSLNumbers = new Set<String>();
Set<String> SupplierNumbers = new Set<String>();
Set<String> SupplierSiteNames = new Set<String>();
Set<String> EntityNumbers = new Set<String>();
Set<String> NSFECapexSuppliers = new Set<String>();
Set<String> uniqueSuppliers = new Set<String>();
for(MTX_SupplierOracleAP__c oracleAPSupplier : oracleAPSuppliers){
if(oracleAPSupplier.NewGSLNumber__c != null && oracleAPSupplier.NewGSLNumber__c != ''){
GSLNumbers.add(oracleAPSupplier.NewGSLNumber__c);
}
SupplierNumbers.add(oracleAPSupplier.SupplierNumber__c);
SupplierSiteNames.add(oracleAPSupplier.SupplierSiteName__c);
EntityNumbers.add(oracleAPSupplier.EntityNumber__c);
NSFECapexSuppliers.add(oracleAPSupplier.NSFECapexSupplier__c);
String uniqueSupplier = '';
uniqueSupplier = oracleAPSupplier.SupplierNumber__c +'-'+ oracleAPSupplier.SupplierSiteName__c +'-'+ oracleAPSupplier.EntityNumber__c+'-'+ oracleAPSupplier.NSFECapexSupplier__c ;
uniqueSuppliers.add(uniqueSupplier);
}
Set<String> existingGSLNumbers = new Set<String>();
for(Account acc : [Select GSLNumber__c from Account Where GSLNumber__c IN :GSLNumbers AND BranchCode__c != 'T01' ] ){
existingGSLNumbers.add(acc.GSLNumber__c);
}
Set<String> existingUniqueSuppliers = new Set<String>();
for(Account acc : [Select Id , AccountNumber ,SiteName__c , Company_Reg_Number__c , SupplierCategory__c
From Account
Where AccountNumber IN :SupplierNumbers AND
SiteName__c IN :SupplierSiteNames AND
Company_Reg_Number__c IN : EntityNumbers AND
SupplierCategory__c IN :NSFECapexSuppliers AND
BranchCode__c != 'T01' ] ){
String uniqueSupplier = '';
uniqueSupplier = acc.AccountNumber +'-'+ acc.SiteName__c +'-'+ acc.Company_Reg_Number__c +'-'+ acc.SupplierCategory__c ;
existingUniqueSuppliers.add(uniqueSupplier);
}
List<MTX_SupplierOracleAP__c> validSuppliers = new List<MTX_SupplierOracleAP__c>();
List<MTX_SupplierOracleAP__c> invalidSuppliers = new List<MTX_SupplierOracleAP__c>();
for(MTX_SupplierOracleAP__c oracleAPSupplier : oracleAPSuppliers){
String errorString = '' ;
if(oracleAPSupplier.NewGSLNumber__c == null || oracleAPSupplier.NewGSLNumber__c == '' ){
errorString += 'No GSL Number ,' ;
}
else if ( existingGSLNumbers.contains(oracleAPSupplier.NewGSLNumber__c)){
errorString += 'GSL Number is not unique ,' ;
}
String uniqueSupplier = '';
uniqueSupplier = oracleAPSupplier.SupplierNumber__c +'-'+ oracleAPSupplier.SupplierSiteName__c +'-'+ oracleAPSupplier.EntityNumber__c+'-'+ oracleAPSupplier.NSFECapexSupplier__c ;
if(existingUniqueSuppliers.contains(uniqueSupplier)){
errorString += 'Supplier has not unique combination ,' ;
}
if( oracleAPSupplier.ActivatedDate__c != null && oracleAPSupplier.DeactivatedDate__c != null
&& oracleAPSupplier.DeactivatedDate__c < oracleAPSupplier.ActivatedDate__c ){
errorString += 'Deactivated date should be greater than activated date ,' ;
}
oracleAPSupplier.Status__c = '' ;
if( oracleAPSupplier.DeactivatedDate__c != null ){
oracleAPSupplier.Status__c = 'void' ;
}
/*
if(oracleAPSupplier.SupplierCategory__c != 'NSFE'){
errorString += 'Supplier Category is not NSFE ,' ;
}
*/
if(oracleAPSupplier.SupplierName__c == null || oracleAPSupplier.SupplierName__c == ''){
errorString += 'No Supplier Name ,' ;
}
/*
if(oracleAPSupplier.EntityName__c == null){
errorString += 'No Entity Name ,' ;
}
*/
/*
if(oracleAPSupplier.Currency__c == null){
errorString += 'No Currency ,' ;
}
*/
if(oracleAPSupplier.SupplierAddress__c == null || oracleAPSupplier.SupplierAddress__c == ''){
errorString += 'No Address ,' ;
}
if(oracleAPSupplier.SupplierCity__c == null || oracleAPSupplier.SupplierCity__c == ''){
errorString += 'No City ,' ;
}
if(oracleAPSupplier.SupplierCountry__c == null || oracleAPSupplier.SupplierCountry__c == ''){
errorString += 'No Country ,' ;
}
String errorDescription ;
if(errorString != ''){
errorDescription = 'Not inserted with listed reason -- '+ errorString.removeEnd(',');
system.debug('-->Error'+errorDescription);
}
if(errorString == ''){
validSuppliers.add(oracleAPSupplier);
}
else{
oracleAPSupplier.ProcessDate__c = system.now();
oracleAPSupplier.ProcessStatus__c = 'Error' ;
oracleAPSupplier.ErrorDescription__c = errorDescription ;
invalidSuppliers.add(oracleAPSupplier);
}
}
system.debug('-->Valid Suppliers'+validSuppliers);
system.debug('-->inValid Suppliers'+invalidSuppliers);
suppliersWithStatus.addAll(invalidSuppliers);
insertValidSuppliers(validSuppliers);
update suppliersWithStatus ;
}
private void insertValidSuppliers(List<MTX_SupplierOracleAP__c> validSuppliers ){
List<String> supplierIds = new List<String>();
List<Account> supplierAccounts = new List<Account>();
Map<String , Contact> supplierContacts = new Map<String ,Contact>();
List<MTX_Address__c> supplierAddresses = new List<MTX_Address__c>();
for(MTX_SupplierOracleAP__c oracleAPSupplier : validSuppliers){
supplierIds.add(oracleAPSupplier.Id);
Account acc = new Account();
acc.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Supplier').RecordTypeId;
acc.Name = 'test oracle AP interface' ;
acc.AccountNumber = oracleAPSupplier.SupplierNumber__c ;
acc.GSLSiteNumber__c = oracleAPSupplier.SupplierSiteNumber__c ;
acc.SiteName__c = oracleAPSupplier.SupplierSiteName__c ;
acc.Company_Reg_Number__c = oracleAPSupplier.EntityNumber__c ;
//acc.CreatedDate = oracleAPSupplier.ActivatedDate__c ;
//acc.SupplierStatus__c = oracleAPSupplier.DeactivatedDate__c ;
acc.SupplierStatus__c = oracleAPSupplier.Status__c ;
acc.Phone = oracleAPSupplier.SupplierTelephoneNumber__c;
acc.Fax = oracleAPSupplier.SupplierFaxNumber__c ;
acc.SupplierName__c = oracleAPSupplier.SupplierName__c ;
acc.GSLNumber__c = oracleAPSupplier.NewGSLNumber__c ;
acc.SiteName__c = oracleAPSupplier.NewSiteName__c ;
//acc.SupplierPaymentType__c = oracleAPSupplier.PaymentTerms__c ;
acc.Currency__c = oracleAPSupplier.CurrencyIsoCode ;
acc.Email__c = oracleAPSupplier.SupplierEmail__c;
//acc.VAT_Number__c = oracleAPSupplier.VatRegistrationNumber__c ; //FIELD_CUSTOM_VALIDATION_EXCEPTION, VAT Number is not required if Country is BLANK: [VAT_Number__c]
//acc.SupplierCategory__c = oracleAPSupplier.NSFECapexSupplier__c ; Bad picklist value error
supplierAccounts.add(acc);
Contact con = new Contact();
//con.AccountId = acc.Id ;
con.LastName = oracleAPSupplier.RemitContLastName__c ;
con.FirstName= oracleAPSupplier.RemitContFirstName__c ;
con.Fax = oracleAPSupplier.RemittanceFaxNr__c ;
con.Email = oracleAPSupplier.RemittanceEmail__c ;
con.Phone = oracleAPSupplier.RemittanceTelephoneNr__c ;
//con.Mobile = oracleAPSupplier.SupplierMobileNr__c ;
//con.Name = oracleAPSupplier.SupplierContact__c;
supplierContacts.put(oracleAPSupplier.Id , con);
MTX_Address__c address = new MTX_Address__c();
address.AddressLine1__c = oracleAPSupplier.SupplierAddress__c;
address.City__c = oracleAPSupplier.SupplierCity__c ;
address.PostCode__c = oracleAPSupplier.SupplierZipCode__c ;
address.Country__c = oracleAPSupplier.SupplierCountry__c ;
supplierAddresses.add(address);
}
system.debug('account size is-->'+supplierAccounts.size());
system.debug('contact size is-->'+supplierContacts.size());
system.debug('address size is-->'+supplierAddresses.size());
Map<String,String> mapSupplierWithAccount = new Map<String,String>();
Database.SaveResult[] accountsResult = Database.insert( supplierAccounts , false ) ;
for (Integer i = 0; i < accountsResult.size(); i++ ) {
if(accountsResult[i].isSuccess()){
mapSupplierWithAccount.put(supplierIds[i],accountsResult[i].getId());
}
else {
String ErrorMessage = 'ERROR ::' ;
// Operation failed, so get all errors
for(Database.Error err : accountsResult[i].getErrors()) {
ErrorMessage += err.getMessage() + '::';
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
mapSupplierWithAccount.put(supplierIds[i],ErrorMessage);
}
}
system.debug('Accounts are -->'+mapSupplierWithAccount);
Map<String,String> mapSupplierWithAddress = new Map<String,String>();
Database.SaveResult[] addressResult = Database.insert( supplierAccounts , false ) ;
for (Integer i = 0; i < addressResult.size(); i++ ) {
if(addressResult[i].isSuccess()){
mapSupplierWithAddress.put(supplierIds[i],addressResult[i].getId());
}
else {
String ErrorMessage = 'ERROR ::' ;
// Operation failed, so get all errors
for(Database.Error err : addressResult[i].getErrors()) {
ErrorMessage += err.getMessage() + '::';
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
mapSupplierWithAddress.put(supplierIds[i],ErrorMessage);
}
}
system.debug('Addresses are -->'+mapSupplierWithAddress);
List<MTX_AccountAddress__c> accountAddresses = new List<MTX_AccountAddress__c>();
for(String supplierId : mapSupplierWithAccount.keySet()){
if( ! mapSupplierWithAccount.get(supplierId).contains('ERROR ::')){
if(! mapSupplierWithAddress.get(supplierId).contains('ERROR ::')){
MTX_AccountAddress__c accAddress = new MTX_AccountAddress__c();
accAddress.Account__c = mapSupplierWithAccount.get(supplierId) ;
accAddress.Address__c = mapSupplierWithAddress.get(supplierId) ;
accountAddresses.add(accAddress) ;
}
Contact con = supplierContacts.get(supplierId);
con.AccountId = mapSupplierWithAccount.get(supplierId) ;
supplierContacts.put(supplierId, con);
MTX_SupplierOracleAP__c supplier = new MTX_SupplierOracleAP__c(Id = supplierId);
supplier.ProcessDate__c = system.now();
supplier.ProcessStatus__c = 'Success' ;
//supplier.ErrorDescription__c = oracleSupplier.ErrorDescription__c ;
suppliersWithStatus.add(supplier);
}
else {
MTX_SupplierOracleAP__c supplier = new MTX_SupplierOracleAP__c(Id = supplierId);
supplier.ProcessDate__c = system.now();
supplier.ProcessStatus__c = 'Error' ;
supplier.ErrorDescription__c = mapSupplierWithAccount.get(supplierId) ;
suppliersWithStatus.add(supplier);
}
}
insert supplierContacts.values();
insert accountAddresses ;
}
public void sendEmail(){
// Reference the attachment page, pass in the account ID
PageReference supplierPage = Page.MTX_SupplierLogFile;
// Take the PDF content
Blob b = supplierPage .getContent();
OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress limit 1];
// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('supplierLog.txt');
efa.setBody(b);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Sets the paramaters of the email
email.setSubject( 'Test Email for Supplier Log');
email.setToAddresses( new List<String>{'VR00471039@techmahindra.com'});
email.setPlainTextBody( 'Hello PFA.');
email.setOrgWideEmailAddressId(owa.id);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}