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
JayeeJayee 

can't catch a custom object parameter, containing employee data, helper to apex method

Hi all

Currently, sending a Employee enroll input form on a object H_HRBasic__c to the apex class method 

Emp_NM__c / Birth_YMD__c / Sex_Type__c /Email__c /Mobile_NO__c

CMP

<aura:attribute name="HRB_S" type="H_HRBasic__c" default="{'sobjectType':'H_HRBasic__c',
															   'Emp_NM__c':'',
															   'Birth_YMD__c':'',
															   'Sex_Type__c':'',
															   'Email__c':'',
															   'Mobile_NO__c':''}"/>
	<aura:attribute name="newHRB_S" type="H_HRBasic__c[]"/>
	<aura:attribute name="PsID" type="String" />

<lightning:input aura:id="hrbsform" name="Emp_NM__c" value="{!v.HRB_S.Emp_NM__c}" label="Name" required="true" type="String" style="margin-top:10px;" />

<lightning:input  aura:id="hrbsform" value="{!v.PsID}" label="SOCIALID" required="true" style="margin-top:10px;" />

<lightning:input aura:id="hrbsform" type="date" name="Birth_YMD__c" label="Birth date" value="{!v.HRB_S.Birth_YMD__c}" />

<lightning:select aura:id="hrbsform" label="M/F" name="Sex_Type__c" value="{!v.HRB_S.Sex_Type__c}">
 					      <option value="">--None--</option>
 					      <option value="Male">Male</option>
 					      <option value="Female">Female</option>
 </lightning:select>

<lightning:input aura:id="hrbsform" value="{!v.HRB_S.Email__c}" name="Email__c" label="Email" style="margin-top:10px;" />

<lightning:input  aura:id="hrbsform" value="{!v.HRB_S.Mobile_NO__c}" name="Mobile_NO__c" label="Phone" style="margin-top:10px;" />


<lightning:button  variant="neutral" label="save" onclick="{!c.getsaveIns}" />
 

Controller

getsaveIns : function(component, event, helper){

		var validhrbs = component.find("hrbsform").reduce(function (validSoFar,inputCmp){
			inputCmp.showHelpMessageIfInvalid();
			return validSoFar && inputCmp.get('v.validity').valid;
		}, true);
		//This is just for required field check


		if(validhrbs){
			var hrbs = component.get("v.HRB_S");
			var newhrbs = component.get("v.newHRB_S");
			var item = JSON.parse(JSON.stringify(hrbs));
			newhrbs.push(item);
			component.set("v.HRB_S",newhrbs);
			var psid = component.get("v.PsID");
			console.log('hrbs / /'+JSON.stringify(newhrbs));


			helper.setSaveIns(component,event,helper,newhrbs,psid);
		}


	},
 



Helper

setSaveIns : function(component,event,helper,newhrbs,psid){
        var action = component.get('c.saveIn_S');
        console.log('hrbs : : : : : '+newhrbs);
        console.log('psid : : : : : '+psid);
        action.setParams({

        	"HRB_S" : newhrbs,
        	"PsID"  : psid
        });

        action.setCallback(this, function(response){
            var result = response.getReturnValue();
            var state = response.getState();
            var toast = $A.get("e.force:showToast");

   			if(state == 'SUCCESS') {

   				console.log('SUCCES!!!!~'+JSON.stringify(result));

			} else {
   				console.log('fail'+JSON.stringify(result));

			}
			
        });
        $A.enqueueAction(action);
    },

Apex Class
@AuraEnabled
	public static void saveIn_S(H_HRBasic__c HRB_S, String PsID){
        // HRB_S =  new H_HRBasic__c;
        system.debug('psid : : : :'+psid);
        system.debug('HRB_S : : : :'+HRB_S);
        
        H_AssignBasic__c AssB_S = new H_AssignBasic__c();
        H_RetireBasic__c RB_S = new H_RetireBasic__c();
        H_AttendBasic__c AttB_S = new H_AttendBasic__c();
        H_HolsBasic__c HB_S = new H_HolsBasic__c();
        H_PayBasic__c PB_S = new H_PayBasic__c();
        H_4InsBasic__c Ins4B_S = new H_4InsBasic__c();
        H_YearTaxBasic__c YTB_S = new H_YearTaxBasic__c();
..

but it goes well after i instantiate a custom object H_HRBasic__c constructor on the apex method 
like 
H_HRBasic__c HRB_S = new H_HRBasic__c ();
        system.debug('psid : : : :'+psid);
        system.debug('HRB_S : : : :'+HRB_S);


ERROR ON DEVELOPER CONSOLE

18:26:33:002 USER_DEBUG [135]|DEBUG|HRB_S : : : :null

18:26:33:031 FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

I guess it is JSON stringify problem when i sending a javascript object to apex class

but I am not sure which way is the best way to solve it

Thanks for reading this.

Raj VakatiRaj Vakati
Change yyour code as below
 
getsaveIns : function(component, event, helper){

		var validhrbs = component.find("hrbsform").reduce(function (validSoFar,inputCmp){
			inputCmp.showHelpMessageIfInvalid();
			return validSoFar && inputCmp.get('v.validity').valid;
		}, true);
		//This is just for required field check


		if(validhrbs){
			var hrbs = component.get("v.HRB_S");
			var newhrbs = component.get("v.newHRB_S");
var item =[] ;
			item .push(hrbs);
 item .push(newhrbs ); 		
	component.set("v.HRB_S",item );
			var psid = component.get("v.PsID");
			console.log('hrbs / /'+JSON.stringify(newhrbs));


			helper.setSaveIns(component,event,helper,newhrbs,psid);
		}


	},