Hello Friends today post, How to serialize data in JSON or JavaScript Component and Deserialize in Apex Class...
so friends let us start........
First you create a lightning component and making some field , this post only controller part and apex code so you understand better if you create saome field and fetch data in jscontroller..
JsController code...
Save:function(component,event,helper){ //This is function
var AcName=document.getElementById('accountdata').value; //get data
// alert(AcName);
var Status=document.getElementById('txtStatus').value;//get data
var Priority=document.getElementById('Priority').value;//get data
var Origin=document.getElementById('txtOrigin').value;//get data
var Type=document.getElementById('txtType').value;//get data
var Reason=document.getElementById('Reason').value;//get data
var subject=component.find("Subject").get("v.value");//get data
var objCase={ //Seriliaze Data in Variable
"AccName":AcName,
"cStatus":Status,
"cPriority":Priority,
"cOrigin":Origin,
"cType":Type,
"cReason":Reason,
"cSubject":subject
}
//alert('Serialize data'+JSON.stringify(objCase));
var action=component.get("c.SaveCase");
action.setParams({ //Set Parameter when used in Apex Class
"caseData":JSON.stringify(objCase) //Set Parameter when used in Apex Class
});
action.setCallback(this,function(a){
if(a.getState()=="SUCCESS"){
// alert(JSON.stringify(a.getReturnValue()));
//alert("Case Successfully Created!");
component.set("v.Second","slds-hide");
component.set("v.Third","slds-show");
}else{
alert("error");
}
});
$A.enqueueAction(action);
}
=> Apex side Code for Deserialize data for use.......
public class CaseCreationController {
public class caseDataContainer{
@AuraEnabled public String AccName{get;set;}
@AuraEnabled public String cStatus{get;set;}
@AuraEnabled public String cPriority{get;set;}
@AuraEnabled public String cOrigin{get;set;}
@AuraEnabled public String cType{get;set;}
@AuraEnabled public String cReason{get;set;}
@AuraEnabled public String cSubject{get;set;}
}
@AuraEnabled
public static String SaveCase(String caseData){
System.debug('Direct data from js=>'+caseData);
// List<caseDataContainer> serdata=new List<caseDataContainer>();
caseDataContainer data =(caseDataContainer)System.JSON.deserialize(caseData,caseDataContainer.class); //This code is Deserialize Data and use for Create Case
System.debug('Case data=>'+data);
String result='';
try{
Case obj=new Case();
obj.AccountId=data.AccName;
obj.Status=data.cStatus;
obj.Priority=data.cPriority;
obj.Origin=data.cOrigin;
obj.Type=data.cType;
obj.Reason=data.cReason;
obj.Subject=data.cSubject;
insert obj;
result=obj.Id;
}catch(Exception e){
System.debug('Error is' +e.getMessage()+ 'in Line '+e.getLineNumber());
}
return result;
}
Note: I hope you understand this post if you any problem face and contact with Email :-ashish28897@gmail.com.
Thanks!
so friends let us start........
First you create a lightning component and making some field , this post only controller part and apex code so you understand better if you create saome field and fetch data in jscontroller..
JsController code...
Save:function(component,event,helper){ //This is function
var AcName=document.getElementById('accountdata').value; //get data
// alert(AcName);
var Status=document.getElementById('txtStatus').value;//get data
var Priority=document.getElementById('Priority').value;//get data
var Origin=document.getElementById('txtOrigin').value;//get data
var Type=document.getElementById('txtType').value;//get data
var Reason=document.getElementById('Reason').value;//get data
var subject=component.find("Subject").get("v.value");//get data
var objCase={ //Seriliaze Data in Variable
"AccName":AcName,
"cStatus":Status,
"cPriority":Priority,
"cOrigin":Origin,
"cType":Type,
"cReason":Reason,
"cSubject":subject
}
//alert('Serialize data'+JSON.stringify(objCase));
var action=component.get("c.SaveCase");
action.setParams({ //Set Parameter when used in Apex Class
"caseData":JSON.stringify(objCase) //Set Parameter when used in Apex Class
});
action.setCallback(this,function(a){
if(a.getState()=="SUCCESS"){
// alert(JSON.stringify(a.getReturnValue()));
//alert("Case Successfully Created!");
component.set("v.Second","slds-hide");
component.set("v.Third","slds-show");
}else{
alert("error");
}
});
$A.enqueueAction(action);
}
=> Apex side Code for Deserialize data for use.......
public class CaseCreationController {
public class caseDataContainer{
@AuraEnabled public String AccName{get;set;}
@AuraEnabled public String cStatus{get;set;}
@AuraEnabled public String cPriority{get;set;}
@AuraEnabled public String cOrigin{get;set;}
@AuraEnabled public String cType{get;set;}
@AuraEnabled public String cReason{get;set;}
@AuraEnabled public String cSubject{get;set;}
}
@AuraEnabled
public static String SaveCase(String caseData){
System.debug('Direct data from js=>'+caseData);
// List<caseDataContainer> serdata=new List<caseDataContainer>();
caseDataContainer data =(caseDataContainer)System.JSON.deserialize(caseData,caseDataContainer.class); //This code is Deserialize Data and use for Create Case
System.debug('Case data=>'+data);
String result='';
try{
Case obj=new Case();
obj.AccountId=data.AccName;
obj.Status=data.cStatus;
obj.Priority=data.cPriority;
obj.Origin=data.cOrigin;
obj.Type=data.cType;
obj.Reason=data.cReason;
obj.Subject=data.cSubject;
insert obj;
result=obj.Id;
}catch(Exception e){
System.debug('Error is' +e.getMessage()+ 'in Line '+e.getLineNumber());
}
return result;
}
Note: I hope you understand this post if you any problem face and contact with Email :-ashish28897@gmail.com.
Thanks!
Buy me a coffee
Comments
Post a Comment