function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gvgv 

Test Method failing after adding a validation rule

our salesforce admin added a validation rule on opportunity object and because of which previously working test methods started failing. The validation rule checks if the account has HeadQuarters and its defined as

 

ISNULL( HQ_Account_Region__c)

 

 

When I am trying to move my trigger changes (on a different object), triggers related to opportunity fail because of the above validation rule.

 

here is the test method. can anyone say whats missing here. I have tried adding a HQ_ACCount_region but its a read only field, so I cannot assing any values to it.

 

 

pls.let me know where I am going wrong or how to avoid the error

 

 

testOpportunitySalesGoalSyncTime.testMethodOpportunitySalesGoal System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The account is missing primary billing address so this opportunity will not be routed properly.  To fix it, please go to the account record and add a primary billing address.: [AccountId]

 

This is the error message the above validatio rulehas

 

The account is missing primary billing address so this opportunity will not be routed properly.  To fix it, please go to the account record and add a primary billing address.: [AccountId]

 

Let me know if you have more questions in order to fix this issue

Download Apex
public class testOpportunitySalesGoalSyncTime {

public static testmethod void testMethodOpportunitySalesGoal() {

/* 09/03/08 changed test to create 4 unique Opportunity_Sales_Goal records */

Account acct = createAccount();

Opportunity newOpportunity1 = createOpportunity(acct);
Opportunity newOpportunity2 = createOpportunity(acct);
Opportunity newOpportunity3 = createOpportunity(acct);
Opportunity newOpportunity4 = createOpportunity(acct);

String salesGoalId1 = getSalesGoalId();
String salesGoalId2 = getSalesGoalId();
String salesGoalId3 = getSalesGoalId();
String salesGoalId4 = getSalesGoalId();

createOpportunitySaleGoal(newOpportunity1,salesGoalId1);
createOpportunitySaleGoal(newOpportunity2,salesGoalId2);
createOpportunitySaleGoal(newOpportunity3,salesGoalId3);
createOpportunitySaleGoal(newOpportunity4,salesGoalId4);

newOpportunity1.Name = 'AAA';
update newOpportunity1;

newOpportunity2.Name = 'BBB';
update newOpportunity2;

newOpportunity3.Name = 'CCC';
update newOpportunity3;

newOpportunity4.Name = 'DDD';
update newOpportunity4;


DateTime currentTime = System.now();
for(Opportunity_Sales_Goal__c opportunitySalesGoal:[Select Id,Last_Sync_To_Parent__c from
Opportunity_Sales_Goal__c where Opportunity__c = :newOpportunity1.Id
or Opportunity__c = :newOpportunity2.Id
or Opportunity__c = :newOpportunity3.Id
or Opportunity__c = :newOpportunity4.Id]) {
System.assertEquals(opportunitySalesGoal.Last_Sync_To_Parent__c.day(),currentTime.day());
System.assertEquals(opportunitySalesGoal.Last_Sync_To_Parent__c.month(),currentTime.month());
System.assertEquals(opportunitySalesGoal.Last_Sync_To_Parent__c.year(),currentTime.year());
System.assertEquals(opportunitySalesGoal.Last_Sync_To_Parent__c.hour(),currentTime.hour());
System.assertEquals(opportunitySalesGoal.Last_Sync_To_Parent__c.minute(),currentTime.minute());
}


}

private static void createOpportunitySaleGoal(Opportunity newOpportunity, String salesGoalId){

//for(Integer i=0; i<4 ; i++){
Opportunity_Sales_Goal__c opportunitySalesGoal = new Opportunity_Sales_Goal__c();
opportunitySalesGoal.Opportunity__c = newOpportunity.Id;
opportunitySalesGoal.Sales_Goal__c = salesGoalId;

DateTime dtTime = DateTime.newInstance(1980,1,1);
opportunitySalesGoal.Last_Sync_To_Parent__c = dtTime;
insert opportunitysalesgoal;
//}

}
private static Opportunity createOpportunity(Account acct){

Opportunity newOpportunity = new Opportunity();
newOpportunity.Name = 'Testing Values';
newOpportunity.Account = acct;
newOpportunity.StageName = 'Qualifying';
newOpportunity.CloseDate = Date.today();

insert newOpportunity;
return newOpportunity;

}
private static Account createAccount(){



Account account = [ Select a.Name from Account a
where a.HQ_Account_Region__c <> null
and A.billingCountry <> Null limit 1];





//Account account = new Account();
//account.Name = 'TestAccount';
//Account.BillingCountry = 'United States';

//insert account;
return account;
}

private static String getSalesGoalId(){

Sales_Goal__c newSalesGoal = new Sales_Goal__c();
newSalesGoal.Description__c = 'Testing Sales Goal';
newSalesGoal.no_Required__c=1;
insert newSalesGoal;
return newSalesGoal.Id;

}
 
}
public 

 

 

 

 

 

 

 

 

 

\

visulaforcevisulaforce

check whether createAccount method is returning a record (not null) and is there any trigger on opporutnity. If it is then send the code for trigger.