You need to sign in to do that
Don't have an account?
rajesh kumar 50
Hi i am new to test classes and i have written a trigger on opportunity can any help me writting test class for th
MY Trigger:
trigger opportunityinsertupdate on Opportunity (before insert,before update) {
if(checkRecursive.runOnce()) {
Set<Id> accountIds = new Set<Id>();
for(Opportunity currentOpportunity: Trigger.New) {
accountIds.add(currentOpportunity.AccountId);
}
Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Super_Region__c from Account Where Id in:accountIds]);
boolean flag=True;
if(Trigger.isInsert){
for(Opportunity opp: trigger.New){
system.debug('opp.name.===='+opp.name);
if(opp.name != null){
if(opp.name.startsWith('FO-')){
opp.CampaignId = '701U0000000QsAA';
}
}
}
for(opportunity o : trigger.new) {
if(accountMap.get(o.AccountId) != null) {
if(((o.Record_Type_Name__c == 'NC Power')||(o.Record_Type_Name__c == 'NC Oil & Gas')||(o.Record_Type_Name__c == 'NC Nuclear')) &&(o.stagename == 'Closed Won') && (o.FS_Included__c == false) && accountMap.get(o.AccountId).Super_Region__c == 'Asia/India') {
o.Name = o.Name + '- FS Opp';
o.stagename = 'Sales Lead';
o.amount = 1;
o.CurrencyIsoCode = 'USD';
o.Target_ShipDate__c = o.CloseDate.addmonths(3);
flag = false;
o.FS_Included__c = true;
}
}
}
}
if(trigger.isUpdate && flag) {
for(opportunity o1:trigger.new) {
if(accountMap.get(o1.AccountId) != null) {
if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')||(o1.Record_Type_Name__c == 'NC Nuclear')) &&(o1.stagename == 'Closed Won') && (o1.FS_Included__c == false) && accountMap.get(o1.AccountId).Super_Region__c == 'Asia/India' /*&& o1.Check__c == false*/) {
Opportunity o2= new opportunity();
o2.name = o1.name+'Fs-opp';
o2.CloseDate = o1.CloseDate;
o2.RecordTypeId = '012U0000000UIoX';
o2.stagename = 'Sales Lead';
o2.amount = 1;
o2.CurrencyIsoCode = 'USD';
o2.Target_ShipDate__c = o1.CloseDate.addmonths(3);
o2.FS_Included__c = true;
insert o2;
}
}
}
}
}
}
and i have tried test class also but i think its completed mess..
My TEST class:
@isTest(SeeAllData=true)
public class TestOppInsertUpdate {
Set<Id> accountIds = new Set<Id>();
Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Super_Region__c from Account Where Id in:accountIds]);
static testMethod Void OppInsert() {
RecordType rtid = new RecordType();
rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India'
and FS_Included__c = false];
Opportunity o = new Opportunity();
o.stagename = 'Sales Lead';
o.amount = 1;
o.CurrencyIsoCode = 'USD';
o.Target_ShipDate__c = o.CloseDate.addmonths(3);
//flag = false;
test.startTest();
insert o;
test.stopTest();
}
static testMethod Void Oppupdate() {
Set<Id> accountIds = new Set<Id>();
Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Super_Region__c from Account Where Id in:accountIds]);
RecordType rtid = new RecordType();
rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India'
and FS_Included__c = false];
if(trigger.isUpdate) {
// for(opportunity o1:trigger.new) {
if(accountMap.get(opp.AccountId) != null) {
if(((opp.Record_Type_Name__c == 'NC Power')||(opp.Record_Type_Name__c == 'NC Oil & Gas')||(opp.Record_Type_Name__c == 'Controls Field Service')) &&(opp.stagename == 'Closed Won') && (opp.FS_Included__c == false) && accountMap.get(opp.AccountId).Super_Region__c == 'Asia/India') {
Opportunity o = new Opportunity();
o.stagename = 'Sales Lead';
o.amount = 1;
o.CurrencyIsoCode = 'USD';
o.Target_ShipDate__c = o.CloseDate.addmonths(3);
//flag = false;
//check__C = true;
test.startTest();
update o;
test.stopTest();
}
}
//}
}
}
}
please can any one help me out of this.
thanks in advance
trigger opportunityinsertupdate on Opportunity (before insert,before update) {
if(checkRecursive.runOnce()) {
Set<Id> accountIds = new Set<Id>();
for(Opportunity currentOpportunity: Trigger.New) {
accountIds.add(currentOpportunity.AccountId);
}
Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Super_Region__c from Account Where Id in:accountIds]);
boolean flag=True;
if(Trigger.isInsert){
for(Opportunity opp: trigger.New){
system.debug('opp.name.===='+opp.name);
if(opp.name != null){
if(opp.name.startsWith('FO-')){
opp.CampaignId = '701U0000000QsAA';
}
}
}
for(opportunity o : trigger.new) {
if(accountMap.get(o.AccountId) != null) {
if(((o.Record_Type_Name__c == 'NC Power')||(o.Record_Type_Name__c == 'NC Oil & Gas')||(o.Record_Type_Name__c == 'NC Nuclear')) &&(o.stagename == 'Closed Won') && (o.FS_Included__c == false) && accountMap.get(o.AccountId).Super_Region__c == 'Asia/India') {
o.Name = o.Name + '- FS Opp';
o.stagename = 'Sales Lead';
o.amount = 1;
o.CurrencyIsoCode = 'USD';
o.Target_ShipDate__c = o.CloseDate.addmonths(3);
flag = false;
o.FS_Included__c = true;
}
}
}
}
if(trigger.isUpdate && flag) {
for(opportunity o1:trigger.new) {
if(accountMap.get(o1.AccountId) != null) {
if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')||(o1.Record_Type_Name__c == 'NC Nuclear')) &&(o1.stagename == 'Closed Won') && (o1.FS_Included__c == false) && accountMap.get(o1.AccountId).Super_Region__c == 'Asia/India' /*&& o1.Check__c == false*/) {
Opportunity o2= new opportunity();
o2.name = o1.name+'Fs-opp';
o2.CloseDate = o1.CloseDate;
o2.RecordTypeId = '012U0000000UIoX';
o2.stagename = 'Sales Lead';
o2.amount = 1;
o2.CurrencyIsoCode = 'USD';
o2.Target_ShipDate__c = o1.CloseDate.addmonths(3);
o2.FS_Included__c = true;
insert o2;
}
}
}
}
}
}
and i have tried test class also but i think its completed mess..
My TEST class:
@isTest(SeeAllData=true)
public class TestOppInsertUpdate {
Set<Id> accountIds = new Set<Id>();
Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Super_Region__c from Account Where Id in:accountIds]);
static testMethod Void OppInsert() {
RecordType rtid = new RecordType();
rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India'
and FS_Included__c = false];
Opportunity o = new Opportunity();
o.stagename = 'Sales Lead';
o.amount = 1;
o.CurrencyIsoCode = 'USD';
o.Target_ShipDate__c = o.CloseDate.addmonths(3);
//flag = false;
test.startTest();
insert o;
test.stopTest();
}
static testMethod Void Oppupdate() {
Set<Id> accountIds = new Set<Id>();
Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Super_Region__c from Account Where Id in:accountIds]);
RecordType rtid = new RecordType();
rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India'
and FS_Included__c = false];
if(trigger.isUpdate) {
// for(opportunity o1:trigger.new) {
if(accountMap.get(opp.AccountId) != null) {
if(((opp.Record_Type_Name__c == 'NC Power')||(opp.Record_Type_Name__c == 'NC Oil & Gas')||(opp.Record_Type_Name__c == 'Controls Field Service')) &&(opp.stagename == 'Closed Won') && (opp.FS_Included__c == false) && accountMap.get(opp.AccountId).Super_Region__c == 'Asia/India') {
Opportunity o = new Opportunity();
o.stagename = 'Sales Lead';
o.amount = 1;
o.CurrencyIsoCode = 'USD';
o.Target_ShipDate__c = o.CloseDate.addmonths(3);
//flag = false;
//check__C = true;
test.startTest();
update o;
test.stopTest();
}
}
//}
}
}
}
please can any one help me out of this.
thanks in advance
Here are the guideline which will help you to get test class coverage:
1. As you have hard coded the campaign id, make sure you handle it in your test class.
2. Insert the opportunity record with field values that will cover both IF & Else part of the conditional statement.
3. Use system.assert(), system.assertequls() statements to validate the field values for the records that you created in your test class.
Thanks,
Pratik