I am trying to clone a record by using recordeditform in LWC. from LWC i am passing all the values to Apex.
while cloning in Apex i am getting error Illegal assignment from String to Date
How to resolve this.
Below is the JSON which is passed from LWC to apex -
{
"Id": "0061j000008IDB4AAO",
"AccountId": "0011j00000KigCXAAZ",
"CloseDate": "2019-12-31",
"TEst__c": "2019-11-08",
"Test1__c": "2019-11-20",
"Test2__c": "2019-10-31",
"Freetime_Terms__c": null,
"Test3__c": "2019-10-31",
"Test4__c": "2019-11-07",
"Opportunity_contains__c": null,
"Test5__c": "2019-11-09",
"Type": "Acquisition"
}
Apex -
public static String insertOpportunity(String opportunityId, String opportunityJson) {
system.debug('opportunityJson' + opportunityJson);
Map<String, Object> clonedOppValues = (Map<String, Object>)JSON.deserializeUntyped(opportunityJson);
String opportunityQuery = 'SELECT Id, ';
if(Schema.SObjectType.Opportunity.getRecordTypeInfosByName().size() > 1) {
opportunityQuery += 'RecordTypeId, ';
}
for( String fieldName : clonedOppValues.keySet() ) {
if(!opportunityQuery.containsIgnoreCase(fieldName)) {
opportunityQuery += fieldName + ', ';
}
}
opportunityQuery = opportunityQuery.removeEnd(', ');
opportunityQuery += ' FROM Opportunity WHERE Id = :opportunityId';
Opportunity originalOpp = Database.query(opportunityQuery);
Opportunity newOpp = originalOpp.clone(false, true);
for( String fieldName : clonedOppValues.keySet() ) {
system.debug('fieldName' + fieldName);
newOpp.put(fieldName, clonedOppValues.get(fieldName));
}
try {
insert newOpp;
}catch(Exception e){
}
return newOpp.Id;
}