You need to sign in to do that
Don't have an account?
Paul 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?
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?
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
But not sure if the controller is in existance at the time the button is pressed, or what the global variable would be called...
You can write something like below:
But here you may need to use different VF pageswith the same controller.
Please do let me know if it helps you.
Regards,
Mahesh
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">