You need to sign in to do that
Don't have an account?
SFDC_BigDog
Code Coverage 0% after including trigger.oldmap in the code
I am unable to resolve my test class. Before including trigger.oldmap it was 80% coverage. Now it is 0%. Any suggestions could be helpful for me.
Thank you.
Trigger:
Test Class:
Thank you.
Trigger:
trigger populateOpportunityfromContact on Opportunity (before insert , before update) { Set<ID> ConIds = new Set<ID>(); for(Opportunity opp : trigger.new) { ConIds.add(opp.RSM_Shipping_Contact__c); } list <contact> conlist = [SELECT Email,Id,MailingCity,MailingCountry,MailingPostalCode,MailingState,MailingStreet,Phone FROM Contact where id IN:ConIds]; MAP<ID , contact> mapCon = new MAP<ID , Contact>(); for(Contact c : conlist) { mapcon.put(c.id,c); } for(Opportunity opp : trigger.new) { if(trigger.oldmap.get(opp.Id).RSM_Shipping_Contact__c != opp.RSM_Shipping_Contact__c) { if(opp.RSM_Shipping_Contact__c!=null) { if(mapcon.containskey(opp.RSM_Shipping_Contact__c)) { contact c = mapcon.get(opp.RSM_Shipping_Contact__c); opp.Shipping_Street__c = c.MailingStreet; opp.Shipping_City__c = c.MailingCity; opp.Shipping_State__c = c.MailingState; opp.Shipping_Country__c= c.MailingCountry; opp.Shipping_postal_code__c = c.MailingPostalCode; opp.Shipping_Email__c = c.Email; opp.Shipping_Phone__c = c.phone; } } else { opp.Shipping_Street__c = null; opp.Shipping_City__c = null; opp.Shipping_State__c = null; opp.Shipping_Country__c= null; opp.Shipping_postal_code__c = null; opp.Shipping_Email__c = null; opp.Shipping_Phone__c = null; } } } }
Test Class:
@istest public class populateOpportunityfromContactTestclass { @testSetup static void setup() { contact c = new contact(); c.lastname = 'Gopi Jayaram'; c.mailingstreet = '1409 Roper Mountain Road'; c.mailingcity = 'Greenville'; c.mailingstate = 'South Carolina'; c.mailingcountry = 'United State of America'; c.mailingpostalcode = '29615'; c.email = 'gopijayaram@gmail.com'; c.phone = '4053786543'; insert c; opportunity o = new opportunity(); o.name = 'Gopi Jayaram ATT'; o.RSM_Shipping_Contact__c = c.id; o.stagename = 'prospecting'; o.closedate = Date.today(); insert o; } Static testMethod void insertItemNull() { Opportunity op = [Select id, name,Shipping_Street__c from opportunity where name = 'Gopi Jayaram ATT' ]; // contact ct = [Select id,email,phone,mailingstreet,mailingcity,mailingstate,mailingcountry,mailingpostalcode from contact where name = 'Gopi Jayaram' ]; System.assertnotequals(op.Shipping_Street__c,null); } Static testMethod void insertItem() { Opportunity op = [Select id, name,Shipping_Street__c from opportunity where name = 'Gopi Jayaram ATT' ]; contact ct = [Select id,email,phone,mailingstreet,mailingcity,mailingstate,mailingcountry,mailingpostalcode from contact where name = 'Gopi Jayaram' ]; System.assertequals(op.Shipping_Street__c,ct.mailingstreet); } }
Try the below test class:
@istest
public class populateOpportunityfromContactTestclass
{
@testSetup static void setup()
{
contact c = new contact();
c.lastname = 'Gopi Jayaram';
c.mailingstreet = '1409 Roper Mountain Road';
c.mailingcity = 'Greenville';
c.mailingstate = 'South Carolina';
c.mailingcountry = 'United State of America';
c.mailingpostalcode = '29615';
c.email = 'gopijayaram@gmail.com';
c.phone = '4053786543';
insert c;
contact c2 = new contact();
c2.lastname = 'Jim Carey';
c2.mailingstreet = '1200 Beverely Hills';
c2.mailingcity = 'Beverely Hills';
c2.mailingstate = 'CA';
c2.mailingcountry = 'USA';
c2.mailingpostalcode = '77777';
c2.email = 'jcarey@test.com';
c2.phone = '4053786544';
insert c2;
Test.startTest();
opportunity o = new opportunity();
o.name = 'Gopi Jayaram ATT';
o.RSM_Shipping_Contact__c = c.id;
o.stagename = 'prospecting';
o.closedate = Date.today();
insert o;
o.RSM_Shipping_Contact__c = c2.id;
update o;
Test.stopTest();
}
Static testMethod void insertItemNull()
{
Opportunity op = [Select id, name,Shipping_Street__c from opportunity where name = 'Gopi Jayaram ATT' ];
// contact ct = [Select id,email,phone,mailingstreet,mailingcity,mailingstate,mailingcountry,mailingpostalcode from contact where name = 'Gopi Jayaram' ];
System.assertnotequals(op.Shipping_Street__c,null);
}
Static testMethod void insertItem()
{
Opportunity op = [Select id, name,Shipping_Street__c from opportunity where name = 'Gopi Jayaram ATT' ];
contact ct = [Select id,email,phone,mailingstreet,mailingcity,mailingstate,mailingcountry,mailingpostalcode from contact where name = 'Gopi Jayaram' ];
System.assertequals(op.Shipping_Street__c,ct.mailingstreet);
}
}
Hope this helps!
Thanks!
{
Set<ID> ConIds = new Set<ID>();
for(Opportunity opp : trigger.new)
{
ConIds.add(opp.RSM_Shipping_Contact__c);
}
list <contact> conlist = [SELECT Email,Id,MailingCity,MailingCountry,MailingPostalCode,MailingState,MailingStreet,Phone FROM Contact where id IN:ConIds];
MAP<ID , contact> mapCon = new MAP<ID , Contact>();
for(Contact c : conlist)
{
mapcon.put(c.id,c);
}
for(Opportunity opp : trigger.new)
{
if(Trigger.isUpdate){
Opportunity oldopp = Trigger.oldMap.get(opp.Id);
if(opp.RSM_Shipping_Contact__c != oldopp.RSM_Shipping_Contact__c )
{
if(opp.RSM_Shipping_Contact__c!=null)
{
if(mapcon.containskey(opp.RSM_Shipping_Contact__c))
{
contact c = mapcon.get(opp.RSM_Shipping_Contact__c);
opp.Shipping_Street__c = c.MailingStreet;
opp.Shipping_City__c = c.MailingCity;
opp.Shipping_State__c = c.MailingState;
opp.Shipping_Country__c= c.MailingCountry;
opp.Shipping_postal_code__c = c.MailingPostalCode;
opp.Shipping_Email__c = c.Email;
opp.Shipping_Phone__c = c.phone;
}
}
}
else
{
opp.Shipping_Street__c = null;
opp.Shipping_City__c = null;
opp.Shipping_State__c = null;
opp.Shipping_Country__c= null;
opp.Shipping_postal_code__c = null;
opp.Shipping_Email__c = null;
opp.Shipping_Phone__c = null;
}
}
}
}
TEST CLASS
@istest(seealldata=true)
public class populateOpportunityfromContactTestclass
{
Static testMethod void insertItemNull()
{
contact c = new contact();
c.lastname = 'Gopi Jayaram';
c.mailingstreet = '1409 Roper Mountain Road';
c.mailingcity = 'Greenville';
c.mailingstate = 'South Carolina';
c.mailingcountry = 'United State of America';
c.mailingpostalcode = '29615';
c.email = 'gopijayaram@gmail.com';
c.phone = '4053786543';
insert c;
opportunity o = new opportunity();
o.name = 'Gopi Jayaram ATT';
o.stagename = 'prospecting';
o.closedate = Date.today();
insert o;
o.RSM_Shipping_Contact__c = null;
upsert o;
Opportunity op = [Select id, name,Shipping_Street__c,RSM_Shipping_Contact__c from opportunity where id=:o.Id];
// contact ct = [Select id,email,phone,mailingstreet,mailingcity,mailingstate,mailingcountry,mailingpostalcode from contact where name = 'Gopi Jayaram' ];
System.assertequals(op.Shipping_Street__c,null);
System.assertEquals(op.RSM_Shipping_Contact__c, null);
}
Static testMethod void insertItem()
{
contact c = new contact();
c.lastname = 'Gopi Jayaram';
c.mailingstreet = '1409 Roper Mountain Road';
c.mailingcity = 'Greenville';
c.mailingstate = 'South Carolina';
c.mailingcountry = 'United State of America';
c.mailingpostalcode = '29615';
c.email = 'gopijayaram@gmail.com';
c.phone = '4053786543';
insert c;
opportunity o = new opportunity();
o.name = 'Gopi Jayaram ATT';
o.stagename = 'prospecting';
o.closedate = Date.today();
insert o;
o.Probability = 100.00;
o.RSM_Shipping_Contact__c = c.id;
o.Shipping_City__c = c.MailingCity;
o.Shipping_Street__c = c.MailingStreet;
upsert o;
Opportunity op = [Select id, name,Shipping_Street__c,Shipping_State__c, RSM_Shipping_Contact__c,
Shipping_City__c,Shipping_Postal_Code__c,Shipping_Country__c,Shipping_Email__c,Shipping_Phone__c from Opportunity where Name= 'Gopi Jayaram ATT'];
contact ct = [Select id,email,phone,mailingstreet,mailingcity,mailingstate,mailingcountry,mailingpostalcode from contact where name = 'Gopi Jayaram' ];
System.assertequals(op.Shipping_Street__c,ct.mailingstreet);
System.assertequals(op.Shipping_State__c,ct.MailingState);
System.assertequals(op.Shipping_city__c,ct.MailingCity);
System.assertequals(op.Shipping_Country__c,ct.MailingCountry);
System.assertequals(op.Shipping_Postal_Code__c,ct.MailingPostalCode);
System.assertequals(op.RSM_Shipping_Contact__c,ct.Id);
}
}
HOPE THIS IS HELPFULL. MARK AS A SOLUTION IF THIS WORKS FOR YOU SO IT WOULD BE A FUTURE REFERENCE TO OTHERS.
THANKS,
ANDY.