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
JoeyDJoeyD 

Save Record and Related Records in list

Hi all,

 

I need to add the Orders__c.ID value to every Ordered_SKU__c.Order__c field on save.  I can't figure out how to get it in there... Any suggestions?

 

 

public class orderInsert {

Orders__c orders;
List<Ordered_SKU__c> skus {get; set;}

public Orders__c getOrders(){
if(orders == null){
orders = new Orders__c();
}
return orders;
}

public orderInsert(){
skus = new List<Ordered_SKU__c>();
skus.add(New Ordered_SKU__c()); // I've tried (New Ordered_SKU__c(Order__c = orders.id)); but obviously that returns a null value.
}

public void addrow(){
skus.add(new Ordered_SKU__c());
}

public PageReference save() {
try{
insert orders;
insert skus;
}
catch(DmlException ex){
ApexPages.addMessages(ex);
}
PageReference pr = new PageReference('/'+orders.id);
pr.setRedirect(true);
return pr;
}
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
NallapatiNallapati

Try modifying the save() function

 

public PageReference save() {
        try{
            insert orders;

            for(Ordered_SKU__c sku : skus)

                sku.order__C  = orders.id;
            insert skus;
        }
        catch(DmlException ex){
            ApexPages.addMessages(ex);  
        }
        PageReference pr = new PageReference('/'+orders.id);
        pr.setRedirect(true);
        return pr;
    }

 

All Answers

Alok_NagarroAlok_Nagarro

Hi,

 

I think this is controller class, you can use controller.getRecord() method to get data from page and then insert.

In your code, you r trying to insert null data.First u get data and then insert.

Apart from that you can use get- set property to overcome this problem. Now if you feel problem,then please post your completly code( VF page & controller).

 

Thanks,

JoeyDJoeyD

Here is my VF page as well.  The whole idea is to be able to enter a new order, with the product for that order in one go.   The product object being the child to the Order object.

 

 

<apex:page controller="orderInsert">
    <apex:form >
        <apex:pageBlock >
        
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" rerender="error"/>
            </apex:pageBlockButtons>
                <table>
                    <tr>
                        <td>Account: <apex:inputField value="{!orders.Account_del__c}"/></td>
                        <td>Invoice #:<br/><apex:inputField value="{!orders.Name}"/></td>
                        <td>Order Date:<br/><apex:inputField value="{!orders.Order_Date__c}"/></td>
                        <td>Ship Date:<br/><apex:inputField value="{!orders.Ship_Date__c}"/></td>
                    </tr>
                </table>
            <apex:pageBlockTable value="{!skus}" var="a" id="table">
                <apex:facet name="footer">
                    <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error"/>
                </apex:facet>
                <apex:column headerValue="SKU">
                    <apex:inputField value="{!a.SKU_del__c}"/>
                </apex:column>
                                       
            </apex:pageBlockTable>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>
		

 

 

 

 

NallapatiNallapati

Try modifying the save() function

 

public PageReference save() {
        try{
            insert orders;

            for(Ordered_SKU__c sku : skus)

                sku.order__C  = orders.id;
            insert skus;
        }
        catch(DmlException ex){
            ApexPages.addMessages(ex);  
        }
        PageReference pr = new PageReference('/'+orders.id);
        pr.setRedirect(true);
        return pr;
    }

 

This was selected as the best answer
Anil DAnil D

Hi, I m new to Salesforce

 

i want to do same, need to save Opportunity with custome lst Airlines

 

Her is my apex class

 

public class AirlineInsert {

    Opportunity opp;
    public List<Airline__c> listAir {get; set;}
    
    public Opportunity getOpp(){
        if(opp == null){
            opp = new Opportunity();
        }
        return opp;
    }
    public AirlineInsert(ApexPages.StandardController controller)
    {
        listAir = new List<Airline__c>();
        listAir.add(New Airline__c());
    }
    
    public void addrow(){
        listAir.add(new Airline__c());
    }
    
   public PageReference save() {
        try{
            update opp;
            for(Airline__c air : listAir)
             {
               air.Opportunity__C  = opp.id;
             }
            insert listAir;
        }
        catch(DmlException ex){
            ApexPages.addMessages(ex);  
        }
        PageReference pr = new PageReference('/'+ opp.id);
        pr.setRedirect(true);
        return pr;
    }
}

 

 

And this is Apex page

 

 

<apex:page standardController="Opportunity" extensions="AirlineInsert" >
   <apex:form >
       <apex:pageblock id="pb"  title="Opportunity Detail">
           <apex:pageBlockSection title="Order Details" columns="1">
                <apex:inputField value="{!Opportunity.OrderID__c}" label="Id:"/>
                <apex:outputField value="{!Opportunity.Status__c}" label="Status:"/>
                <apex:outputField value="{!Opportunity.Date_Sold__c}" label="Date Sold:"/>
                  <apex:inputfield value="{!Opportunity.Airline_Name__c}" label="Airline Name:"/>
           </apex:pageBlockSection>
           <apex:pageBlockSection title="Agents Info" columns="1">
                <apex:inputField value="{!Opportunity.Agent_Name__c}" label="Agent Name:"/>
                <apex:inputText value="{!Opportunity.Agent_Email__c}" label="Agent Email:"/>
                <apex:inputText value="{!Opportunity.Agent_Phone__c}" label="Agent Phone:"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Flight Info" columns="1">
                 <apex:inputField value="{!Opportunity.Region__c}" label="Region:"/>
                 <apex:pageBlockTable value="{!listAir}" var="a" id="table">
                    <apex:facet name="footer">
                        <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error"/>
                    </apex:facet>
                 <apex:column headerValue="Airline Name">
                    <apex:inputText value="{!a.Name}"/>
                 </apex:column>
                 <apex:column headerValue="PNR">
                    <apex:inputText value="{!a.PNR__c}"/>
                 </apex:column>
                 </apex:pageBlockTable>

          </apex:pageblock>
    </apex:form>
</apex:page>

 

But following error is coming

 

System.NullPointerException: Attempt to de-reference a null object
Class.AirlineInsert.save: line 26, column 1

 

Please help me on this