0

I need to create Multiple Child Records from Parent Related List but i get the Error: "URL No Longer Exists" but the data saved in the database. Also, it seem that the Parent Id does not populate autonmatically so, i have to add it manually. I want to notice that I add the child page as a related list of the parent. The Parent Obejct is Devery__c, the child is Item_Distributed__c Here is my code:

public class AddOrderItem {

    public String Delivery { get; set; }
Id DeliveryId;
public List<Item_Distributed__c> listOrderItem {get;set;}
public Integer rowNum{get;set;}

public AddOrderItem(ApexPages.StandardController controller){
   Id OrderItemId = ApexPages.currentPage().getParameters().get('OrderItemId');
    DeliveryId=ApexPages.currentPage().getParameters().get('DeliveryId');
    listOrderItem = new List<Item_Distributed__c>();
    listOrderItem.add(new Item_Distributed__c());

}

public pagereference insertOrderItem(){
    insert listOrderItem;
    Pagereference page=new pagereference('/'+DeliveryId);
    Return page;
}

public void insertRow(){
    listOrderItem.add(new Item_Distributed__c());
}

public void delRow(){
    rowNum = 
Integer.valueof(apexpages.currentpage().getparameters().get('index'));
    listOrderItem.remove(rowNum);
}
}

VF Page

<apex:page StandardController="Delivery__c"  Extensions="AddOrderItem" showHeader="false" sidebar="false" >
<apex:form >
    <apex:variable var="rowNum" value="{!0}" />
    <apex:pageBlock >
        <apex:variable var="rowNum" value="{!0}" />
        <apex:PageBlockTable value="{!listOrderItem}" var="int">
        <apex:facet name="footer">
            <apex:commandLink value="Add" action="{!insertRow}"/>
            </apex:facet>
            <apex:column headerValue="Quantity">
                <apex:inputField value="{!int.Quantity__c}"/>                                      
            </apex:column>
            <apex:column headerValue="Product">
            <apex:inputField value="{!int.Product__c}"/> 
            </apex:column>
         <apex:column headerValue="Product">
            <apex:inputField value="{!int.Delivery__c}"/> 
            </apex:column>  
             <apex:column headerValue="Delete">
            <apex:commandLink style="font-size:15px; font-weight:bold; text-align:center;color:red;" value="X" action="{!delRow}">
                <apex:param value="{!rowNum}" name="index"/>
                </apex:commandLink>
                <apex:variable var="rowNum" value="{!rowNum+1}"/>
            </apex:column>
        </apex:PageBlockTable>
    <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!insertOrderItem}"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>

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

enter image description here

1
  • Put a System.debug statement to check the value of deliveryId in debug logs. Make sure it isn't null. Commented Apr 18, 2019 at 11:50

2 Answers 2

0

From what I understand from your questions is, you want to allow users to add Item_Distributed__c records from inline visualforce page on standard layout of Delivery__c object.

If my above assumption is right, then you will have to make following changes in your code to make this to work.

You are getting the DeliveryId incorrectly which is the reason you are getting null from URL and hence it is giving you the error you are getting. You can get the DeliveryId field using any of the below ways, your constructor should look like this

public AddOrderItem(ApexPages.StandardController controller){
    //Not sure why you need OrderItemId
    //Id OrderItemId = ApexPages.currentPage().getParameters().get('OrderItemId');
    //You won't get the DeliveryId from URL with key as "DeliveryId" instead you should use "Id"
    //DeliveryId=ApexPages.currentPage().getParameters().get('DeliveryId');
    DeliveryId = ApexPages.currentPage().getParameters().get('Id');
    //or else you can get the delivery Id using standard controller's instance like below
    //DeliveryId=controller.getId();
    listOrderItem = new List<Item_Distributed__c>();
    //And the reason, Delivery ID is not populated in the child records is because, you are not setting the value of Delivery ID in your listOrderItem
    //listOrderItem.add(new Item_Distributed__c());
    listOrderItem.add(new Item_Distributed__c(Delivery__c= DeliveryId));
}

Similarly, when you are adding new rows to the related list, you will have to prepopulate the Delivery__c field for the new instances you are creating. Your insertRow method should be

public void insertRow(){
    listOrderItem.add(new Item_Distributed__c(Delivery__c= DeliveryId));
}
2
  • Hi @Vijay It works. Now, how can i remove the Standard Page of the parent which appears in the custom related page after i hit the save Button? Commented Apr 18, 2019 at 13:07
  • That's great ! For this, you can refer this link. If it doesn't work, I would suggest you to post another question, so that the wider community can provide their perspectives and you can pick the best solution which fits your needs. Commented Apr 18, 2019 at 13:12
0

You need to make sure that URL has a DeliveryId parameter. It seems that deliveryId parameter is not present in URL. If you need to redirect the page to parent delivery record upon saving child records then deliveryId parameter shouldn't be empty or null.

You must log in to answer this question.

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