1

I am working on an Invocable Apex Class to Clone Records based on a selected Objects. The base code I got from here is working well.
Now I am trying to add logic to increment a field after the record is cloned - i.e. Name 01, Name 02, etc. However i am receiving an error on Line 32 where I try to set the name

newObj.nameField.getName() = 'Test' + co;

On line 14 I was able to retrieve the API Name of the Object. I just need help with the syntax for using this in the clone method.

public class BulkCloneRecords {
    @InvocableMethod(label='Bulk Clone Records' description='Bulk Clone Records')
    public static List<FlowWrapper> createRecords(List<FlowWrapper> flowData) {
        List<FlowWrapper> fwList = new List<FlowWrapper>();
        Set<ID> recordIDSet = new Set<Id>();

        for(FlowWrapper fw : flowData)
            recordIDSet.add(fw.recordId);

        String sObjectName = flowData[0].recordId.getsobjecttype().getDescribe().getName();

        Map<String,Schema.SObjectField> objfields = Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap();

        for(String fieldApi :objfields.keySet() ) {
            Schema.DescribeFieldResult nameField = objfields.get(fieldApi).getDescribe();
            System.debug('API Name = ' + nameField.getName());
        }
        
        String query = 'SELECT ';
            query += String.join(new List<String>(objfields.KeySet()),',');
            query += ' FROM '+sObjectName+' WHERE ID IN : recordIDSet';
        
        
        List<sObject> sobjList = new List<sObject>();
        Integer currentRecord = 0;
        
        for(Sobject sob : Database.query(query)) {
            FlowWrapper fw = new FlowWrapper();
            for(Integer co = 1 ; co<= flowData[currentRecord].count; co++) {

                Sobject newObj = sob.clone(false, true, false, false);
                newObj.nameField.getName() = 'Test' + co;
                sobjList.add(newObj);
            }

            fw.sObjectList = sobjList;
            fw.isSuccess = true;
            fw.recordId = sob.Id;
            fw.count = flowData[currentRecord].count;
            fwList.add(fw);
            currentRecord++;
        }
        
        insert sobjList;
        return fwList;
    }
    
    public class FlowWrapper {
        @InvocableVariable(required=true)
        Public Id recordId;
        @InvocableVariable(required=true)
        Public Integer count;
    @InvocableVariable
        Public List<sObject> sObjectList;
    @InvocableVariable
        Public boolean isSuccess;        
    }
}
5
  • right now it looks like it generates Test1, Test2, etc. and you want to use the clone source's name field value and generate ABC 01, DEF 02, myCompany 03 ?
    – cropredy
    Commented Sep 12, 2022 at 22:43
  • Currently it is not working. I am trying to get it to generate Test 01, Test 02, etc. Commented Sep 13, 2022 at 1:43
  • Take a look at this post to see how to fetch the Name field. Then use the SObject class put method to set the value
    – cropredy
    Commented Sep 13, 2022 at 2:08
  • please add the error message, that way future google searches may point people here. and it makes it easier to answer Commented Sep 13, 2022 at 10:32
  • @JeffCallahan, I see your response to the Answer,really keen to have the Error Message Text that was generated by line 32 Commented Sep 14, 2022 at 3:11

1 Answer 1

1

Replace 'getName()' with 'put()'. SObject instances contain map methods for field data. Replace:

newObj.nameField.getName() = 'Test' + co;

With:

newObj.put('name', 'Test' + co);

this assumes objects unlike case object where the name field is not 'name'

or:

newObj.put(getNameMethod(), 'Test' + co);

a method to find the name field would be useful here

The key to the answer it to use a put method to assign the name to the name field.

2
  • 2
    Your answers are too short and need more content, in this instance your answer may be correct but it lacks the specifics that make it easily readable and link the problem with the solution. A little difficult to follow. spell it out for the OP and the future people so it is easier to just read your answer. Going to upvote because it works, and because you are putting some effort in across a whole bunch of questions. consider improving them all and get more points. Commented Sep 13, 2022 at 10:20
  • David - thanks! I replaced Line 32 with clonedRec.put(apiFieldName,clonedRec.get(apiFieldName) + ' ' + sequence); (I alos used a variable for the Field Name and Sequence) Commented Sep 13, 2022 at 12:36

You must log in to answer this question.

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