+ Start a Discussion
ChinnoyChinnoy 

Please help me on the below code.

Hi I am not able to save the record using lightning component.please find the code below.

Lightning Component:
<aura:component access="global" controller="LightningBloodDonorReg">
    
        <aura:attribute name="bloodReg" type="WT_BloodDonor__c" default="{ 'sobjectType' : 'WT_BloodDonor__c' }"/>
        
        <lightning:layout multipleRows="true">
            <div class="div1">
                <lightning:layoutItem flexibility="auto" padding="around-small" size="12">
                    JANASENA
                </lightning:layoutItem></div>
            <div class="div2">
                <lightning:layoutItem flexibility="auto" padding="around-small" size="12">
                    My Life; My janasena
                </lightning:layoutItem></div>
            
            <lightning:layoutItem flexibility="auto" padding="around-small" size="12">
                <lightning:layout >
                    <div style="padding-left:400px;">
                        <lightning:layoutItem size="4"></lightning:layoutItem>
                    </div>
                    <div style="background-color:orange;width:100%;">
                        <lightning:layoutItem flexibility="auto" padding="around-small" size="12" >
                            
                            <ui:inputText label="Donor Name" aura:id="Donor Name" class="field" value="{!v.bloodReg.Name}" />
                          Blood Group  <force:inputField  aura:id="Donor Name" class="field" value="{!v.bloodReg.BloodGroup__c}" />
                            <ui:inputText label="Donor Phone"  aura:id="Donor Name" class="field" value="{!v.bloodReg.PhoneNumber__c}" />
                           State <force:inputField  aura:id="Donor Name" class="field" value="{!v.bloodReg.State__c}" />
                           District <force:inputField  aura:id="Donor Name" class="field" value="{!v.bloodReg.District__c}" />
                            <!--   <ui:inputText label="Enter some text     " class="field" value="Test Field" updateOn="click"/>
                        <ui:inputText label="Enter some text     " class="field" value="Test Field" updateOn="click"/>
                        <ui:inputText label="Enter some text     " class="field" value="Test Field" updateOn="click"/>
                        <ui:inputText label="Enter some text     " class="field" value="Test Field" updateOn="click"/>-->
                            
                            <ui:button label="Submit" class="slds-button slds-button--neutral" labelClass="label" press="{!c.createDonor}"/>
                        </lightning:layoutItem>
                    </div>
                    
                    <lightning:layoutItem size="4"></lightning:layoutItem>
                </lightning:layout>
            </lightning:layoutItem>
        </lightning:layout>
  
</aura:component>


.JS file

({
  createDonor : function(component, event) {
    var newDonor = component.get("v.bloodReg");
    var action = component.get("c.createDonor");
    action.setParams({ 
        "bloodDonor": newDonor
    });
    action.setCallback(this, function(a) {
           var state = a.getState();
            if (state === "SUCCESS") {
                var name = a.getReturnValue();
                alert("success");
            }
        else{
              alert("Failed");
         }
        });
    $A.enqueueAction(action)
}
})

Class:

public class LightningBloodDonorReg {
    @AuraEnabled
    public static WT_BloodDonor__c bloodDonor { get; set; }
    
    public LightningBloodDonorReg() {
        
        bloodDonor = new WT_BloodDonor__c();
        
        
    }
    @AuraEnabled
    public static WT_BloodDonor__c saveDetails() {
        system.debug('bloodDonor'+bloodDonor);
       Upsert bloodDonor;
        system.debug('bloodDonor'+bloodDonor);
         return bloodDonor ;
    }
}
Best Answer chosen by Chinnoy
sachinarorasfsachinarorasf
Hi Chinnoy
JS Controller

createDonor : function (c,e,h){
          try{
              let newDonor  = c.get('v.bloodReg');
              let action = c.get("c.saveDetails");
              action.setParams({
                  "bloodDonor" : JSON.stringify(newDonor )
              });
              action.setCallback(this, function(r) {
                  let state = r.getState();
                  let storedResponse = r.getReturnValue();
                  if (state === "SUCCESS") {
                          alert('Done');
                  }
                  else {
                      console.log("Failed with state: " + state);
                  }
              });
              $A.enqueueAction(action);
          }catch(ex){
              console.log('**** ERROR **** '+ ex.message);
          }
      },


Apex Class



@AuraEnabled
	public static WT_BloodDonor__c  saveDetails(String bloodDonor) {
		WT_BloodDonor__c   getbloodDonorRecord ;
		getbloodDonorRecord = (WT_BloodDonor__c )JSON.deserialize(bloodDonor, WT_BloodDonor__c .class);
		Upsert getbloodDonorRecord;
		system.debug('bloodDonor'+getbloodDonorRecord);
		return getbloodDonorRecord ;
	}

P.S.: Mark this as Best Answer if this reply helped you.

Thanks
Sachin Arora