1

I am trying to create Account, Contact and User from Custom Aura Lightning Component but iam not able to set RecordId for the Record. Below is the Method that iam using for Record Creation. In the parameters iam getting values from Aura Component JS Controller but when iam updating recordId it is showing null don't know why. I've tried with providing hard Code Id for rTypeId but still getting null can you guide me what wrong iam doing here.

LApplicant and SAuthority are the Picklist Values passed in registeringAs Parameter of Apex Method

FYI - The same code iam running in Anonymous block is working and iam getting RecordTypeId there

    public static Final String L_APPLICANT = 'LApplicant';
    public static Final String S_AUTHORITY = 'SAuthority';
    @AuraEnabled
    public static String submitForm(Account act, Contact con, String registeringAs, User usr){
        
        String result = '';
        try{
            resultWrapper uWrap;
            List<AccountContactRelation> acrList = new List<AccountContactRelation>();
            String rTypeId;
            rTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get(registeringAs.equalsIgnoreCase(S_AUTHORITY) ? 'AUTHORITY' : (registeringAs.equalsIgnoreCase(L_APPLICANT) ? 'L_APPLICANT' : 'APPLICANT')).getRecordTypeId();
            
            
            if(con != null && String.isNotBlank(con.Id)){
                acrList = [SELECT Id, AccountId FROM AccountContactRelation WHERE 
                                                                   Account.FirstTime__c = true AND 
                                                                   Account.RecordTypeId = :rTypeId AND 
                                                                   ContactId = :con.Id LIMIT 1];
                                                        
                act.RecordTypeId = rTypeId;
                //There already exists an account so update the same
                if(acrList != null && !acrList.isEmpty()){
                    update con;
                    
                    act.Id = String.isNotBlank(act.Id) ? act.Id : acrList[0].AccountId;
                    update act;
                }
                //Create new Account
                else{
                    act.FirstTime__c = true;
                    insert act;
                    
                    con.AccountId = act.Id;
                    update con;
                    
                    result =  con.Id;
                }
            }
            
            //insert both account and contact
            else{
                system.debug(act.Name);
                if(act.id == null){
                    act.Name = String.isNotBlank(act.Name) ? act.Name : (String.isNotBlank(act.First_Name__c)? act.First_Name__c+' ' : '') +(String.isNotBlank(act.Middle_Name__c)? act.Middle_Name__c+' ' : '')+ (String.isNotBlank(act.Last_Name__c)? act.Last_Name__c : '');
                    act.ownerId = userInfo.getUserId();
                }
                
                
                act.RecordTypeId = rTypeId;
                
                if(string.isBlank(act.MA_Permit_Number__c)){
                    act.MA_Permit_Status__c ='Prospective';
                }
                if( string.isBlank(act.NA_Certification_Number__c) ){
                    act.NA_Certification_Status__c ='Prospective';
                }
                if( string.isBlank(act.NFA_License_Number__c)){
                    act.NFA_License_Status__c ='Prospective';
                }
                system.debug(act);
                act.FirstTime__c = true;
                upsert act;
                
                if(con != null){
                    con.AccountId = act.Id;
                    insert con;
                    //AccountContactRelation acr = new AccountContactRelation();
                    //acr.ContactId = con.Id;
                    //acr.AccountId = act.Id;    
                    //acr.IsDirect = true;
                    //insert acr;
                }
            }
            uWrap = new resultWrapper();
            uWrap.firstName = registeringAs.equalsIgnoreCase(L_APPLICANT) ?   act.First_Name__c : con.FirstName ;
            uWrap.lastName = registeringAs.equalsIgnoreCase(L_APPLICANT) ? act.Last_Name__c  : con.LastName;
            uWrap.email = registeringAs.equalsIgnoreCase(L_APPLICANT) ?  act.primary_email__c : con.Email;
            uWrap.contactId = (con != null && String.isNotBlank(con.Id)) ? con.Id : null ;
            uWrap.registeringAs = registeringAs;
            uWrap.priorEmail = usr != null ? usr.Email : null;
            uWrap.userId = usr != null ? usr.Id : null;
            uWrap.accountId = registeringAs.equalsIgnoreCase(L_APPLICANT) ?  act.Id : null ;
            uWrap.RecordTypeId = rTypeId;
            result += ' ## '+uWrap;
             result += ' @@ ' + createUpdateUser(uWrap);
            
        }
        catch(Exception ex){
            result +=  act +' '+ con+ '##' + ex.getMessage()+' '+ex.getStackTraceString();
        }
        return result;
    }
    
    public class resultWrapper{
        public String firstName;
        public String lastName;
        public String email;
        public String contactId;
        public String registeringAs;
        public String priorEmail;
        public String userId;
        public String accountId;
        public String RecordTypeId;
    }

1 Answer 1

0

Check for the code below, do you have AUTHORITY, L_APPLICANT, and APPLICANT three record types in your org, I think L_APPLICANT may a wrong record type API name.

rTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get(registeringAs.equalsIgnoreCase(S_AUTHORITY) ? 'AUTHORITY' : (registeringAs.equalsIgnoreCase(L_APPLICANT) ? 'L_APPLICANT' : 'APPLICANT')).getRecordTypeId();

Or change your code to

rTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get(registeringAs.equalsIgnoreCase(S_AUTHORITY) ? 'AUTHORITY' : (registeringAs.equalsIgnoreCase(L_APPLICANT) ? 'APPLICANT' : 'L_APPLICANT')).getRecordTypeId();

Debug this statement in the Anonymous Window first.

3
  • Wrapper is just for information but in DML it still showing null and that was my question
    – SFDC_M
    Mar 18, 2021 at 11:01
  • Sorry about the answer.
    – Gary Chen
    Mar 18, 2021 at 11:09
  • Regarding the new Answer - I've tried the statement that iam using in Anonymous block and it is working fine and showing recordTypeId. There is nothing wrong in that code. Whereas if i try to give hardCode Id for this variable(rTypeId ) iam still getting null result.
    – SFDC_M
    Mar 18, 2021 at 11:29

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.