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
Hermann OuréHermann Ouré 

apex controller

Hi,
I have created a form on community where a client has to fill in information. when the client submit the form, it creates a contact on Salesforce with all the information from the form. 

All the fields required are filled accordingly except for the field "Contact's Preferred Language" that remains blank. It does not fill with the value selected on the form.
Must certainly have to do with my apex controller.
If anyone could help?
Thanks 
  • here is my code .cmp: 
<aura:component implements="forceCommunity:availableForAllPageTypes" controller="auraRegisterFormController" access="global" >
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    
    <aura:handler name="SendHumanResult" action="{!c.verifiedResult}" event="CLAYH:HumanCheck" />
    
    <aura:attribute name="firstname" type="String" default="" />
    <aura:attribute name="lastname" type="String" default="" />
    <aura:attribute name="company" type="String" default="" />
    <aura:attribute name="email" type="String" default="" />
    <aura:attribute name="phone" type="String" default="" />
    <aura:attribute name="Contact_s_preferred_language" type="String" default="" />
    
    <aura:attribute name="verified" type="Boolean" default="false" />
    
    <div class="registerFormComp">
        <div class="registerFormTitle">NEW USER REGISTRATION</div>
        <h2>Please fill the following form :</h2>
        <lightning:input aura:id="field" name="firstname" label="Firstname" value="{!v.firstname}" placeholder="" required="true" />
        <lightning:input aura:id="field" name="lastname" label="Lastname" value="{!v.lastname}" placeholder="" required="true" />
        <lightning:input aura:id="field" name="email" label="Business individual email only" value="{!v.email}" placeholder="please don’t use private or generic email" required="true" />
        <lightning:input aura:id="field" name="company" label="Company" value="{!v.company}" placeholder="" required="true" />
        <lightning:input aura:id="field" name="phone" label="Phone" value="{!v.phone}" placeholder="" required="true" />
        <lightning:select aura:id="field" name="Contact_s_preferred_language" label="Contact's Preferred Language" value="{!v.Contact_s_preferred_language}" required="true">
            <option value="">--None--</option>
            <option value="English">English</option>
            <option value="French">French</option>
            <option value="Spanish">Spanish</option>
            <option value="Italian">Italian</option>
            <option value="Polish">Polish</option>
            <option value="German">German</option>
            <option value="Portuguese">Portuguese</option>
            <option value="Chinese">Chinese</option>
            <option value="Turkish">Turkish</option>
        </lightning:select>
        <div class="areyouhuman"><CLAYH:AreYouHuman /></div>
        <lightning:button aura:id="submit" variant="brand" label="Request Access" onclick="{!c.submitForm}" disabled="{!v.verified==false}" />
     </div>
</aura:component>
  • here is my controller.apxc:

public without sharing class auraRegisterFormController {
    
    @auraenabled
    public static void addNewContactRequest(String firstname,String lastname,String email,String company,String phone,String Contact_s_preferred_language)
    {
        System.debug('#REGISTERFORM# '+firstname+' '+lastname+' '+email);
        
        // CHECK IF CONTACT ALREADY EXIST ON EMAIL THEN UPSERT RECORD
        
        List<Contact> cList = [ SELECT Id,Firstname,Lastname,Email,Company__c,Phone,Contact_status__c,Contact_s_preferred_language__c FROM Contact WHERE Email = :email ];        
        if(cList.size()>0)
        {
            for(Contact c:cList)
            {
                c.FirstName = firstname;
                c.LastName = lastname;
                c.Email = email;
                c.Company__c = company;
                c.Phone = phone;
                c.Contact_Status__c = 'Pending request';
                c.Contact_s_preferred_language__c = Contact_s_preferred_language;
            }
        }
        else
        {
            Contact c = new Contact();
            c.FirstName = firstname;
            c.LastName = lastname;
            c.Email = email;
            c.Company__c = company;
            c.Phone = phone;
            c.Contact_Status__c = 'Pending request';
            c.Contact_s_preferred_language__c = Contact_s_preferred_language;
            cList.add(c);
        }
        upsert cList;       
    }
}

User-added imageUser-added image
 
Best Answer chosen by Hermann Ouré
Bhawana Mehta SFDCBhawana Mehta SFDC
@Hermann : I think I  figured out why you are facing this issue. I faced it too in earlier day of LIghtning Component development :) :).

Yeah so the thing is, Parameter in Lightning components are case-sensitive meaning the paramater name in your Apex controller method and your js controller should match. When you do action.setParama() and the action.enqueue(), LIghtning will automatically map the parameter values passed from UI to the Aura Controller based on parameter names.

Now here, In Apex aura controller, you have used parameter name as "Contact_s_preferred_language" but when you are setting these values from UI via action.setParam , you have used "ContactPreferredLanguage" hence Lightning is not able to map value for this parameter.
 
action.setParams({
                               
                firstname: component.get("v.firstname"),
                lastname: component.get("v.lastname"),
                email: component.get("v.email"),
                company: component.get("v.company"),
                phone: component.get("v.phone"),
                ContactPreferredLanguage: component.get("v.Contact_s_preferred_language")
            });
 
public static void addNewContactRequest(String firstname,String lastname,String email,String company,String phone,
                                            String Contact_s_preferred_language)

Use the parameter name as "Contact_s_preferred_language" in action.setParam and you should be good to go.

All Answers

Bhawana Mehta SFDCBhawana Mehta SFDC
HI Hermann,

Can you post the code of submitForm method please?
Hermann OuréHermann Ouré
Hi @Bhawana Mehta,
Thanks for your reply

Here are all the codes:

registerForm.cmp:
<aura:component implements="forceCommunity:availableForAllPageTypes" controller="auraRegisterFormController" access="global" >
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    
    <aura:handler name="SendHumanResult" action="{!c.verifiedResult}" event="CLAYH:HumanCheck" />
    
    <aura:attribute name="firstname" type="String" default="" />
    <aura:attribute name="lastname" type="String" default="" />
    <aura:attribute name="company" type="String" default="" />
    <aura:attribute name="email" type="String" default="" />
    <aura:attribute name="phone" type="String" default="" />
    <aura:attribute name="Contact_s_preferred_language" type="String" default="" />
    
    <aura:attribute name="verified" type="Boolean" default="false" />
    
    <div class="registerFormComp">
        <div class="registerFormTitle">NEW USER REGISTRATION</div>
        <h2>Please fill the following form :</h2>
        <lightning:input aura:id="field" name="firstname" label="Firstname" value="{!v.firstname}" placeholder="" required="true" />
        <lightning:input aura:id="field" name="lastname" label="Lastname" value="{!v.lastname}" placeholder="" required="true" />
        <lightning:input aura:id="field" name="email" label="Business individual email only" value="{!v.email}" placeholder="please don’t use private or generic email" required="true" />
        <lightning:input aura:id="field" name="company" label="Company" value="{!v.company}" placeholder="" required="true" />
        <lightning:input aura:id="field" name="phone" label="Phone" value="{!v.phone}" placeholder="" required="true" />
        <lightning:select aura:id="field" name="Contact_s_preferred_language" label="Contact's Preferred Language" value="{!v.Contact_s_preferred_language}" required="true">
            <option value="">--None--</option>
            <option value="English">English</option>
            <option value="French">French</option>
            <option value="Spanish">Spanish</option>
            <option value="Italian">Italian</option>
            <option value="Polish">Polish</option>
            <option value="German">German</option>
            <option value="Portuguese">Portuguese</option>
            <option value="Chinese">Chinese</option>
            <option value="Turkish">Turkish</option>
        </lightning:select>
        <div class="areyouhuman"><CLAYH:AreYouHuman /></div>
        <lightning:button aura:id="submit" variant="brand" label="Request Access" onclick="{!c.submitForm}" disabled="{!v.verified==false}" />
     </div>
    
</aura:component>

registerFormController.js:
({
    doInit : function(component, event, helper) {  
    },
    
    
    verifiedResult : function(component, event, helper) {
    
        var ayhResult = event.getParam("result");
        component.set("v.verified", ayhResult);
    },
    
    submitForm : function(component, event, helper) {
        
        var allValid = component.find('field').reduce(function (validSoFar, inputCmp) {
            inputCmp.showHelpMessageIfInvalid();
            return validSoFar && inputCmp.get('v.validity').valid;
        }, true);
        
        var toastEvent = $A.get("e.force:showToast");
        
        if (allValid) {
             toastEvent.setParams({
                "title":"Thank You",
                "type": "success",
                "message": "Registration completed, we'll get back to you as soon as possible!",
                "duration" : 10000
            });            
           
            var action = component.get("c.addNewContactRequest");
            action.setParams({
                               
                firstname: component.get("v.firstname"),
                lastname: component.get("v.lastname"),
                email: component.get("v.email"),
                company: component.get("v.company"),
                phone: component.get("v.phone"),
                ContactPreferredLanguage: component.get("v.Contact_s_preferred_language")
            });
            action.setCallback(this, function(action) {
                $A.get("e.force:navigateToURL").setParams({"url":"/"}).fire();
            });
            
            $A.enqueueAction(action);
            
            
        } else {
                toastEvent.setParams({
                "title":"Error",
                "type": "error",
                "message": "Please fill in all required fields !",
                "duration" : 4000
            });
        }       
       
        toastEvent.fire();
    }
})

registerForm.apxc:
public without sharing class auraRegisterFormController {
    
    @auraenabled
    public static void addNewContactRequest(String firstname,String lastname,String email,String company,String phone,String Contact_s_preferred_language)
    {
        System.debug('#REGISTERFORM# '+firstname+' '+lastname+' '+email);
        
        // CHECK IF CONTACT ALREADY EXIST ON EMAIL THEN UPSERT RECORD
        
        List<Contact> cList = [ SELECT Id,Firstname,Lastname,Email,Company__c,Phone,Contact_status__c,Contact_s_preferred_language__c FROM Contact WHERE Email = :email ];        
        if(cList.size()>0)
        {
            for(Contact c:cList)
            {
                c.FirstName = firstname;
                c.LastName = lastname;
                c.Email = email;
                c.Company__c = company;
                c.Phone = phone;
                c.Contact_Status__c = 'Pending request';
                c.Contact_s_preferred_language__c = Contact_s_preferred_language;
            }
        }
        else
        {
            Contact c = new Contact();
            c.FirstName = firstname;
            c.LastName = lastname;
            c.Email = email;
            c.Company__c = company;
            c.Phone = phone;
            c.Contact_Status__c = 'Pending request';
            c.Contact_s_preferred_language__c = Contact_s_preferred_language;
            cList.add(c);
        }
        upsert cList;       
    }
}

 
Bhawana Mehta SFDCBhawana Mehta SFDC
@Hermann : I think I  figured out why you are facing this issue. I faced it too in earlier day of LIghtning Component development :) :).

Yeah so the thing is, Parameter in Lightning components are case-sensitive meaning the paramater name in your Apex controller method and your js controller should match. When you do action.setParama() and the action.enqueue(), LIghtning will automatically map the parameter values passed from UI to the Aura Controller based on parameter names.

Now here, In Apex aura controller, you have used parameter name as "Contact_s_preferred_language" but when you are setting these values from UI via action.setParam , you have used "ContactPreferredLanguage" hence Lightning is not able to map value for this parameter.
 
action.setParams({
                               
                firstname: component.get("v.firstname"),
                lastname: component.get("v.lastname"),
                email: component.get("v.email"),
                company: component.get("v.company"),
                phone: component.get("v.phone"),
                ContactPreferredLanguage: component.get("v.Contact_s_preferred_language")
            });
 
public static void addNewContactRequest(String firstname,String lastname,String email,String company,String phone,
                                            String Contact_s_preferred_language)

Use the parameter name as "Contact_s_preferred_language" in action.setParam and you should be good to go.
This was selected as the best answer
Hermann OuréHermann Ouré
Thank you Mate! Awesome
Hermann