function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Janet Putnam 2Janet Putnam 2 

apex populate object with list of children

I'm trying to return an Order with an OrderLine as a JSON string.
I create two classes Order and OrderLines. Order contains a list of OrderLines.
public class Order
    {
        public string CustNmbr;
        public string ShipMthd;
        public string Refrence;
        public double FrtAmnt;
        public double TaxAmnt;
        public string Cntcprsn;
        public string ShipToName;
        public string Address1;
        public string Address3;
        public string City;
        public string State;
        public string ZipCode;
        public string PhNumbr1;
        public List<OrderLines> SOP10200;
    }
    public class OrderLines
    {
        public string itemnmbr;
        public decimal quantity;
        public string unitprce;
        public string uofm;
    }
Then I populate the fields from a query.
public static string getOrderFromContract(string contractID){
        sObject myOrder = [select id, name, contact_city__c, contact_state__c, contact_street_address__c, contact_zip_code__c, contact_email_address__c, contact_phone_number__c, contact_name_export__c, fns_number__c, number_of_device_sets_needed__c, processor_id__c, raps2_id__c, saturdaydelivery__c, store_name__c, terminal_id__c, terminal_type__c from cde_contract__c where id = :contractID];
        
        Order newOrder = new Order();
        OrderLines newLines = new OrderLines();
        
        string fns = (String)myOrder.get('fns_number__c');
        
        newOrder.custnmbr = ('f' + fns);
        newOrder.state = (String)myOrder.get('contact_state__c');
        newOrder.shipmthd = 'FedEX';
        newOrder.refrence = 'SalesForce Contract';
        newOrder.frtamnt = 0.00;
        newOrder.taxamnt = 0.00;
        newOrder.cntcprsn = (String)myOrder.get('contact_name_export__c');
        newOrder.shiptoname = (String)myOrder.get('store_name__c');
        newOrder.address1 = (String)myOrder.get('contact_street_address__c');
        newOrder.address3 = (String)myOrder.get('contact_email_address__c');
        newOrder.city = (String)myOrder.get('contact_city__c');
        newOrder.state = (String)myOrder.get('contact_state__c');
        newOrder.zipcode = (String)myOrder.get('contact_zip_code__c');
        newOrder.phnumbr1 = (String)myOrder.get('contact_phone_number__c');
        
        newLines.itemnmbr = '120604';
        newLines.quantity = (decimal)myOrder.get('number_of_device_sets_needed__c');
        newLines.uofm = 'roll';
                 
        return JSON.serialize(newOrder);
    }
This works to create the order header but I can't get the order lines part to work.
newOrder.SOP10200 = newLines;
I need something to assign the newLines item to the SOP10200 but I don't know how to get it done. The above results in the following error.
Illegal assignment from SalesOrderFromContract.OrderLines to List<SalesOrderFromContract.OrderLines>
Thanks for any help.




 
Best Answer chosen by Janet Putnam 2
Karan Shekhar KaulKaran Shekhar Kaul
Hi Janet,

newOrder.SOP10200 expects List but you are passing only single object's instance. 


public static string getOrderFromContract(string contractID){
        sObject myOrder = [select id, name, contact_city__c, contact_state__c, contact_street_address__c, contact_zip_code__c, contact_email_address__c, contact_phone_number__c, contact_name_export__c, fns_number__c, number_of_device_sets_needed__c, processor_id__c, raps2_id__c, saturdaydelivery__c, store_name__c, terminal_id__c, terminal_type__c from cde_contract__c where id = :contractID];
        
        Order newOrder = new Order();
        OrderLines newLines = new OrderLines();  
   // Added Code
       List<OrderLines > orderLineList = new List<OrderLines>();
        
        string fns = (String)myOrder.get('fns_number__c');
        
        newOrder.custnmbr = ('f' + fns);
        newOrder.state = (String)myOrder.get('contact_state__c');
        newOrder.shipmthd = 'FedEX';
        newOrder.refrence = 'SalesForce Contract';
        newOrder.frtamnt = 0.00;
        newOrder.taxamnt = 0.00;
        newOrder.cntcprsn = (String)myOrder.get('contact_name_export__c');
        newOrder.shiptoname = (String)myOrder.get('store_name__c');
        newOrder.address1 = (String)myOrder.get('contact_street_address__c');
        newOrder.address3 = (String)myOrder.get('contact_email_address__c');
        newOrder.city = (String)myOrder.get('contact_city__c');
        newOrder.state = (String)myOrder.get('contact_state__c');
        newOrder.zipcode = (String)myOrder.get('contact_zip_code__c');
        newOrder.phnumbr1 = (String)myOrder.get('contact_phone_number__c');
        
        newLines.itemnmbr = '120604';
        newLines.quantity = (decimal)myOrder.get('number_of_device_sets_needed__c');
        newLines.uofm = 'roll';
         // Added Code
        orderLineList .add(newLines);
        newOrder.SOP10200 = orderLineList ;
                 
        return JSON.serialize(newOrder);
    }

Hope this helps.

All Answers

Karan Shekhar KaulKaran Shekhar Kaul
Hi Janet,

newOrder.SOP10200 expects List but you are passing only single object's instance. 


public static string getOrderFromContract(string contractID){
        sObject myOrder = [select id, name, contact_city__c, contact_state__c, contact_street_address__c, contact_zip_code__c, contact_email_address__c, contact_phone_number__c, contact_name_export__c, fns_number__c, number_of_device_sets_needed__c, processor_id__c, raps2_id__c, saturdaydelivery__c, store_name__c, terminal_id__c, terminal_type__c from cde_contract__c where id = :contractID];
        
        Order newOrder = new Order();
        OrderLines newLines = new OrderLines();  
   // Added Code
       List<OrderLines > orderLineList = new List<OrderLines>();
        
        string fns = (String)myOrder.get('fns_number__c');
        
        newOrder.custnmbr = ('f' + fns);
        newOrder.state = (String)myOrder.get('contact_state__c');
        newOrder.shipmthd = 'FedEX';
        newOrder.refrence = 'SalesForce Contract';
        newOrder.frtamnt = 0.00;
        newOrder.taxamnt = 0.00;
        newOrder.cntcprsn = (String)myOrder.get('contact_name_export__c');
        newOrder.shiptoname = (String)myOrder.get('store_name__c');
        newOrder.address1 = (String)myOrder.get('contact_street_address__c');
        newOrder.address3 = (String)myOrder.get('contact_email_address__c');
        newOrder.city = (String)myOrder.get('contact_city__c');
        newOrder.state = (String)myOrder.get('contact_state__c');
        newOrder.zipcode = (String)myOrder.get('contact_zip_code__c');
        newOrder.phnumbr1 = (String)myOrder.get('contact_phone_number__c');
        
        newLines.itemnmbr = '120604';
        newLines.quantity = (decimal)myOrder.get('number_of_device_sets_needed__c');
        newLines.uofm = 'roll';
         // Added Code
        orderLineList .add(newLines);
        newOrder.SOP10200 = orderLineList ;
                 
        return JSON.serialize(newOrder);
    }

Hope this helps.
This was selected as the best answer
Janet Putnam 2Janet Putnam 2
It works. Thank you very much.