0

i'm facing the following error while trying to save this. can someone tell me what im doing wrong please?

public class GanttDTO {

public class RecordDetail {
    @AuraEnabled public String identifier;
    @AuraEnabled public String parent;
    @AuraEnabled public String name;
    @AuraEnabled public String owner;
    @AuraEnabled public String start;
    @AuraEnabled public String due;
    @AuraEnabled public String completed;
    @AuraEnabled public String status;
    @AuraEnabled public String sortorder;
    @AuraEnabled public Record[] records;
}
public class Record {
    RecordDetail record;
}}

and this is the class code

public class GanttData {

@AuraEnabled 
public static void getData(String recordid, String objecttype){
    
    //get onboarding
    list<onboarding__c> onboardings_1 = new list<onboarding__c>();
    if(objecttype == 'Account'){
        onboardings_1 = [select id,name, 
                                       (select name, start_date__c, due_date__c, completed_date__c, sort_order__c, status__c from milestones__r order by sort_order__c asc), 
                                       (select name, start_date__c, due_date__c, completed_date__c, sort_order__c, status__c from task_trackers__r order by sort_order__c asc) 
                                       from onboarding__c 
                                       where account__c = :recordid];
        
    }else if(objecttype == 'Onboarding'){
        onboardings_1 = [select id,name, 
                                       (select name, start_date__c, due_date__c, completed_date__c, sort_order__c, status__c from milestones__r order by sort_order__c asc), 
                                       (select name, start_date__c, due_date__c, completed_date__c, sort_order__c, status__c from task_trackers__r order by sort_order__c asc) 
                                       from onboarding__c 
                                       where id = :recordid];
    }
    //loop through level 1
    List<GanttDTO.RecordDetail> onbaordings = new List<GanttDTO.RecordDetail>();
    //List<GanttDTO.Record> onboarding_dependants = new List<GanttDTO.Record>();
    List<GanttDTO.RecordDetail> onboarding_dependants = new List<GanttDTO.RecordDetail>();
    
    for(Integer i=0 ; i < onboardings_1.size() ; i++){
        onboarding__c ob = onboardings_1[i];
        if(ob.milestones__r != null){
            List<GanttDTO.RecordDetail> milestones = new List<GanttDTO.RecordDetail>();
            for(milestone__c m: ob.milestones__r){
                GanttDTO.RecordDetail milestone = new GanttDTO.RecordDetail();
                milestone.name = m.Name;
                milestones.add(milestone);
            }
            System.debug('milestones:' +milestones.size()+ ':' +milestones);
            onboarding_dependants.addAll(milestones);
        }
        if(ob.task_trackers__r != null){
            List<GanttDTO.RecordDetail> tasktrackers = new List<GanttDTO.RecordDetail>();
            for(task_tracker__c t : ob.task_trackers__r){
                GanttDTO.RecordDetail tasktracker = new GanttDTO.RecordDetail();
                tasktracker.name = t.Name;
                tasktrackers.add(tasktracker);
            }
            System.debug('tasktrackers:' +tasktrackers.size() + ':' +tasktrackers);
            onboarding_dependants.addAll(tasktrackers);
        }                
        
        GanttDTO.RecordDetail onboarding = new GanttDTO.RecordDetail();
        
        onboarding.name = ob.Name;
        onboarding.records = new List<GanttDTO.RecordDetail>(onboarding_dependants); 
        onbaordings.add(onboarding);
    }
    System.debug('onbaordings:' +onbaordings);        
}}

onboarding.records = new List<GanttDTO.RecordDetail>(onboarding_dependants);

this line throws an error : Illegal assignment from List<GanttDTO.RecordDetail> to List<GanttDTO.Record>

1 Answer 1

0

The record object itself should be initialized:

public class Record {
    public RecordDetail record;
    public Record(RecordDetail data) {
      record = data;
    }
}

And then the various records need to be constructed that way:

onboarding.records = new List<GanttDTO.Record>(); 
for(GanttDTO.RecordDetail detail: onboard_dependants) {
  onboarding.records.add(new GanttDTO.Record(detail));
}

This happens because of the inner level structure of the wrapper class.

6
  • Hi @sfdcfox I made the changes you recommended but im getting this error Method is not visible: void GanttDTO.Record.<init>(GanttDTO.RecordDetail)
    – roy
    Commented Jan 16, 2021 at 23:15
  • @roy Sorry, it should have been public. Give that a try.
    – sfdcfox
    Commented Jan 16, 2021 at 23:49
  • worked like a charm. I have much to learn! thank you so much!
    – roy
    Commented Jan 16, 2021 at 23:56
  • @roy I would ask just why the RecordDetail attribute "records" is of type list of Record instead of list of RecordDetail? As it stands here, your Record class adds no value and simply means you have to waste time constructing a Record instance from a RecordDetails one. Obviously if Record contains more info not derived from the RecordDetail then there is value, but I am not sure how you intend that to be the case given how the Record instance is constructed.
    – Phil W
    Commented Jan 17, 2021 at 9:00
  • @PhilW Based on their previous questions, they are creating a JSON data structure that wants this intermediate object, something like { "record": { "id": 123, ..., "records": [ { "record": { ... } ] } }. This is loosely based on how it would have been in XML, and some people like the needlessly wasteful design.
    – sfdcfox
    Commented Jan 17, 2021 at 11:23

You must log in to answer this question.

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