I wrote a scheduler class. It is working fine and test coverage also covered, but When I am deploying from sandbox to production then Got an error.
Scheduler Class:
global class PartnerLeadtoAccountScheduler implements Schedulable {
global void execute(SchedulableContext sc) {
map < Id, Account > mapAccount = new map < Id, Account > ();
set < string > checkDuplicateEmail = new set < string > ();
set < Id > updateSet = new set < Id > ();
set < Id > conflictSet = new set < Id > ();
set < String > emailEntry = new set < String > ();
List < Partner_Lead__c > partnerLead = [Select id, Business_Nature__c, Company_Name__c, Contact_Number__c, Contact_Person__c, Date__c, Designation__c, Email_Address__c, Package_Plan__c, OwnerId, CP_Name_Text__c, createddate FROM Partner_Lead__c WHERE Email_Address__c != Null];
system.debug('--partnerLead--' + partnerLead);
List < Partner_Lead__c > partnerLead1 = [Select id, Business_Nature__c, Company_Name__c, Contact_Number__c, Contact_Person__c, Date__c, Designation__c, Email_Address__c, Package_Plan__c, OwnerId, CP_Name_Text__c, createddate FROM Partner_Lead__c WHERE Email_Address__c != Null order by createddate asc];
Map < string, schema.RecordTypeInfo > rtName = schema.SObjectType.Account.getRecordTypeInfosByName();
Id rt = rtName.get('Lead').getRecordTypeId();
Map < string, schema.RecordTypeInfo > rtName1 = schema.SObjectType.Account.getRecordTypeInfosByName();
Id rt1 = rtName.get('Merchant').getRecordTypeId();
mapAccount = new Map < Id, Account > ([SELECT id, Subscription_Email__c, Channel_Partner__c, Closure_Source__c, RecordTypeId FROM Account]);
for (account acc: mapAccount.values()) {
checkDuplicateEmail.add(acc.Subscription_Email__c);
}
system.debug('mapAccount' + mapAccount);
Map < String, Partner_Lead__c[] > assotiactedServiceByEmail = new Map < String, Partner_Lead__c[] > ();
List < Partner_Lead__c > partnerleadToBeUpdated = new List < Partner_Lead__c > ();
List < Account > accountList = new List < Account > ();
List < Account > updatedAccountList = new List < Account > ();
Map < string, Account > mapSAB = new Map < string, Account > ();
Map < string, Account > mapUpdatedSAB = new Map < string, Account > ();
Partner_Lead__c plead;
for (Partner_Lead__c pl: partnerLead) {
plead = new Partner_Lead__c(id = pl.id);
if (!checkDuplicateEmail.contains(pl.Email_Address__c)) {
Account acc = new Account();
acc.LastName = pl.Contact_Person__c;
acc.RecordTypeId = rt;
acc.Business_Type__c = pl.Business_Nature__c;
acc.Company_Name__c = pl.Company_Name__c;
acc.Phone = pl.Contact_Number__c;
acc.Designation__c = pl.Designation__c;
acc.Subscription_Email__c = pl.Email_Address__c;
acc.AccountSource = 'Channel Partner';
acc.Channel_Partner__c = True;
acc.Package_Plan__c = pl.Package_Plan__c;
acc.CP_Name__c = pl.OwnerId;
acc.Cp_Date__c = DateTime.valueOf(pl.createddate).Date();
acc.CP_Name_Text__c = pl.Company_Name__c;
// system.debug('--acc--'+acc.Id);
//plead.Updated__c=true;
accountList.add(acc);
// partnerleadToBeUpdated.add(plead);
}
if (checkDuplicateEmail.contains(pl.Email_Address__c)) {
Map < string, schema.RecordTypeInfo > rtName2 = schema.SObjectType.Account.getRecordTypeInfosByName();
Id rt2 = rtName.get('Lead').getRecordTypeId();
Map < string, schema.RecordTypeInfo > rtName3 = schema.SObjectType.Account.getRecordTypeInfosByName();
Id rt3 = rtName.get('Merchant').getRecordTypeId();
Account ac = [Select Id, Subscription_Email__c, RecordTypeId from Account where Subscription_Email__c = : pl.Email_Address__c Limit 1];
system.debug('--ac--' + ac);
system.debug('--rt3--' + rt3);
system.debug('--rt2--' + rt2);
updatedAccountList.add(ac);
if (mapAccount.get(ac.Id).Closure_Source__c == 'Organic' && mapAccount.get(ac.Id).RecordTypeId == rt3) {
mapAccount.get(ac.Id).Channel_Partner__c = true;
mapAccount.get(ac.Id).Closure_Source__c = 'Channel Partner';
mapAccount.get(ac.Id).CP_Name__c = pl.OwnerId;
mapAccount.get(ac.Id).CP_Name_Text__c = pl.Company_Name__c;
mapAccount.get(ac.Id).Cp_Date__c = pl.Date__c;
mapAccount.get(ac.Id).Package_Plan__c = pl.Package_Plan__c;
//plead.Updated__c=true;
//system.debug('--plead.Updated__c--'+plead.Updated__c);
//mapAccount.get(ac.Id).Updated__c = true;
//inserFlag = false;
//updateFlag = true;
//partnerleadToBeUpdated.add(plead);
} else if (mapAccount.get(ac.Id).Closure_Source__c != 'Organic' && mapAccount.get(ac.Id).RecordTypeId == rt2) {
mapAccount.get(ac.Id).Channel_Partner__c = true;
mapAccount.get(ac.Id).CP_Name__c = pl.OwnerId;
mapAccount.get(ac.Id).CP_Name_Text__c = pl.Company_Name__c;
mapAccount.get(ac.Id).Package_Plan__c = pl.Package_Plan__c;
mapAccount.get(ac.Id).Cp_Date__c = pl.Date__c;
//plead.Updated__c=true;
//system.debug('--plead.Updated__c--'+plead.Updated__c);
//mapAccount.get(ac.Id).Updated__c = true;
//inserFlag = false;
//updateFlag = true;
//partnerleadToBeUpdated.add(plead);
}
}
}
List<Partner_Lead__c> pllist=new list<Partner_Lead__c>();
if(accountList!=null){
for (Account acc1: accountList) {
system.debug('--acc1--' + acc1);
mapSAB.put(acc1.Subscription_Email__c, acc1);
system.debug('--mapSAB--' + mapSAB);
}
}
for (Account acc1: updatedAccountList) {
system.debug('--acc1--' + acc1);
mapUpdatedSAB.put(acc1.Subscription_Email__c, acc1);
system.debug('--mapUpdatedSAB--' + mapUpdatedSAB);
}
if (!mapSAB.values().isEmpty()) {
insert mapSAB.values();
system.debug('--mapSAB.values()--' + mapSAB.values());
}
update mapAccount.values();
//Populating Account lookup on partner lead object
for(Partner_Lead__c pl :partnerLead){
system.debug('--mapSAB acc id--'+mapSAB.get(pl.Email_Address__c));
//system.debug('--mapAccount acc id--'+mapAccount.get(pl.Email_Address__c));
if(mapSAB.get(pl.Email_Address__c)!=null){
pl.Account__c=mapSAB.get(pl.Email_Address__c).id;
system.debug('--pl--'+pl);
}
else if(mapUpdatedSAB.get(pl.Email_Address__c)!=null){
pl.Account__c=mapUpdatedSAB.get(pl.Email_Address__c).id;
system.debug('--pl--'+pl);
}
pllist.add(pl);
system.debug('--pllist!!!!--'+pllist);
}
//List < Partner_Lead__c > pllist = new list < Partner_Lead__c > ();
Integer cnt = 0;
for (Partner_Lead__c pl: partnerLead1) {
cnt++;
plead = new Partner_Lead__c(id = pl.id);
system.debug('--IF-Condition----' + checkDuplicateEmail.contains(pl.Email_Address__c) + ' && ' + !emailEntry.contains(pl.Email_Address__c));
system.debug('--emailEntry-----' + emailEntry);
if (!(emailEntry.contains(pl.Email_Address__c))) {
plead.Updated__c = true;
//updateSet.add(pl.Id);
emailEntry.add(pl.Email_Address__c);
system.debug('--IF-cnt-' + pl.Email_Address__c);
/*
if (mapSAB.get(pl.Email_Address__c) != null) {
plead.Account__c = mapSAB.get(pl.Email_Address__c).id;
system.debug('--plead--' + plead);
}
*/
} else {
system.debug('--else-cnt-' + pl.Email_Address__c);
plead.Conflict__c = true;
// conflictSet.add(pl.Id);
}
partnerleadToBeUpdated.add(plead);
}
system.debug('--accountList--' + accountList);
update partnerleadToBeUpdated;
update pllist;
system.debug('--pllist--' + pllist);
}
}
Apex Test Class:
@isTest(Seealldata=true)
public class TestPartnerleadtoAccountscheduler {
static testmethod void test1()
{
Test.startTest();
Partner_Lead__c P = new Partner_Lead__c();
P.Business_Nature__c='IT';
P.Company_Name__c='PWC';
P.Contact_Number__c='7894561230';
P.Contact_Person__c='jacob';
P.Date__c=System.today();
P.Designation__c='Tester';
P.Email_Address__c='grace1456@gmail.com';
P.Package_Plan__c='1';
P.CP_Name_Text__c='Anurag Avula';
P.Updated__c=true;
insert P;
Profile pro = [SELECT Id FROM Profile WHERE Name='System Administrator'];
/*
User u=new User();
u.Alias = 'standt';
u.Email='newuser3@testorg.com';
u.EmailEncodingKey='UTF-8';
u.LastName='Testing';
u.LanguageLocaleKey='en_US';
u.LocaleSidKey='en_US';
u.ProfileId = pro.Id;
u.TimeZoneSidKey='America/Los_Angeles';
u.UserName='newuser344@testorg.com';
insert u;
*/
Account A = new Account();
A.LastName=P.Contact_Person__c;
A.Subscription_Email__c='grace1456@gmail.com';
A.Channel_Partner__c=true;
A.Closure_Source__c='Organic';
a.Business_Type__c='Product';
a.Company_Name__c='Test';
a.Phone='23456789';
a.Designation__c='Salesforce Developer';
a.AccountSource='Channel Partner';
a.Package_Plan__c='CapTree';
//a.CP_Name__c=u.id;
a.Cp_Date__c=date.today();
a.CP_Name_Text__c='Test';
insert A;
PartnerleadtoAccountscheduler scheUpdateAccount = new PartnerleadtoAccountscheduler();
String sch ='0 57 * * * ?'; //This will run at 57th minute of any given hour.
System.schedule('Schedule to update Account Name', sch,scheUpdateAccount);
Test.stopTest();
}
static testmethod void TestPartnerleadtoAccountscheduler()
{
Test.starttest();
Partner_Lead__c P = new Partner_Lead__c();
P.Business_Nature__c='IT';
P.Company_Name__c='PWC';
P.Contact_Number__c='7894561230';
P.Contact_Person__c='jacob';
P.Date__c=System.today();
P.Designation__c='Tester';
P.Email_Address__c='grace123@gmail.com';
P.Package_Plan__c='1';
P.CP_Name_Text__c='Anurag Avula';
P.Updated__c=true;
insert P;
Profile pro = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u=new User();
u.Alias = 'standt';
u.Email='newuser1@testorg.com';
u.EmailEncodingKey='UTF-8';
u.LastName='Testing';
u.LanguageLocaleKey='en_US';
u.LocaleSidKey='en_US';
u.ProfileId = pro.Id;
u.TimeZoneSidKey='America/Los_Angeles';
u.UserName='newuser1456@testorg.com';
insert u;
Account A = new Account();
A.LastName=P.Contact_Person__c;
A.Subscription_Email__c='grace123@gmail.com';
A.Channel_Partner__c=true;
A.Closure_Source__c='Organic';
a.Business_Type__c='Product';
a.Company_Name__c='Test';
a.Phone='23456789';
a.Designation__c='Salesforce Developer';
a.AccountSource='Channel Partner';
a.Package_Plan__c='CapTree';
a.CP_Name__c=u.id;
a.Cp_Date__c=date.today();
a.CP_Name_Text__c='Test';
insert A;
Test.stoptest();
}
static testmethod void TestPartnerleadtoAccountscheduler1()
{
Test.starttest();
Partner_Lead__c P1 = new Partner_Lead__c();
P1.Business_Nature__c='IT';
P1.Company_Name__c='PWC';
P1.Contact_Number__c='7894561230';
P1.Contact_Person__c='jacob';
P1.Date__c=System.today();
P1.Designation__c='Tester';
P1.Email_Address__c='grace123456@gmail.com';
P1.Package_Plan__c='1';
P1.CP_Name_Text__c='Anurag Avula';
P1.Updated__c=true;
insert P1;
Profile pro1 = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u1=new User();
u1.Alias = 'standt';
u1.Email='newuser1@testorg.com';
u1.EmailEncodingKey='UTF-8';
u1.LastName='Testing';
u1.LanguageLocaleKey='en_US';
u1.LocaleSidKey='en_US';
u1.ProfileId = pro1.Id;
u1.TimeZoneSidKey='America/Los_Angeles';
u1.UserName='newuser14564@testorg.com';
insert u1;
Account A1 = new Account();
A1.LastName=P1.Contact_Person__c;
A1.Subscription_Email__c='grace123456@gmail.com';
A1.Channel_Partner__c=true;
A1.Closure_Source__c='Organic';
a1.Business_Type__c='Product';
a1.Company_Name__c='Test';
a1.Phone='23456789';
a1.Designation__c='Salesforce Developer';
a1.AccountSource='Channel Partner';
a1.Package_Plan__c='CapTree';
a1.CP_Name__c=u1.id;
a1.Cp_Date__c=date.today();
a1.CP_Name_Text__c='Test';
insert A1;
Test.stoptest();
}
}