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
imrohitimrohit 

i have created a fieldset in contact object and retrived some fields like firstname,lastname.email,phone etc and display all the fields as a form in lightning component like this

<aura:iteration items="{! v.fields }" var="field">
           
            <div class="slds-size_5-of-12">
                <lightning:input aura:id="idName"   label="{!field.Label}"  required="{!field.DBRequired}"  type="{!field.Type}" class="slds-p-top_small slds-m-top_medium" />
            </div>
             <div class="slds-size_1-of-12"></div>
            </aura:iteration>
and i want  ,whatever i type in input box of firstname,lastname or whatever  the value of that field should be store in attribute of type Contact otherwise i want all the data as Json in lightning component  Js controller  Please help me to solve this...
SKSANJAYSKSANJAY
Hi Likesh,

FYI
<aura:iteration items="{! v.fields }" var="field" indexVar="listIndex">
	<div class="slds-size_5-of-12">
		<lightning:input aura:id="{!'fieldId' + listIndex}"   label="{!field.Label}"  required="{!field.DBRequired}"  type="{!field.Type}" class="{!'slds-p-top_small slds-m-top_medium fieldClass'}" />
	</div>
	<div class="slds-size_1-of-12"></div>
</aura:iteration>
<a href="javascript:void(0);" onclick="{!c.getJSONData}">Get JSON Value</a>

Controller Code : If you are using jquery
getJSONData : function(component, event, helper){
	try{
		//JSON object declaration	
		var fieldList = [];
		//If you are using Jquery
		$(".fieldClass").each(function(){
			var fieldValue = $(this).val();
			console.log("Field Value : " + fieldValue);
			
			var currentFieldObject = {"fieldValue" : fieldValue};			
			fieldList.push(currentFieldObject);
		});
		
		var fieldObjectJSON = {"fieldList" : fieldList};
		
		console.log("My JSON DATA : " + JSON.stringify(fieldObjectJSON));
		//End if you are using Jquery
          }catch(err){
                 console.log("Exception in getJSONData method : " + err.stack);
          }
}
Controller code with simple javascript
 
getJSONData : function(component, event, helper){
	try{
		//If no jquery
		var fieldsList = component.get("v.fields");
		if(fieldsList != undefined && fieldsList != null){
			var totalFieldCount = fieldsList.length;
			
			var fieldList = [];
			for(var i=0; i<totalFieldCount.length; i++){
				var fieldValue = document.getElementById("fieldId" + i).value;
				console.log("Normal javascript field value : " + fieldValue);
				var currentFieldObject = {"fieldValue" : fieldValue};			
				fieldList.push(currentFieldObject);
			}
			var fieldObjectJSON = {"fieldList" : fieldList};
		
			console.log("My JSON DATA : " + JSON.stringify(fieldObjectJSON));
		}
		/End of if no jquery
	}catch(err){
		console.log("Exception in getJSONData method : " + err.stack);
	}
}

Hope this will help you. Check out te consoles printed.

Thanks,
Sanjay​
 
imrohitimrohit
HI Sanjay
Thanks for reply 
Instead of Json like this as your code is printing in console 
My JSON DATA : {"fieldList":[{"fieldValue":"dfsg"},{"fieldValue":"fdsg"},{"fieldValue":null},{"fieldValue":null},{"fieldValue":null},{"fieldValue":null},{"fieldValue":null},{"fieldValue":null},{"fieldValue":null},{"fieldValue":null}]}
 I want to print JSOn like this
My JSON DATA : {"fieldList":[{"FirstName":"dfsg"},{"LastName":"fdsg"},{"Email":null},{"AssistantPhone":null},{"MailingStreet":null},{"MailingCity":null},{"MailingPostalCode":null},{"MailingState":null},{"MailingCountry":null},{"MailingPostalCode":null}]}

All my APi name are in the name attribute of lightning input you can see in bold text as below


           <aura:iteration items="{!v.fields }" var="field" indexVar="listIndex">
           
            <div class="slds-size_5-of-12">
                <lightning:input aura:id="idName" name="{!field.APIName}"  label="{!field.Label}"  required="{!field.DBRequired}"  type="{!field.Type}" class="slds-p-top_small slds-m-top_medium" />
            </div>
             <div class="slds-size_1-of-12"></div>
            </aura:iteration>



 
SKSANJAYSKSANJAY
You can use your field attribute. FYI
 
getJSONData : function(component, event, helper){
	try{
		//If no jquery
		var fieldsList = component.get("v.fields");
		if(fieldsList != undefined && fieldsList != null){
			var totalFieldCount = fieldsList.length;
			
			var fieldList = [];
			for(var i=0; i<totalFieldCount.length; i++){
				var fieldValue = document.getElementById("fieldId" + i).value;
				console.log("Normal javascript field value : " + fieldValue);
				var currentFieldObject = {fieldsList[i].APIName : fieldValue};	//Use your specific api name		
				fieldList.push(currentFieldObject);
			}
			var fieldObjectJSON = {"fieldList" : fieldList};
		
			console.log("My JSON DATA : " + JSON.stringify(fieldObjectJSON));
		}
		/End of if no jquery
	}catch(err){
		console.log("Exception in getJSONData method : " + err.stack);
	}
}