0

I am trying to create a new order with line items based on data sent from a Lightning Web Component to my Apex class.

I have managed to send the data by using the following function:

newOrder(){
        let Items = JSON.stringify(this.cartItems);
        createCheckoutOrder({lstProds: Items});
        window.console.log('Cart Items '+ Items);

    }

This is the apex class that I am using:

public static void createCheckoutOrder(List<Map<string, object>> lstProds){

        system.debug(lstProds); 

        list<OrderItem> orderItems = new list<OrderItem>();

        for(Object pl: lstProds){
            orderItems.add(new OrderItem(UnitPrice         = pl.UnitPrice,
                                          OrderId           = ord.id,
                                          Quantity          = pl.quantity,
                                          Product2Id        = pl.Id,
                                          PricebookEntryId  = pl.PricebookEntryId

            ));
        }
        system.debug(orderItems);
        insert orderItems;
}

This is the form of the data in the debug log:

({element={Id=01t0X000008AclSQAS, PricebookEntryId=01u0X000011uFBwQAM, UnitPrice=25000, quantity=1}}, {element={Id=01t0X000008AclVQAS, PricebookEntryId=01u0X000011uFBzQAM, UnitPrice=20000, quantity=1}}, {element={Id=01t0X000008AclbQAC, PricebookEntryId=01u0X000011uFC5QAM, UnitPrice=10000, quantity=1}})

I have tried to create a new OrderItem List and add items to the list based on the data received form the LWC. How can I correctly assign values to the order item fields with the correct values from the LWC?

  • 1
    What's the error raised ? What is returned by your system.debug call? – Martin Lezer Feb 3 at 15:03
  • The error is that variable UnitPrice, quantity, Id, PricebookEntryId does not exist. Which makes sense. just not sure how to access the values that i get in lstProds. – Jemat Feb 3 at 15:09
0

First thing, What is the issues in your code?

You are trying to access these fields on Map, which does not exist. You need to convert the pl into the map, then you can access that.

public static void createCheckoutOrder(List<Map<string, object>> lstProds){

    system.debug(lstProds); 

    list<OrderItem> orderItems = new list<OrderItem>();

    for(Object pl: lstProds){

        Map<String, Object> oliFIeldsMap = (Map<String, Object>) pl;
        system.debug('UnitPrice ' + oliFIeldsMap.get('UnitPrice'));
    }
    system.debug(orderItems);
    insert orderItems;
}

Secondly, the recommended way

The simple way I would prefer to change to signature of the Aura Enabled Apex method so that it accepts the list of OrderItems directly.

public static void createCheckoutOrder(List<OrderItem> lstProds){
    for(OrderItem oli: lstProds){
        //... do some stuff if needed
    }
    insert lstProds;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.