trigger
trigger OpportunityTrigger on Opportunity (before insert) {
Set<String> oppSet = new Set<String>();
for(Opportunity opp:Trigger.New) {
oppSet.add(opp.AccountId);
}
List<Account> acctList = [SELECT Id, ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode FROM Account WHERE Id IN:oppSet];
Map<String, Account> accMap = new Map<String, Account>();
for(Account acct:acctList) {
accMap.put(acct.Id, acct);
}
for(Opportunity opp:Trigger.new) {
Account acct = accMap.get(opp.AccountId);
if (opp.Shipping_Address__c == NULL) { opp.Shipping_Address__c = acct.ShippingStreet; }
if (opp.Shipping_City__c == NULL) { opp.Shipping_City__c = acct.ShippingCity; }
if (opp.Shipping_State__c == NULL) { opp.Shipping_State__c = acct.ShippingState; }
if (opp.Shipping_Zip__c == NULL) { opp.Shipping_Zip__c = acct.ShippingPostalCode; }
}
}
and the test class
Thanks,@isTest
public class OpportunityTriggerTest {
@isTest public static void OpportunityTest() {
Account acct = new Account (Name = 'test 123',
ShippingState = 'CA',
ShippingStreet = '1234 drive',
ShippingCity = 'Buellton');
Insert(acct);
Opportunity opp = new Opportunity(Name='test opp',
AccountId=acct.id,
StageName = 'W.Win',
CloseDate = system.today(),
of_Doors__c = 0);
test.startTest();
insert(opp);
System.assertEquals(acct.ShippingStreet, opp.Shipping_Address__c);
test.stopTest();
}
}
Hi Mathew,Everything seems to be fine in your test class except one SOQL prior to assert. you need to query Opp from the database to do the assert.Use following test class code it should work
Thanks,Himanshu@isTest
public class OpportunityTriggerTest {
@isTest public static void OpportunityTest() {
Account acct = new Account (Name = 'test 123',
ShippingState = 'CA',
ShippingStreet = '1234 drive',
ShippingCity = 'Buellton');
Insert(acct);
Opportunity opp = new Opportunity(Name='test opp',
AccountId=acct.id,
StageName = 'W.Win',
CloseDate = system.today(),
of_Doors__c = 0);
test.startTest();
insert(opp);
Opportunity insertedopp = [select Shipping_Address__c from opportunity where id =: opp.id];
System.assertEquals(acct.ShippingStreet, insertedopp.Shipping_Address__c);
test.stopTest();
}
}