1

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;
    }
2
  • Did my answer solve your issue?
    – Saroj Bera
    Nov 7, 2019 at 5:16
  • Your ans helped me to proceed further, but i didn't wanted to hardcode any field name, so i did it dynamically using sObject and fieldType.. Nov 7, 2019 at 10:37

1 Answer 1

0

The values in the Map are of generic object type. You can't directly assign them to Opportunity fields. You have to explicitly cast them to that field type during assignment.

It should work, if you change the code in the similar way as below.

.......
.......
newOpp.put('fieldName', Date.valueOf((String.valueOf(clonedOppValues.get('fieldName')))));
.......
.......

Working Example Code:

Opportunity originalOpp = [SELECT Id FROM Opportunity WHERE Id = '0060o00001E3DyHAAV'];
String opportunityJson = '{"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"}';
Map<String, Object> clonedOppValues = (Map<String, Object>)JSON.deserializeUntyped(opportunityJson);
Opportunity newOpp = originalOpp.clone(false, true);
newOpp.put('Name', 'New Opportunity');
newOpp.put('StageName', 'Prospecting');
newOpp.put('CloseDate', System.today());
newOpp.put('Test1__c', Date.valueOf((String.valueOf(clonedOppValues.get('Test1__c')))));
insert newOpp;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .