1

I am trying to access custom settings via Apex, but it refuses to work properly.

I've got the following two examples:

public class PO_PartnerTeam_OppCustomController  {
   public Integration_Partner_Scoring__c test = new Integration_Partner_Scoring__c.getInstance('Test');
}

The below is anonymous apex::

Integration_Partner_Scoring__c test = [SELECT Id, Name FROM 
Integration_Partner_Scoring__c];
System.debug('test output :: ' + test);

Integration_Partner_Scoring__c test2 =  Integration_Partner_Scoring__c.getAll().values();
System.debug('test2 output :: ' + test2);

Integration_Partner_Scoring__c test3 =  Integration_Partner_Scoring__c.getInstance();
System.debug('test3 output :: ' + test3);

The anonymous Apex works fine, and returns my custom settings just fine. This lets me confirm the custom setting is indeed created, and available. But the class won't, and I cannot figure out why. I will note, I can save Integration_Partner_Scoring__c test = [SELECT Id, Name FROM Integration_Partner_Scoring__c]; just fine in the class(and if I log that to console it gives the expected results), but I am wondering why I cannot get it using the getInstance.

I can compile it without .getInstance() and it works fine just creating a new copy of Integration_Partner_Scoring__c, but it refuses to work with .getInstance(). It just gives me the error Invalid type: Integration_Partner_Scoring__c.getInstance.

Any advice would be appreciated. Thanks!

5

You don't need the new keyword. The following should work. Refer documentation.

public class PO_PartnerTeam_OppCustomController  {
   public Integration_Partner_Scoring__c test =Integration_Partner_Scoring__c.getInstance('Test');
}
  • Well, I feel silly. But I'm also glad it didn't end up being something big. I'll read the documentation and make a mental note of this. Thanks! Appreciate it :) – Genko Oct 10 '17 at 21:10
  • @Genko Can you mark this as the answer? Otherwise, the community will keep bumping this question to top. – Avinash Oct 10 '17 at 21:12
  • I am going to in a bit. I tried but stackexchange threw a 'You cannot mark an answer for 6 minutes' message. Just waiting on that. – Genko Oct 10 '17 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.