You need to sign in to do that
Don't have an account?
Nagendra Babu 55
how to write test class for my class
global class ReportIssueContactAssociationBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator([SELECT Id, Contact__c, Email__c FROM Issue__c WHERE Contact__c = NULL]);
}
global void execute(Database.BatchableContext bc, List<sObject> scope){
// loop through all Issue records and collect email addresses
// Query Contacts based on Email Addresses
// Create a Map of Email -- Contact
Set<String> allEmails = new Set<String>();
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
allEmails.add(issue.Email__c);
}
Map<String, String> contactEmailContactIdMap = new Map<String, String>();
for(Contact c : [SELECT id, Email FROM Contact WHERE EMail IN :allEmails]){
contactEmailContactIdMap.put(c.Email, c.Id);
}
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
issue.Contact__c = contactEmailContactIdMap.get(issue.Email__c);
}
Database.update(scope);
}
global void finish(Database.BatchableContext bc){
}
}
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator([SELECT Id, Contact__c, Email__c FROM Issue__c WHERE Contact__c = NULL]);
}
global void execute(Database.BatchableContext bc, List<sObject> scope){
// loop through all Issue records and collect email addresses
// Query Contacts based on Email Addresses
// Create a Map of Email -- Contact
Set<String> allEmails = new Set<String>();
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
allEmails.add(issue.Email__c);
}
Map<String, String> contactEmailContactIdMap = new Map<String, String>();
for(Contact c : [SELECT id, Email FROM Contact WHERE EMail IN :allEmails]){
contactEmailContactIdMap.put(c.Email, c.Id);
}
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
issue.Contact__c = contactEmailContactIdMap.get(issue.Email__c);
}
Database.update(scope);
}
global void finish(Database.BatchableContext bc){
}
}
private class classname {
static testMethod void Method(){
Test.startTest();
ReportIssueContactAssociationBatch batchobj = new ReportIssueContactAssociationBatch();
batchobj.execute(null,null);
Test.stopTest();
}
}
the basic structure , what i know . can anyone jus help us out
Please try this code
@isTest
public class AccountUpdateBatchJobTest
{
static testMethod void testMethod1()
{
List<Issue__c> lstIssue = new List<Issue__c>();
for(Integer i=0 ;i <200;i++)
{
Issue__c issue = new Issue__c();
issue.Name ='Name'+i;
lstIssue.add(issue);
}
insert lstIssue;
Test.startTest();
ReportIssueContactAssociationBatch obj = new ReportIssueContactAssociationBatch();
DataBase.executeBatch(obj);
Test.stopTest();
System.AssertNotEquals(null, issue.Contact__c, 'Issue Contact should be filled now');
}
}
I think u are thinking of records which is not there in ur database but tomorrow if u get that record u taught of updating your fields to that record or records on bases of EMAIL ID
deve can you help me out
@isTest
private class ReportIssueContactAssociationBatchTest {
static testMethod void testMethod1()
{
List<Issue__c> lstIssue = new List<Issue__c>();
for(Integer i=0 ;i <200;i++)
{
Issue__c issue = new Issue__c();
issue.Name ='Name'+i;
lstIssue.add(issue);
}
insert lstIssue;
Test.startTest();
ReportIssueContactAssociationBatch obj = new ReportIssueContactAssociationBatch();
DataBase.executeBatch(obj);
Test.stopTest();
System.AssertNotEquals(null, issue.Contact__c, 'Issue Contact should be filled now');
}
}
There is some mistake in that. Please check this
@isTest
private class ReportIssueContactAssociationBatchTest {
static testMethod void testMethod1()
{
List<Issue__c> lstIssue = new List<Issue__c>();
for(Integer i=0 ;i <200;i++)
{
Issue__c issue = new Issue__c();
issue.Name ='Name'+i;
lstIssue.add(issue);
}
insert lstIssue;
Test.startTest();
ReportIssueContactAssociationBatch obj = new ReportIssueContactAssociationBatch();
DataBase.executeBatch(obj);
Test.stopTest();
System.AssertNotEquals(null, lstIssue.get(0).Contact__c, 'Issue Contact should be filled now');
}
}
I think we have reached Nagendra's Criteria
can u explain him.
List lstIssue = new List();
Issue__c issue1 = new Issue__c();*
issue1.Name ='test';
issue1.Email__c = test@test.com ;
lstIssue.add(issue1)
Issue__c issue2 = new Issue__c();
issue2.Name ='test1';
issue2.Email__c = test1@test.com ;
lstIssue.add(issue2); i
nsert lstIssue;
Then run the batch using
ReportIssueContactAssociationBatch obj = new ReportIssueContactAssociationBatch();
DataBase.executeBatch(obj);
Then check in assert that your field Contact__c of Issue__c is updated or not.
Sample Code:
@isTest
private class ReportIssueContactAssociationBatchTest {
static testMethod void testMethod1()
{
List lstIssue = new List();
Issue__c issue1 = new Issue__c();*
issue1.Name ='test';
issue1.Email__c = test@test.com ;
lstIssue.add(issue1)
Issue__c issue2 = new Issue__c();
issue2.Name ='test1';
issue2.Email__c = test1@test.com ;
lstIssue.add(issue2);
insert lstIssue;
Test.startTest();
ReportIssueContactAssociationBatch obj = new ReportIssueContactAssociationBatch();
DataBase.executeBatch(obj);
Test.stopTest();
System.AssertNotEquals(null, lstIssue.get(0).Contact__c, 'Issue Contact should be filled now');
}
}
I got one doubt that in Same process if the Contact__c is not filled and Email id not fill where in i want to search by Phone.
global class ReportIssueContactAssociationBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator([SELECT Id, Contact__c, Email__c FROM Issue__c WHERE Contact__c = NULL]);
}
global void execute(Database.BatchableContext bc, List<sObject> scope){
// loop through all Issue records and collect email addresses
// Query Contacts based on Email Addresses
// Create a Map of Email -- Contact
Set<String> allEmails = new Set<String>();
Set<String> allPhone = new Set<String>();
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
allEmails.add(issue.Email__c);
}
Map<String, String> contactEmailContactIdMap = new Map<String, String>();
Map<String, String> contactPhoneContactIdMap = new Map<String, String>();
if(Email != Null){
for(Contact c : [SELECT id, Email FROM Contact WHERE EMail IN :allEmails]){
contactEmailContactIdMap.put(c.Email, c.Id);
}
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
issue.Contact__c = contactEmailContactIdMap.get(issue.Email__c);
}
Database.update(scope);
}else if(Phone !=Null){
for(Contact c : [SELECT id, phone FROM Contact WHERE EMail IN :allPhone]){
contactEmailContactIdMap.put(c.Phone, c.Id);
}
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
issue.Contact__c = contactPhoneContactIdMap.get(issue.Phone__c);
}
Database.update(scope);
}
}
global void finish(Database.BatchableContext bc){
}
}
You can check this in loop where you are adding values in allEmail list and also you have include this field in query. Please check this and let me know if any doubt
global class ReportIssueContactAssociationBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator([SELECT Id, Contact__c, Email__c, Phone FROM Issue__c WHERE Contact__c = NULL]);
}
global void execute(Database.BatchableContext bc, List<sObject> scope){
// loop through all Issue records and collect email addresses
// Query Contacts based on Email Addresses
// Create a Map of Email -- Contact
Set<String> allEmails = new Set<String>();
Set<String> allPhone = new Set<String>();
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
if(issue.Email__c!=null) {
allEmails.add(issue.Email__c);
}
else {
allPhone.add(issue.Phone);
}
}
Map<String, String> contactEmailContactIdMap = new Map<String, String>();
Map<String, String> contactPhoneContactIdMap = new Map<String, String>();
if(allEmails.size()>0){
for(Contact c : [SELECT id, Email FROM Contact WHERE EMail IN :allEmails]){
contactEmailContactIdMap.put(c.Email, c.Id);
}
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
issue.Contact__c = contactEmailContactIdMap.get(issue.Email__c);
}
Database.update(scope);
}else if(allPhone.size()>0){
for(Contact c : [SELECT id, phone FROM Contact WHERE EMail IN :allPhone]){
contactEmailContactIdMap.put(c.Phone, c.Id);
}
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
issue.Contact__c = contactPhoneContactIdMap.get(issue.Phone__c);
}
Database.update(scope);
}
}
global void finish(Database.BatchableContext bc){
}
}
//Schedule Apex
global class ReportIssueContactAssociationScheduler implements System.Schedulable {
global void execute(SchedulableContext sc){
Database.executeBatch(new ReportIssueContactAssociationBatch());
}
}
TestClass for above schedule apex
@isTest
private class ReportIssueScheduleTest {
static testmethod void SchedulerTest(){
String xx = ' 0 0 1 16 4 ? *';
// Create your test data
Contact acc = new Contact();
acc.FirstName = 'FirstTest';
insert acc;
Test.startTest();
String jobId = System.schedule('ScheduleApexClassTest', xx, new ReportIssueContactAssociationScheduler());
// Add assert here to validate result
}
}
I got one doubt that in Same process if the Contact__c is not filled where Last Name and email is not filled it should search by phone and Last name.
global class ReportIssueContactAssociationBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator([SELECT Id, Contact__c, Email__c, Phone__C,LastName__c FROM Issue__c WHERE Contact__c = NULL]);
}
global void execute(Database.BatchableContext bc, List<sObject> scope){
Set<String> allEmails = new Set<String>();
Set<String> allPhone = new Set<String>();
Set<String> allLastName = new Set<String>();
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
if(issue.Email__c!=null && issue.LastName__c!=null ) {
allEmails.add(issue.Email__c );
allLastName .add(issue.LastName__c );
}
else if(issue.Phone__c!=null && issue.LastName__c!=null ) {
allPhone.add(issue.Phone);
allLastName .add(issue.LastName__c );
}
}
Map<String, String> contactEmailContactIdMap = new Map<String, String>();
Map<String, String> contactPhoneContactIdMap = new Map<String, String>();
Map<String, String> contactLastNameContactIdMap = new Map<String, String>();
if(allEmails.size()>0 && allLastName.size()>0 ){
for(Contact c : [SELECT id, Email,LastName__c FROM Contact WHERE EMail IN :allEmails AND LastName IN:allLastName ]){
contactEmailContactIdMap.put(c.Email, c.Id);
contactLastNameIdMap.put(c.LastName, c.Id);
}
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
issue.Contact__c = contactEmailContactIdMap.get(issue.Email__c);
issue.LastName__c = contactLastNameContactIdMap.get(issue.LastName__c);
}
Database.update(scope);
}else If (allPhone.size()>0 && allLastName.size()>0){
for(Contact c : [SELECT id, phone FROM Contact WHERE EMail IN :allEmails AND LastName IN:allLastName]){
contactEmailContactIdMap.put(c.Phone, c.Id);
contactLastNameIdMap.put(c.LastName, c.Id);
}
for(sObject so : scope){
Issue__c issue = (Issue__c)so;
issue.Contact__c = contactPhoneContactIdMap.get(issue.Phone__c);
issue.LastName__c = contactLastNameContactIdMap.get(issue.LastName__c);
}
Database.update(scope);
}
}
global void finish(Database.BatchableContext bc){
}
}
Then you can use this query
SELECT id, phone FROM Contact WHERE Phone IN :allPhone AND LastName IN:allLastName
if Email is null