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
saad mechiche alamisaad mechiche alami 

Create custom settings for a unit test

hello, 

I need to create a custom setting for my unit test 
the custom setting object name is called Terms_of_Use
how can i program that ?
many thanks
Best Answer chosen by saad mechiche alami
kevin lamkevin lam
It's the same as creating records for a custom object:
Terms_of_Use__c testRecord = new Terms_of_Use__c(field values);
insert testRecord;

All Answers

kevin lamkevin lam
It's the same as creating records for a custom object:
Terms_of_Use__c testRecord = new Terms_of_Use__c(field values);
insert testRecord;
This was selected as the best answer
ShashForceShashForce
Hi,

You can create a custom setting just like you createa standard or custom objects in your test methods. Please see this if you need some further help: http://salesforce.stackexchange.com/questions/3565/how-to-access-custom-settings-in-test-class-without-using-the-seealldata

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
Arunkumar RArunkumar R
Inserting Custom setting is similar to creating records in Custom objects.

For inserting List Custom Setting,
insert new Terms_of_Use(Name='Test');

or 

Terms_of_Use tu=new Terms_of_Use(Name='Test');
insert tu;

For inserting Hierarchy Custom Setting,
insert new Terms_of_Use(SetupOwnerId=Userinfo.getUserId());

or

Terms_of_Use tu=new Terms_of_Use(SetupOwnerId=Userinfo.getUserId());
insert tu;

You can also insert a test user record or profile and assign those id in SetupOwnerID for hierarchy custom setting..

Thanks,
Arunkumar R
saad mechiche alamisaad mechiche alami
Great , thanks guys :)