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
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue 

Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION

Trying to modify a test trigger for the Chat Up app as advised in the documentation, but my test fails.

 

/* Create 1 test recurring donation */
npe03__Recurring_Donation__c a = new npe03__Recurring_Donation__c(name = 'testRecurringDonation',npe03__Contact__c = '003J0000006W3j4',npe03__Organization__c
='001J0000007ZAD7');
insert a;

 

I get this error:

Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You must select an Organization or Contact: []

 

The code provided in the documentation only included adding the name field, so after the first time I got this error, I added the fields for Contact and Organization, but I still get the same error.

 

Additional info: in the form, Contact and Organization (aka Account) are lookup fields, and you would normally enter a Contact or an Organization, but not both- I'm using the Nonprofit Starter Pack, so there is a person Account associated with each Contact.  The IDs used in the code reflect the ID of the Contact and the Contact's associated Account (aka Organization) 

 

The sample code for the test trigger is provided in this documentation:

 

What am I missing?

 

Thanks, Amanda

 

Best Answer chosen by Admin (Salesforce Developers) 
Devendra@SFDCDevendra@SFDC

Hi,

 

Looking at this "Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You must select an Organization or Contact: []" error, there might be a validation rule on that object, which is causing record insert problem.

 

Thanks,

Devendra

All Answers

Devendra@SFDCDevendra@SFDC

Hi,

 

I think you need to append Namespace prefix for Name field as well,

 

npe03__name = 'testRecurringDonation'

 

Thanks,

Devendra

Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue
I double-checked the object and the export of that table, and the field is listed only as "Name"

But I gave it a try anyway, and got this error:

Error: Compile Error: Invalid field npe03__name for SObject npe03__Recurring_Donation__c at line 9 column 90


Must be something else...
Devendra@SFDCDevendra@SFDC

Hi,

 

Looking at this "Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You must select an Organization or Contact: []" error, there might be a validation rule on that object, which is causing record insert problem.

 

Thanks,

Devendra

This was selected as the best answer
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue

Thanks, Devendra, it’s a move in the right direction!

 

There were no validation rules on the custom object; however, I looked at the page layout for the object and found 7 fields that were required on the layout. 

 

So after adding values for those 7 fields, the code changed to:

        /* Create 1 test recurring donation */
        npe03__Recurring_Donation__c a = new npe03__Recurring_Donation__c(name  = 'testRecurringDonation',npe03__Contact__c = '003J0000006W3j4',RecordTypeId = '012A0000000qshA',npe03__Date_Established__c
 = system.today(), npe03__Amount__c = 1,npe03__Installment_Period__c = 'Monthly', npe03__Schedule_Type__c = 'Divide By',npe03__Installments__c = 12,Designation_Type__c = 'Unrestricted'
);
        insert a;

 and when I ran the test, I got a different error:

 

ChatterOpptyOnRecurringDonation: execution of AfterInsert

caused by: System.SObjectException: Invalid field npe03__Recurring_Donation__c.id for Opportunity

Class.ChatterUpHelper.chatterUp: line 63, column 1
Trigger.ChatterOpptyOnRecurringDonation: line 36, column 1: []

 

For this forum, would the etiquette be to drop this post here, and start a new one for the different error, or continue?  (I can modify code, but I'm not an Apex programmer per say.  Still hammering at trying to figure out myself)

 

In case of continuing, here's the code, I'm honestly not sure if the problem is the first or second reference to a.id or npe03__Recurring_Donation__c.id:

@isTest
private class testChatterOpptyOnRecurringDonations {
    static testMethod void testChatterOppty() {
        List<Opportunity> myOpportunities = new List<Opportunity>();
        Chatter_Up__c settings = createChatterUpConfig();
        insert settings;
        
        /* Create 1 test recurring donation */
        npe03__Recurring_Donation__c a = new npe03__Recurring_Donation__c(name  = 'testRecurringDonation',npe03__Contact__c = '003J0000006W3j4',RecordTypeId = '012A0000000qshA',npe03__Date_Established__c
 = system.today(), npe03__Amount__c = 1,npe03__Installment_Period__c = 'Monthly', npe03__Schedule_Type__c = 'Divide By',npe03__Installments__c = 12,Designation_Type__c = 'Unrestricted'
);
        insert a;
        
        /* Create 100 test opportunities */
        for(Integer i=0; i < 100; i++){
            Opportunity o = new Opportunity(Name = 'TestOpp_'+i);
            o.npe03__Recurring_Donation__c = a.id;
            o.CloseDate = date.Today()+i;
            o.StageName = 'Negotiation/Review';
            myOpportunities.add(o);
        }
        
        test.startTest();
        insert myOpportunities;
        test.stopTest();
        
        /*there should be 100 posts as you iterated 100 times*/
        List<npe03__Recurring_Donation__Feed> chatterPosts = [select id from npe03__Recurring_Donation__Feed where parentId = :a.id];
        System.assertEquals(chatterPosts.size(), 100);

    }
    
    static private Chatter_Up__c createChatterUpConfig() {
        
        Chatter_Up__c setting = new Chatter_Up__c();
        
        setting.name = 'Opportunity Settings';
        setting.object_api_name__c = 'Opportunity';
        setting.parent_id_field__c = 'npe03__Recurring_Donation__c.id';
        setting.chatter_post_body__c = 'This is a test';
        setting.chatter_link_title__c = 'This is test title';
        
        return setting;
    }     
}

 

 

 

 

 

Sean TanSean Tan

The error you're getting most likely boils down to the chatter config that you're setting up. I've highlighted the line in bold:

 

Chatter_Up__c setting = new Chatter_Up__c();

setting.name = 'Opportunity Settings';
setting.object_api_name__c = 'Opportunity';
setting.parent_id_field__c = 'npe03__Recurring_Donation__c.id';
setting.chatter_post_body__c = 'This is a test';
setting.chatter_link_title__c = 'This is test title';

return setting;

 

This is most likely causing some of you're code to look at an incorrect field on the Opportunity object. This most likely needs to be changed simply to:

 

setting.parent_id_field__c = 'npe03__Recurring_Donation__c';

 

I'd also be careful about hardcoding Id's in your test methods, it's not really good practice and all depedencies and test data should be managed in the test class itself.

 

Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue

That was it.  The test passed successfully.

 

I'm not at all surprised that hard-coding the id fields is not ideal, it was just the fastest work-around.  Would you suggest creating a Contact and an Account in my test code (in a similar way the Recurring Donation was created), and then using those values to plug into the Recurring Donation required fields?  I think there's enough sample code here for me to figure it out, although there's bound to be a hurdle or two.

 

Thanks, A

Sean TanSean Tan

Yes what you described is the right way of handling it. Inserting you're own Contact / Account and plugging the Id's is what should be done assuming you have the time to do it.