2

I am trying to leverage a custom setting to bypass some validation for a specific update in some APEX. This is not test code coverage so I do not need to create the record first, I should simply be able to query the existing record. I am actually using this same code to query/change the same custom setting in another non-test code class with no issue, however, I am getting the following error in this class:

System.QueryException: List has no rows for assignment to SObject

the part of the code that is causing this is the first line of the following:

    Automation_FO__c  autoFO1 = [select Id, Validation_Rules_DISABLED__c
                            from Automation_FO__c
                            limit 1];autoFO1.Validation_Rules_DISABLED__c = true;
    update autoFO1;         
    try
    {


        update FOsToUpdate;
        update FLIsToUpdate;




    }
    catch(DmlException e)
    {
        system.debug(LoggingLevel.ERROR, e.getMessage());
    }               
    // Change the Custom Setting back to put validation rules back in to effect. case 00012193.
    Automation_FO__c  autoFO2 = [select Id, Validation_Rules_DISABLED__c
                                         from Automation_FO__c
                                         limit 1];
    autoFO2.Validation_Rules_DISABLED__c = false;
    update autoFO2;

I have also heard that putting this in a list might get rid of this error but I do not want to have an empty list returned as this will defeat the purpose of what I am trying to accomplish. Any thoughts as to what is causing this or how to resolve it would be appreciated!


in responce to Das:

I using Hierarchy rather than List custom settings so that I can use them in a validation rule (I believe that List custom settings cannot be referenced in validation rules) . Thus if I create a record, it needs to be specific to a role or profile, which I do not want, I want this to work for everyone. Using "Run As" might be a workaround for this though.

However, even then, this record has no name that I am aware of so I am not sure what to reference in the place where you referenced 'ALPHA'.

There are default org level values that you can check and uncheck and I would like to access these but again, I do not know how to reference these by name as you did with the 'ALPHA'. Any thoughts?

custom setting screen shot

  • Are you absolutely sure there's data in the custom setting ? – Samuel De Rycke Apr 2 '15 at 22:25
1

You need to actually add a new set of data to your custom setting. Once you do, your query will work. But don't just run off and do that, you should rethink your approach to using Custom Settings.

Remember that Custom Settings live in the Application Cache. This is a big deal, one major implication being that we don't need to waste SOQL queries to access them.

The following code blocks assume a data set has been added to your custom setting with a name of 'ALPHA'.

Automation_FO__c cs = Automation_FO__c.getValues('ALPHA');

//toggle your custom setting boolean
cs.Validation_Rules_DISABLED__c = true;
update cs;

//do amazing stuff, no validation rules to bother you

//toggle your custom setting boolean
cs.Validation_Rules_DISABLED__c = false;
update cs;

An even better approach would be to NOT write back to the database. You don't need to. Everything you are doing only needs to exists in the current execution context, its the same difference if you only make the change in the Application Cache.

Automation_FO__c cs = Automation_FO__c.getValues('ALPHA');

//toggle your custom setting boolean, only impacts current execution context
cs.Validation_Rules_DISABLED__c = true;

//do amazing stuff, no validation rules to bother you

//toggle your custom setting boolean, only impacts current execution context
cs.Validation_Rules_DISABLED__c = false;

Similar approach for a Hierarchy Custom Setting.

Automation_FO__c cs = Automation_FO__c.getValues([SELECT Id, Name FROM Profile WHERE Name = 'System Administrator'].Id);

//toggle your custom setting boolean, only impacts current execution context
cs.Validation_Rules_DISABLED__c = true;

//do amazing stuff, no validation rules to bother you

//toggle your custom setting boolean, only impacts current execution context
cs.Validation_Rules_DISABLED__c = false;
  • Thanks for the detailed answer Das, as well as the good tips for reducing queries and DML statements (always a plus) but I am still having difficulty with this. – Lites Apr 3 '15 at 16:53
  • @Lites are you able to access your data set? Where are you having trouble? – krigi Apr 3 '15 at 17:01
  • sorry, I exceeded my comment character limit and then could not edit because it had been too long. I put more detail in the original question. – Lites Apr 3 '15 at 17:09
  • ah - you are using Hierarchies and not Lists. OK, I'll update my answer shortly. – krigi Apr 3 '15 at 17:37
  • Hi Das, thanks for all the help. that last part actually gave me a null pointer exception though. It did point me in the direction of using the getOrgDefaults method which would compile but did not accomplish what I was trying to fix. I went back and found a way to make it so that I did not need to disable the validation in the first place though so my issue is solved but none of these technically worked if someone is looking at this for future reference. Thanks again Das though for all your help! – Lites Apr 3 '15 at 22:11

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.