Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<aura:component controller="RecordFormCompController" implements="flexipage:availableForRecordHome,force:hasRecordId,force:apphostable" access="global">
<aura:attribute name="sObjectName" type="String" default="Account" />
<aura:attribute name="iconName" type="String" default="account" />
<aura:attribute name="recordId" type="Id"/>
<lightning:card iconName="{!'standard:'+v.iconName}" title="{! 'New '+v.sObjectName}">
<div class="slds-p-left_large slds-p-right_medium">
<lightning:recordForm
objectApiName="{!v.sObjectName}"
recordId="{!v.recordId}"
layoutType="Full"
columns="2"
mode="edit"
onsubmit="{!c.handleSubmit}"
/>
</div>
</lightning:card>
</aura:component>
public class RecordFormCompController {
@AuraEnabled
public static string createRecords(String recordJSON, String ObjectName) {
sObject sObj = Schema.getGlobalDescribe().get(ObjectName).newSObject() ;
schema.SObjectType sobjType = Schema.getGlobalDescribe().get(ObjectName);
Map<String, Schema.sObjectField> sObjectFields = sobjType.getDescribe().fields.getMap();
Map<String, Object> objMap = (Map<String, Object>) JSON.deserializeUntyped(recordJSON);
for(String fieldName : objMap.keySet()) {
Object value = objMap.get(fieldName);
Schema.DisplayType valueType = sObjectFields.get(fieldName).getDescribe().getType();
if (value instanceof String && valueType != Schema.DisplayType.String)
{
String svalue = (String)value;
if (valueType == Schema.DisplayType.Date)
sObj.put(fieldName, Date.valueOf(svalue));
else if(valueType == Schema.DisplayType.DateTime) {
//DateTime is a special case which we need to handle carefully. It is working in my case you need to handle it.
try{
String d1 = svalue;
list<String> d2 = d1.split('-');
list<integer> timeComponent = new list<integer>();
timeComponent.add(Integer.valueOf(d2[0]));
timeComponent.add(Integer.valueOf(d2[1]));
timeComponent.add(Integer.valueOf(d2[2].left(2)));
String t = d2[2].substringBetween('T','.');
list<String> time1 = t.split(':');
timeComponent.add(Integer.valueOf(time1[0]));
timeComponent.add(Integer.valueOf(time1[1]));
timeComponent.add(Integer.valueOf(time1[2]));
Datetime dt = Datetime.newInstance(timeComponent[0],timeComponent[1],timeComponent[2],timeComponent[3],timeComponent[4],timeComponent[5]);
sObj.put(fieldName, dt);
}
catch(exception ex){}
}
else if (valueType == Schema.DisplayType.Percent || valueType == Schema.DisplayType.Currency)
sObj.put(fieldName, svalue == '' ? null : Decimal.valueOf(svalue));
else if (valueType == Schema.DisplayType.Double)
sObj.put(fieldName, svalue == '' ? null : Double.valueOf(svalue));
else if (valueType == Schema.DisplayType.Integer)
sObj.put(fieldName, Integer.valueOf(svalue));
else if (valueType == Schema.DisplayType.Base64)
sObj.put(fieldName, Blob.valueOf(svalue));
else
sObj.put(fieldName, svalue);
}
else
sObj.put(fieldName, value);
}
upsert sObj;
return sObj.Id;
}
}
({
handleSubmit : function(component, event, helper) {
event.preventDefault();
var recordData = event.getParam("fields");
var record = event.getParam("record");
var action = component.get("c.createRecords");
action.setParams({
"recordJSON":JSON.stringify(recordData),
"ObjectName": component.get("v.sObjectName")
});
action.setCallback(this,function(response){
// you should be using getState on result as getStatus doesn't exist in standard API
var status = response.getState();
if(status === "SUCCESS"){
var recId = response.getReturnValue();
var navEvt = $A.get("e.force:navigateToSObject");
navEvt.setParams({
"recordId": recId
});
navEvt.fire();
}
});
$A.enqueueAction(action);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment