0

I have a DML statement inserting a new User into the Org. The JS is as follows:

var action = component.get("c.saveUser");
action.setParams({
        "user" : newUser,
        "profile" : profileName,
        "language" : selectedLanguage,
        "state" : selectedState
});

action.setCallback(this, function(response){
var state = response.getState();
    if(state === 'SUCCESS'){
        var name = response.getReturnValue()
        alert("From server: " + name);;
    }
    else{
        var errors = response.getError();
        console.log('There was an error: ' + response.getError());
        console.log('Error message: ' + errors[0].message);
        alert('User is being created but with a State Error');
    }
});
$A.enqueueAction(action);

I know the logic in my Apex is correct, since all the triggers I've created, along with the test classes, work as intended. The User and the related records I create are inserted into the database. However, the state comes back as 'ERROR'. I'm curious how the server can return an 'ERROR' response but still perform the DML statement correctly. The error messages that I log come back as "There was an error: [object Object]" and then "Error message: undefined"

The Apex:

@AuraEnabled
public static User saveUser(User user, String profile, String language, String state){
    
    Map<String, String> languageInfo = new Map<String, String>();
    DescribeFieldResult field = Schema.User.LanguageLocaleKey.getDescribe();
    for(Schema.PicklistEntry f : field.getPicklistValues()){
        languageInfo.put(f.getLabel(), f.getValue());
    }
    
    //if(Schema.SObjectType.Profile.isAccessible()){
        EDO_Profiles_Setting__mdt[] profiles = [SELECT ProfileId__c, Display_Label__c, MasterLabel FROM EDO_Profiles_Setting__mdt WHERE MasterLabel =: profile];
        if(profiles.size() > 0){
            user.profileId = profiles[0].ProfileId__c;
        }
        user.Reset_Password__c = true;
        if(state != ''){
            user.State = state;
        }
        user.Username = user.Email;
        user.Alias = user.Email.SubStringBefore('@');
        if(user.Alias.length() > 8){
            user.Alias = user.Alias.substring(0,8);
        }
        user.TimeZoneSidKey = UserInfo.getTimeZone().toString();
        user.EmailEncodingKey = 'ISO-8859-1'; // seems to be the default Salesforce User's have
        if(languageInfo.ContainsKey(language)){
            user.LanguageLocaleKey = languageInfo.get(language);
            user.LocaleSidKey = languageInfo.get(language);
        }
        else{
            //default user.LanguageLocaleKey
            //default user.LocaleSidKey
        }
        
        
        //if(!Schema.SObjectType.User.isCreateable()){ //check if this is preventing form from working correctly
            //try{
            //
            Database.DMLOptions dlo=new Database.DMLOptions();
            dlo.EmailHeader.triggerUserEmail =true;
            //Database.insert(user,dlo); 
            user.setOptions(dlo);
            insert user;
            //}
            //catch(Exception ex){
                //ApexPages.addMessages(ex);
            //}
        //}
    //}
    return user;
}
  • What does your Apex look like? – sfdcfox Oct 19 at 15:00
  • @sfdcfox added the Apex code – ThatBrayKid Oct 19 at 15:48
  • Can you check by converting object to json - console.log('There was an error: ' + JSON.stringify( response.getError() ) ); – Ysr Shk Oct 19 at 16:28

Your Answer

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

Browse other questions tagged or ask your own question.