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
Paul DevenportPaul Devenport 

How to find id and type of object that contains a generic/reusable related list object. (custom obj has multiple lookup fields)

I have a button that launches a custom visualforce page.  The button can exist on multiple objects, so I need to do some sort of check to find the parent objects id and type so the custom controller knows what type of object it is dealing with...
This is kind of what I am wanting to do:
{!URLFOR("/apex/PD_CustomAttachment?id="+ !ParentObject.Id)}  gives me syntax error when saving...
I am looking for a literal keyword like ParentObject that refers to any parent and is not tied to a hardcoded type like:  Contact.Id
Any ideas?  Does this even exist in visualforce/apex?
Naval Sharma4Naval Sharma4
Hi Paul,

First you you are putting '!' next to ParentObject.Id in URLFOR function.

This isn't possible in Visualfoce, but you can get current record id. If you are using this button on object record then you can get the ID and use that ID as your parent object ID.
{!URLFOR("/apex/PD_CustomAttachment?id="+ Account.Id)} 
Do let me know if you need any help.

Thanks,
Naval
Paul DevenportPaul Devenport
Thanks.  My issue is that I have an object that I want to use on Account, Opportunity, Case, Contract, etc.  So I don't know what type the parent will be at code-time.  
Paul DevenportPaul Devenport
If I have access to the controller, something like this:  {!URLFOR("/apex/PD_CustomAttachment?id="+ controller.getRecord().Id)}
But not sure if the controller is in existance at the time the button is pressed, or what the global variable would be called...
Mahesh DMahesh D
Hi Paul,

You can write something like below:
 
Case cs {get; set;}
Opportunity opp {get; set;}
public ControllerCreateProposalView(ApexPages.StandardController stdController){
	if(stdController.getRecord().getsObjectType() == Case.sObjectType)
		cs = (Case) stdController.getRecord();
	if(stdController.getRecord().getsObjectType() == Opportunity.sObjectType)
		opp = (Opportunity) stdController.getRecord();
}

But here you may need to use different VF pageswith the same controller.

Please do let me know if it helps you.

Regards,
Mahesh
Paul DevenportPaul Devenport
Thanks for the answers, it got me thinking and I came up with a hack that works:  
I tested it with three parents (Contact, CONTRACT, Opportunity), all having a Lookup relationship from the Attachment.
 
Here is what I did:
 
On the customer “Add Attachment” button, added this URL:  {!URLFOR("/apex/PD_CustomAttachment?id=" +Contact.Id + Contract.Id + Opportunity.Id +
"&obj1=" +  Contact.Id + "&obj2=" + Contract.Id + "&obj3=" + Opportunity.Id )}
 
In the controller, check to see which url variable is set and use it as a flag to determine the parent object type:
public PD_CustomAttachmentController(){
        String contactId = ApexPages.currentPage().getParameters().get('obj1');
        String CONTRACTId = ApexPages.currentPage().getParameters().get('obj2');
        String opportunityId = ApexPages.currentPage().getParameters().get('obj3');
        if(contactId != ''){
            parentId = contactId;
            parentType = 1;
        }else if(CONTRACTId != ''){
            parentId = CONTRACTId;
            parentType = 2;
        }else if(opportunityId !=''){
            parentId = opportunityId;
           parentType = 3;
        }       
    }
 
Then in the code that saves the attachment, add the relationship reference from attachment to correct parent variable:
private Database.SaveResult saveCustomAttachment(){
        Custom_Attachment__c obj = new Custom_Attachment__c();
        if(parentType == 1){
            obj.contact__c = parentId;
        }else if(parentType == 2){
            obj.CONTRACT__c = parentId;
        }else if(parentType == 3){
            obj.opportunity__c = parentId;
        }
       
 
In visualforce page, just use my controller instead of standardController/extentioncontroller.
<apex:page   controller="PD_CustomAttachmentController">