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
eriktowneriktown 

Problem with custom settings in test org

Hello folks,

 

I've just uploaded a beta of my application to my test org for the first time. Everything works fine in my dev org, but there seems to be a problem with the custom settings that are bundled with my application.

 

The first page a user sees when they open my application's tab for the first time is a 'settings' page. As you can see it just lets the user save some authentication info that the app will use later:

 

public class TCSettingsCon {

    public String password { get; set; }

    public String email { get; set; }    

    public PageReference submit() {
        String id = UserInfo.getUserId();
        TCUserInfo__c currentUserInfo = TCUserInfo__c.getInstance(id);
        if (currentUserInfo == null) {
            TCUserInfo__c userinfo = new TCUserInfo__c(SetupOwnerId=id, email__c=email, password__c=password);
            insert userinfo;
        } else {
            currentUserInfo.email__c = email;
            currentUserInfo.password__c = password;
            update currentUserInfo;
        }  
        PageReference ref = new PageReference('/Apex/TCList');
        ref.setRedirect(true);
        return ref; 
    }
}

 Now, I think the problem comes from the fact that my if-condition isn't evaluating the way it did in the dev org. When I did this in the dev org, if the user hadn't configured the application yet by going to this settings page, currentUserInfo would be null. Well, it doesn't seem to be null in the test org, because I get the following error:

 

External entry point

Apex script unhandled exception by user/organization: 005U0000000ca6B/00DU0000000H9Wd

Visualforce Page: /apex/tangocard__TCSettings



caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Class.tc.TCSettingsCon.submit: line 16, column 13

 

As you can see, the if-statement is clearly evaluating as false in this case, so my code runs 'update' instead of 'insert'.

 

So now my question:

 

Does anyone know why the default value of a custom setting would  be null in the dev org, but not in the test org?

 

And, what check can I perform to see if the user has already configured the custom settings, so that I can tell my code to do an insert or an update as appropriate?

 

Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

just update this check

if (currentUserInfo == null) {

 

 

to 

 

if (currentUserInfo == null || currentUserInfo.id == null) {

 it should work for you

All Answers

Shashikant SharmaShashikant Sharma

just update this check

if (currentUserInfo == null) {

 

 

to 

 

if (currentUserInfo == null || currentUserInfo.id == null) {

 it should work for you

This was selected as the best answer
Shashikant SharmaShashikant Sharma

You can also use

upsert currentUserInfo;

instead of 

update currentUserInfo;

 

if you don't want to put check for currentUserInfo.id !=  null as i provided  in last post

 

Both solution should  work.

eriktowneriktown

Thank you Shashikant! That works!