0

I have a wrapper class which updates selected rows from the standard object orderItem, with details from a custom object called Delivery_Note__c.

A change in requirements has meant that I have had to create a junction object between the two objects called Delivery_Note_Line_Item.

I would like to update the wrapper to create a new Delivery_Note_Line_Item against every selected orderItem, populating both the orderItem, and the Delivery_Note__c lookups, as well as a quantity field.

I am not sure how to go about inserting the new record, using the current wrapper class.

Class:

public with sharing class DeliveryNoteSelectExtension {

    public String redirectUrl {get;set;} 

    public List<wrapOrderItem> wrapOrderItemList {get; set;}
    public List<OrderItem> selectedOrderItems {get;set;}

    public Delivery_Note__c currentRecord {get;set;}

    public DeliveryNoteSelectExtension(ApexPages.StandardController controller) {

        currentRecord = [SELECT id,name,order__c,Order__r.id FROM Delivery_Note__c WHERE id = :ApexPages.CurrentPage().getparameters().get('ID')];
        redirectUrl = '/'+currentRecord.Id;

        if(wrapOrderItemList == null) {
            wrapOrderItemList = new List<wrapOrderItem>();
            for(OrderItem o: [
                SELECT id,
                    Delivery_Note__c,Delivered__c,Delivery_Note__r.name,Delivery_Note__r.id,ListPrice,Product2.name,Product2.ProductCode,Quantity,
                    Quantity_Picked__c,Quantity_Back_Order__c,TotalPrice,Total_GST__c,Total_Including_GST__c
                FROM orderItem 
                WHERE orderid = :currentRecord.order__r.id 
                AND Quantity_Back_Order__c > 0

            ]){
                wrapOrderItemList.add(new wrapOrderItem(o));
            }
        }
    }

    public void processSelected() {
        selectedOrderItems = new List<orderItem>();

        for(wrapOrderItem wrapOrderItemObj : wrapOrderItemList) {
            if(wrapOrderItemObj.selected == true) {
                selectedOrderItems.add(wrapOrderItemObj.oi);
                wrapOrderItemObj.oi.Delivery_Note__c = ApexPages.currentPage().getParameters().get('id');
                wrapOrderItemObj.oi.delivered__c = TRUE;
            }

        }
        update selectedOrderItems;
    }

    public class wrapOrderItem {
        public orderItem oi {get; set;}
        public Boolean selected {get; set;}

        public wrapOrderItem(orderItem o) {
            oi = o;
            selected = false;
        }
    }
2
  • what is the issue you are facing? Apr 19, 2018 at 4:10
  • Hey Santanu, I am stuck with where/how to insert the new record, using the wrapper (selected items)... Apr 19, 2018 at 4:13

1 Answer 1

1

it' seems easy to me,

public void processSelected() {
selectedOrderItems = new List<orderItem>();
List<Delivery_Note_Line_Item__c> delNoteLineItemsTobeInserted = new List<Delivery_Note_Line_Item__c>();
for(wrapOrderItem wrapOrderItemObj : wrapOrderItemList) {
    if(wrapOrderItemObj.selected == true) {
        selectedOrderItems.add(wrapOrderItemObj.oi);
        wrapOrderItemObj.oi.Delivery_Note__c = ApexPages.currentPage().getParameters().get('id');
        wrapOrderItemObj.oi.delivered__c = TRUE;

        Delivery_Note_Line_Item__c newDelItem = new Delivery_Note_Line_Item__c();
        newDelItem.OrderLineItemLookup__c = wrapOrderItemObj.oi.Id;
        newDelItem.Delivery_Note__c = ApexPages.currentPage().getParameters().get('id');
        delNoteLineItemsTobeInserted.add(newDelItem);
    }

}
if(delNoteLineItemsTobeInserted.size() > 0){
    insert delNoteLineItemsTobeInserted;
}
update selectedOrderItems;
}

Hope This helps !!

0

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.