I am not a developer and I am trying to copy one existing trigger and test class from the Org and while running the Test Class getting below error-
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, State/Province is required for Australia, United States and Canada.: []
I even have included the BillingState in the Account creation to so that validation rule do not trigger. But still no luck. Can anyone help me in reviewing the below codes?
APEX Trigger-
trigger limitCreationsFromZoominfo on Contact (before insert) {
//This trigger limit to 25 the number of contacts that can be created from Zoominfo per person per month (#33899)
//We can detect that the Contact comes from Zoominfo because: LeadSource = "Zoominfo"
//We need to filter the existing contacts (for the count operation) using the fields:
// CreatedBy = <same user>
// CreatedDate = <in the last 30 days>
// LeadOrigin = "Zoominfo"
// Users with roles "System Administrator", "Denodo Systems Integration" or "Operations Supervisor" should not be affected by this rule.
Id profileId = UserInfo.getProfileId();
String profileName=[Select Id, Name from Profile where Id=:profileId].Name;
if ( !profileName.contains('Operations Supervisor') && !profileName.contains('System Administrator')
&& !profileName.contains('Denodo Systems Integration')){
String userId = UserInfo.getUserId();
Datetime limitDate = System.now().addDays(-30);
List<Contact> contactsToInsertFromZoominfo = new List<Contact>();
for(Contact contactToInsert : System.Trigger.new){
if (contactToInsert.LeadSource == 'Zoominfo'){
contactsToInsertFromZoominfo.add(contactToInsert);
}
}
//if there are insertions from Zoominfo, check the limit
if (contactsToInsertFromZoominfo.size() > 0) {
List<AggregateResult> currentContactsZoominfo = [SELECT Count(Id) currentContacts FROM Contact
WHERE Lead_Origin__c ='Zoominfo' and CreatedDate >= :limitDate and CreatedById = :userId];
for (Contact contactToInsert : contactsToInsertFromZoominfo) {
if ( (Integer.valueOf(currentContactsZoominfo.get(0).get('currentContacts')) + contactsToInsertFromZoominfo.size()) > 25 ){
contactToInsert.addError('You can not import more than 25 contacts from Zoominfo in 30 days.');
}
}
}
}
}
APEX Class Test -
/**
* This class contains unit tests for validating the behavior of Apex classes
* and triggers.
*
* Unit tests are class methods that verify whether a particular piece
* of code is working properly. Unit test methods take no arguments,
* commit no data to the database, and are flagged with the testMethod
* keyword in the method definition.
*
* All test methods in an organization are executed whenever Apex code is deployed
* to a production organization to confirm correctness, ensure code
* coverage, and prevent regressions. All Apex classes are
* required to have at least 75% code coverage in order to be deployed
* to a production organization. In addition, all triggers must have some code coverage.
*
* The @isTest class annotation indicates this class only contains test
* methods. Classes defined with the @isTest annotation do not count against
* the organization size limit for all Apex scripts.
*
* See the Apex Language Reference for more information about Testing and Code Coverage.
*/
@isTest
private class limitCreationsFromZoominfoTest {
static testMethod void myUnitTest() {
Test.startTest();
Profile pISS = [SELECT Id FROM Profile WHERE Name='Inside Sales Specialist'];
User userISS = new User(Alias = 'userISS', UserName='newuserISS@relatedtotest.com', Email = 'test2@relatedtotest.com',
EmailEncodingKey='UTF-8', FirstName = 'testuser2', LastName = 'ISS', ProfileId = pISS.Id,
TimeZoneSidKey='America/Los_Angeles', LocaleSidKey='en_US', LanguageLocaleKey='en_US');
insert userIss;
System.runAs(userISS){
// Seed the database with some accounts, and make sure they can be bulk inserted successfully.
Account account1 = new Account(Name = 'Test1RelatedTo Inc.', BillingCountry = 'United States', BillingState = 'California');
Account[] accts = new Account[] {account1};
insert accts;
List<Contact> contacts = new List<Contact>();
for (Integer i =0 ; i < 26; i++){
String emailc = 'test' + i + '@relatedtotest.com';
contacts.add(new Contact(LastName='Test1', AccountId=account1.Id, Email=emailc, MailingCountry = 'United States', MailingState = 'California', LeadSource='Zoominfo'));
}
contacts.add(new Contact(LastName='Test4', AccountId=account1.Id, Email='testa@relatedtotest.com', MailingCountry = 'United States', MailingState = 'California', LeadSource='Other'));
contacts.add(new Contact(LastName='Test5', AccountId=account1.Id, Email='testb@relatedtotest.com', MailingCountry = 'United States', MailingState = 'California', LeadSource='Web'));
try{
insert contacts;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 101);
System.assert(e.getDmlMessage(0).contains('You can not import more than 25 contacts from Zoominfo in 30 days.'));
}
}
Test.stopTest();
}
}
Any help would be much appreciated.
Thanks!
12 respuestas