You need to sign in to do that
Don't have an account?
pavan kumar 1864
100%code coverage
Hi all this is my class
Global class UPDATINGFROMOPPORTUNITYTOACCOUNT implements database.batchable<sObject>{
string ferchingaccountrecords;
public UPDATINGFROMOPPORTUNITYTOACCOUNT()
{
ferchingaccountrecords = 'select id,name,Agreement_type__c, Partner_involved__c,Self_servied__c,Edition_type__c from account where type = \'Customer - Direct\'';
}
Global database.querylocator start (database.BatchableContext bc)
{
return database.getquerylocator(ferchingaccountrecords);
}
Global void execute(database.BatchableContext bc,list<account>accountfetched)
{
list<account> newupdatedaccount = new list <account>();
map<id,opportunity> mapofaccountopportunity = new map<id,opportunity>();
for(opportunity opportunityrecords : [select id,Agreement_type__c,type,accountid,Partner_involved__c,Edition_type__c from opportunity
where accountid in :accountfetched
and StageName ='Closed Won' and
(type != 'Services Only' or type !='Paid POC' or type !='Change-Order' or type != 'Amendment')
order by CloseDate desc limit 1])
{
mapofaccountopportunity.put(opportunityrecords.accountid,opportunityrecords);
}
account newaccount = new account();
for(id accountid : mapofaccountopportunity.keyset()){
opportunity opportunityrecord = mapofaccountopportunity.get(accountid);
if(opportunityrecord.Type=='New Logo' || opportunityrecord.Type=='Upgrade' || opportunityrecord.Type=='Renewal' || opportunityrecord.Type=='Cross-Sell')
{
if(opportunityrecord.Partner_involved__c=='True')
{
newaccount.Partner_involved__c = true;
}
else
{
newaccount.Partner_involved__c = false;
}
}
if(opportunityrecord.Edition_type__c!= null)
{
newaccount.Edition_type__c = opportunityrecord.Edition_type__c;
}
else
{
newaccount.Edition_type__c = '';
}
if(opportunityrecord.Agreement_type__c!= null)
{
newaccount.Agreement_type__c = opportunityrecord.Agreement_type__c;
if(opportunityrecord.Agreement_type__c == 'Self-Service')
{
newaccount.Self_servied__c = true;
}
else
{
newaccount.Self_servied__c = false;
}
}
else
{
newaccount.Self_servied__c = false;
newaccount.Agreement_type__c = '';
}
newaccount.Id = opportunityrecord.AccountId;
}
if(newaccount!=null)
{
newupdatedaccount.add(newaccount);
update newupdatedaccount;
}
}
Global void finish(database.BatchableContext bc)
{
}
}
this is my test class
@istest
public class updateaaccountfromopportunity {
@testsetup
public static void method()
{
account acountrecord = new account();
acountrecord.name= 'pavabn';
acountrecord.type= 'Customer - Direct';
insert acountrecord;
opportunity opportunityrecord = new opportunity();
opportunityrecord.name= 'pavan';
opportunityrecord.Agreement_type__c='Order Form';
opportunityrecord.Amount=30;
opportunityrecord.Edition_type__c='';
opportunityrecord.type='Customer - Direct';
opportunityrecord.accountid= acountrecord.id;
opportunityrecord.type= 'New Logo';
opportunityrecord.StageName ='Closed Won';
opportunityrecord.CloseDate= system.today()+5;
opportunityrecord.Stage__c='Stage 3';
insert opportunityrecord;
opportunity opportunityrecord1 = new opportunity();
opportunityrecord1.name= 'pava';
opportunityrecord1.Agreement_type__c='Self-Service';
// aaa.Partner_involved__c =True;
opportunityrecord1.Edition_type__c='Essentials';
opportunityrecord1.type='Customer - Direct';
opportunityrecord1.accountid= acountrecord.id;
opportunityrecord1.type= 'New Customer';
opportunityrecord1.StageName='Qualification';
opportunityrecord1.CloseDate= system.today()+4;
opportunityrecord1.Stage__c='Stage 3';
insert opportunityrecord1;
UPDATINGFROMOPPORTUNITYTOACCOUNT updatingaccount = new UPDATINGFROMOPPORTUNITYTOACCOUNT();
Test.startTest();
id updatingaccountid = database.executebatch(updatingaccount,10);
Test.stopTest();
system.assertEquals(false, acountrecord.Partner_involved__c);
system.assertEquals(null,acountrecord.Edition_type__c);
system.assertEquals(false,acountrecord.Self_servied__c);
system.assertEquals(null,acountrecord.Agreement_type__c);
}
public static testmethod void method2()
{
account acountrecord8 = new account();
acountrecord8.name= 'paaavabn';
acountrecord8.type= 'Customer - Direct';
insert acountrecord8;
opportunity opportunityrecord4 = new opportunity();
opportunityrecord4.name= 'pavaaaaan';
opportunityrecord4.Agreement_type__c='Self-Service';
// aaa.Partner_involved__c =True;
opportunityrecord4.Edition_type__c='';
opportunityrecord4.type='';
opportunityrecord4.accountid= acountrecord8.id;
opportunityrecord4.type= ' ';
opportunityrecord4.StageName='Qualification';
opportunityrecord4.CloseDate= system.today()+5;
opportunityrecord4.Stage__c='Stage 4';
insert opportunityrecord4;
opportunity opportunityrecord2 = new opportunity();
opportunityrecord2.name= 'pavaaaa';
opportunityrecord2.Agreement_type__c=' ';
// aaa.Partner_involved__c =True;
opportunityrecord2.Edition_type__c='';
opportunityrecord2.type='';
opportunityrecord2.accountid= acountrecord8.id;
opportunityrecord2.type= ' ';
opportunityrecord2.StageName='Qualification';
opportunityrecord2.CloseDate= system.today()+4;
opportunityrecord2.Stage__c='Stage 4';
insert opportunityrecord2;
UPDATINGFROMOPPORTUNITYTOACCOUNT updatingaccount1 = new UPDATINGFROMOPPORTUNITYTOACCOUNT();
Test.startTest();
id updatingaccountid = database.executebatch(updatingaccount1,10);
Test.stopTest();
system.assertEquals(true,acountrecord8.Self_servied__c);
}}
Can any one please explain how to achieve 100% coverage and how it can be achieved?
Global class UPDATINGFROMOPPORTUNITYTOACCOUNT implements database.batchable<sObject>{
string ferchingaccountrecords;
public UPDATINGFROMOPPORTUNITYTOACCOUNT()
{
ferchingaccountrecords = 'select id,name,Agreement_type__c, Partner_involved__c,Self_servied__c,Edition_type__c from account where type = \'Customer - Direct\'';
}
Global database.querylocator start (database.BatchableContext bc)
{
return database.getquerylocator(ferchingaccountrecords);
}
Global void execute(database.BatchableContext bc,list<account>accountfetched)
{
list<account> newupdatedaccount = new list <account>();
map<id,opportunity> mapofaccountopportunity = new map<id,opportunity>();
for(opportunity opportunityrecords : [select id,Agreement_type__c,type,accountid,Partner_involved__c,Edition_type__c from opportunity
where accountid in :accountfetched
and StageName ='Closed Won' and
(type != 'Services Only' or type !='Paid POC' or type !='Change-Order' or type != 'Amendment')
order by CloseDate desc limit 1])
{
mapofaccountopportunity.put(opportunityrecords.accountid,opportunityrecords);
}
account newaccount = new account();
for(id accountid : mapofaccountopportunity.keyset()){
opportunity opportunityrecord = mapofaccountopportunity.get(accountid);
if(opportunityrecord.Type=='New Logo' || opportunityrecord.Type=='Upgrade' || opportunityrecord.Type=='Renewal' || opportunityrecord.Type=='Cross-Sell')
{
if(opportunityrecord.Partner_involved__c=='True')
{
newaccount.Partner_involved__c = true;
}
else
{
newaccount.Partner_involved__c = false;
}
}
if(opportunityrecord.Edition_type__c!= null)
{
newaccount.Edition_type__c = opportunityrecord.Edition_type__c;
}
else
{
newaccount.Edition_type__c = '';
}
if(opportunityrecord.Agreement_type__c!= null)
{
newaccount.Agreement_type__c = opportunityrecord.Agreement_type__c;
if(opportunityrecord.Agreement_type__c == 'Self-Service')
{
newaccount.Self_servied__c = true;
}
else
{
newaccount.Self_servied__c = false;
}
}
else
{
newaccount.Self_servied__c = false;
newaccount.Agreement_type__c = '';
}
newaccount.Id = opportunityrecord.AccountId;
}
if(newaccount!=null)
{
newupdatedaccount.add(newaccount);
update newupdatedaccount;
}
}
Global void finish(database.BatchableContext bc)
{
}
}
this is my test class
@istest
public class updateaaccountfromopportunity {
@testsetup
public static void method()
{
account acountrecord = new account();
acountrecord.name= 'pavabn';
acountrecord.type= 'Customer - Direct';
insert acountrecord;
opportunity opportunityrecord = new opportunity();
opportunityrecord.name= 'pavan';
opportunityrecord.Agreement_type__c='Order Form';
opportunityrecord.Amount=30;
opportunityrecord.Edition_type__c='';
opportunityrecord.type='Customer - Direct';
opportunityrecord.accountid= acountrecord.id;
opportunityrecord.type= 'New Logo';
opportunityrecord.StageName ='Closed Won';
opportunityrecord.CloseDate= system.today()+5;
opportunityrecord.Stage__c='Stage 3';
insert opportunityrecord;
opportunity opportunityrecord1 = new opportunity();
opportunityrecord1.name= 'pava';
opportunityrecord1.Agreement_type__c='Self-Service';
// aaa.Partner_involved__c =True;
opportunityrecord1.Edition_type__c='Essentials';
opportunityrecord1.type='Customer - Direct';
opportunityrecord1.accountid= acountrecord.id;
opportunityrecord1.type= 'New Customer';
opportunityrecord1.StageName='Qualification';
opportunityrecord1.CloseDate= system.today()+4;
opportunityrecord1.Stage__c='Stage 3';
insert opportunityrecord1;
UPDATINGFROMOPPORTUNITYTOACCOUNT updatingaccount = new UPDATINGFROMOPPORTUNITYTOACCOUNT();
Test.startTest();
id updatingaccountid = database.executebatch(updatingaccount,10);
Test.stopTest();
system.assertEquals(false, acountrecord.Partner_involved__c);
system.assertEquals(null,acountrecord.Edition_type__c);
system.assertEquals(false,acountrecord.Self_servied__c);
system.assertEquals(null,acountrecord.Agreement_type__c);
}
public static testmethod void method2()
{
account acountrecord8 = new account();
acountrecord8.name= 'paaavabn';
acountrecord8.type= 'Customer - Direct';
insert acountrecord8;
opportunity opportunityrecord4 = new opportunity();
opportunityrecord4.name= 'pavaaaaan';
opportunityrecord4.Agreement_type__c='Self-Service';
// aaa.Partner_involved__c =True;
opportunityrecord4.Edition_type__c='';
opportunityrecord4.type='';
opportunityrecord4.accountid= acountrecord8.id;
opportunityrecord4.type= ' ';
opportunityrecord4.StageName='Qualification';
opportunityrecord4.CloseDate= system.today()+5;
opportunityrecord4.Stage__c='Stage 4';
insert opportunityrecord4;
opportunity opportunityrecord2 = new opportunity();
opportunityrecord2.name= 'pavaaaa';
opportunityrecord2.Agreement_type__c=' ';
// aaa.Partner_involved__c =True;
opportunityrecord2.Edition_type__c='';
opportunityrecord2.type='';
opportunityrecord2.accountid= acountrecord8.id;
opportunityrecord2.type= ' ';
opportunityrecord2.StageName='Qualification';
opportunityrecord2.CloseDate= system.today()+4;
opportunityrecord2.Stage__c='Stage 4';
insert opportunityrecord2;
UPDATINGFROMOPPORTUNITYTOACCOUNT updatingaccount1 = new UPDATINGFROMOPPORTUNITYTOACCOUNT();
Test.startTest();
id updatingaccountid = database.executebatch(updatingaccount1,10);
Test.stopTest();
system.assertEquals(true,acountrecord8.Self_servied__c);
}}
Can any one please explain how to achieve 100% coverage and how it can be achieved?
Hi Pavan,
This question has been already posed on June 14,
https://developer.salesforce.com/forums/?id=9062I000000DLUp
So I am marking it as the duplicate.
Regards,
Priya Ranjan
Salesforce Support