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
OdedHarnivOdedHarniv 

Creating new object from Related List

Hi

 

I'm facing a problem which I haven't found how to overcome:

 

I have two custom objects, these objects have a lookup relationship and therefore can be created from the corresponding related list on the lookup object object.


My problem is that for custom object AAA, when I create new (through the Related List) the save action takes me back to the Related List (on the Object page) but for the custom object BBB, after save I get the page showing me the object I just created (Details Page).

 

I want to achieve the first behavior, and I was told that because object BBB has a Recod Type (which I select first thing when I create the object) I get the second behavior.

 

Its really importent for me to achive the first behavior for both.

Any idea how to work around this?

 

Thanks

sebcossebcos

Hi,

the navigation behavior can be overriden using visualforce pages and button overrides.

To change the new functionality go to the object's config page and change the New standard button to the following visualforce page (assuming your custom object name will be testnewfromrellist__c and has lookups to Account and Job Application custom object - please change as needed):

 

<apex:page standardController="testnewfromrellist__c" extensions="ExtTestNewFromRelatedlist"> 
    cancelURL {!cancelURL}
    <apex:sectionHeader title="testnewfromrellist__c Edit" subtitle="{!testnewfromrellist__c.name}"/> 
        <apex:form > 
            <apex:pageBlock title="testnewfromrellist__c Edit" mode="edit"> 
                <apex:pageBlockButtons location="top"> 
                    <apex:commandButton value="Save" action="{!save}"/> 
                    <apex:commandButton value="Cancel" action="{!cancel}"/> 
                </apex:pageBlockButtons> 
                <apex:pageBlockSection title="Information" columns="2"> 
                    <apex:inputField value="{!testnewfromrellist__c.Name}" required="false"/> 
                    <apex:inputField value="{!testnewfromrellist__c.Account__c}" required="false"/> 
                    <apex:inputField value="{!testnewfromrellist__c.Job_Application__c}" required="false"/> 
                </apex:pageBlockSection> 
            </apex:pageBlock> 
        </apex:form> 
</apex:page>

 

The extension code looks like this:

 

 

public with sharing class ExtTestNewFromRelatedlist {

    private ApexPages.StandardController ctrl;
    public Pagereference cancelURL {get;set;}
    private String keyPrefix {get;set;}
    
    public ExtTestNewFromRelatedlist(ApexPages.StandardController controller) {
    
        this.ctrl = controller;
        cancelURL = ctrl.cancel();
        Schema.DescribeSObjectResult R = controller.getRecord().getsObjectType().getDescribe();
        keyPrefix = R.getKeyPrefix();
    }
    
    public PageReference save(){
        
        PageReference pr = ctrl.save();
        if (cancelURL.getURL().contains('/' + keyPrefix)) { // return to same record if not coming from parent
            return pr;
        } else { // return to Parent using cancel pagereference url 
            return cancelURL;  
        }
        
    }
    
    static testMethod void testSaveReturnToParent(){
    
        testnewfromrellist__c s = new testnewfromrellist__c();
        ExtTestNewFromRelatedlist ext = new ExtTestNewFromRelatedlist(new ApexPages.StandardController(s));
        system.debug('cancelURL generic home page: ' + ext.cancelURL);
        PageReference returnPR = ext.save();
        // return to parent in this case the generic home page since not coming from anywhere specific - good enough test but home page could change
        system.assertEquals('/home/home.jsp',returnPR.getURL());
    }
    
     static testMethod void testSaveReturnToSameRecord(){
    
        PageReference p = Page.newtestrellist;
        testnewfromrellist__c s = new testnewfromrellist__c();
        insert s;
        p.getParameters().put('retURL', '/+s.id');
        test.setCurrentPage(p);
        ExtTestNewFromRelatedlist ext = new ExtTestNewFromRelatedlist(new ApexPages.StandardController(s));
        system.debug('cancelURL to the record: ' + ext.cancelURL);
        PageReference returnPR = ext.save();
        // return to saved record
        system.assertEquals(ext.cancelURL.getURL(),returnPR.getURL());
        
    
    }
    
    

}

 

The code changes the behavior of the save method by redirecting him/her to the parent record. It leverages the cancel action which  takes back to the previous screen when the user hits cancel. I believe this is what you want to do. 

 

 

 

 

kritinkritin

In this regard you will have to override the new button.

Asn on save you will have to write save method like as:

 

public PageReference save() {

Id AAAID='BVKJBGKJ447476vnj';

PageReference pageRef = new PageReference('/' + AAAID); 
         pageRef.setRedirect(true);
      
     return pageRef;

}

OdedHarnivOdedHarniv

Thank you very much

 

Is there any way to avoid writing a new page?

I really like the way the standard page is working, the behavior on Save is the only thing I want to change

 

Thanks a lot