1

I am new in lightning and trying my hands on it,but get stuck where i simply want to insert a record after creating the page of app. here is my code :- Component`

<aura:component implements = "force:appHostable" controller = "SampleAppController">
    <aura:attribute name = "candidate" type = "Candidate__c"/>
      <!-- Page header -->
      <lightning:layout class="slds-page-header slds-page-header_object-home"> 
              <lightning:layoutItem>
                  <lightning:icon iconName = "standard:contact" />
                </lightning:layoutItem>
                <lightning:layoutItem>
                    <div class = "page-header page-section">
                        <h1 class="slds-text-heading--label">Candidate</h1>
                        <h1 class="slds-text-heading--medium">Cadidate detail</h1>
                    </div>
                </lightning:layoutItem>
          </lightning:layout>

          <div aria-labelledby="newCandidteform">
        <!-- BOXED AREA -->
        <fieldset class="slds-box slds-theme--default slds-container--small">
         <legend id="newCandidteform" class="slds-text-heading--small">
          Add Candidate
        </legend>

        <form class="slds-form--stacked">
            <lightning:input aura:id="candName" type="string" name= "Alwar"  label = "City" placeholder = "Enter City"/>
            <lightning:input type="checkbox" name="Currently_Employed__c" label = "Employed?" />
            <lightning:input type="phone" name="Phone" label="Phone"/>
            <lightning:input type="Number" name="Years_of_Experience__c" label="Year Of Experirnce" />
            <lightning:button variant="success" label="Success" title="Save" onclick ="{!c.handClick}" class="slds-m-top--medium"/>
            <!-- <lightning:inputField fieldName = "Education__c" /> -->
            <!-- <lightning:input type="Picklist" name="Education__c" label="Education" /> -->
        </form>
      </fieldset>
    </div>
</aura:component>

this is my client side controller--

({
    handClick : function(component, event) {
      var newCandidate  = component.get("v.candidate");
      var action = component.get("c.saveRecord");
      action.setParams({
          "cand" : newCandidate
      });
      action.setCallback(this,function(a){
          var state = a.getState();
          console.log('state is-->' + state)
          if(state == 'SUCCESS'){
              var name = a.getReturnValue();
              alert("hello from here"+name);

          }
      } );

    $A.enqueueAction(action);
    }
})

This is server side controller-

public with sharing class SampleAppController {
    public SampleAppController() {

    }
    @AuraEnabled
    public static Candidate__c saveRecord(Candidate__c cand){
     if(cand!= null){
          insert cand;
        }
        System.debug('candidate is-->>' + cand);
        return cand;

    }
}

Here i am not getting any error my code is fine but this is not inserting any value this is all giving null value, as i am understanding that there are my values are not getting set which i am passing through UI. Please give a light what is wrong here.

1
  • 2
    Is there a reason you cannot use force:recordData ? – Girbot Aug 4 '18 at 8:40
3
<aura:component controller = "SampleAppController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name = "candidate" type = "Candidate__c" default="{ 'sobjectType': 'Candidate__c',
                                                                      'First_Name__c': '',
                                                                      'Last_Name__c': '',
                                                                      'Email__c': '', 
                                                                      'Mobile__c': '',
                                                                      'Phone__c': '',
                                                                      'City__c': '',
                                                                      'Country__c': '',
                                                                      'Current_Employer__c': '', 
                                                                      'Currently_Employed__c    ': '',
                                                                      'Education__c': '' , 
                                                                      'Fax__c': '',
                                                                      'SSN__c': '',
                                                                      'State_Province__c': '', 
                                                                      'Street__c    ': '',
                                                                      'US_Citizen__c': '' ,
                                                                      'Visa_Required__c': '',
                                                                      'Zip_Postal_Code__c': '',
                                                                      'Years_of_Experience__c': ''                              
                                                                      }"  />
    <!-- Page header -->
    <lightning:layout class="slds-page-header slds-page-header_object-home"> 
        <lightning:layoutItem>
            <lightning:icon iconName = "standard:contact" />
        </lightning:layoutItem>
        <lightning:layoutItem>
            <div class = "page-header page-section">
                <h1 class="slds-text-heading--label">Candidate</h1>
                <h1 class="slds-text-heading--medium">Cadidate detail</h1>
            </div>
        </lightning:layoutItem>
    </lightning:layout>

    <div aria-labelledby="newCandidteform">
        <!-- BOXED AREA -->
        <fieldset class="slds-box slds-theme--default slds-container--small">
            <legend id="newCandidteform" class="slds-text-heading--small">
                Add Candidate
            </legend>
            <form class="slds-form--stacked">
                <lightning:input aura:id="candName" type="string" name= "City__c"  label = "City" placeholder = "Enter City" value="{!v.candidate.City__c}"/>
                <lightning:input type="checkbox" name="Currently_Employed__c" label = "Employed?" value="{!v.candidate.Currently_Employed__c}"/>
                <lightning:input type="phone" name="Phone" label="Phone" value="{!v.candidate.Phone__c}"/>
                <lightning:input type="Number" name="Years_of_Experience__c" label="Year Of Experirnce" value="{!v.candidate.Years_of_Experience__c}" />

                <lightning:button variant="success" label="Success" title="Save" onclick ="{!c.handClick}" class="slds-m-top--medium"/>
             </form>
        </fieldset>
    </div>
</aura:component>

Server side Controller:-

public with sharing class SampleAppController {

    @AuraEnabled
    public static void saveRecord(Candidate__c cand){
        if(cand!= null){
            insert cand;
        }
        System.debug('candidate is-->>' + cand);

    }
}

JavaScript controller:-

({
    handClick : function(component, event, helper) {

         var ObjectWith5Fields = component.get("v.candidate");
        var action = component.get("c.saveRecord");
        action.setParams({
            "cand" : ObjectWith5Fields
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if( state === "SUCCESS") {
                alert("hello from here");
            }
        } );
        $A.enqueueAction(action);
    }
})

This will solve your Problem. If it solve your problem select this as your answer. You left the default values while creating the attributes and in lightning field definition you did not set the values to the attribute. That's why you were not able to save.Also you should implement Force:hasRecordId.

2
  • Thank you it solve my problem.actually i tried with default but don't set all values that's y i always get null value. – SalesforceFolks Aug 4 '18 at 10:03
  • @salesforceFolks welcome🙂 – sanket kumar Aug 4 '18 at 10:09

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.