+ Start a Discussion

Can I Attach a custom object to another custom object

Hi, I am pretty new to this and am hoping someone can help me.


Basically I want to create a button that will allow me to attach a number of secondary/sub custom objects to a primary custom object.


I would then be hoping to display the list of attached secondary/sub custom objects as a related list within the primary object.






Sounds like you would just want define a SFDC "Look-up" type field on your "secondary" object (to the primary object.)  Once you do so, the related listed will automatically be there for any of your "primary" object's layouts to use.


Hope this helps --






Hi, Thanks for the reply, this is very helpful, just wondering if you know of a way of limiting what is returned in the "Look-up", basically, I want to be able to related certain secondary objects with the primary object depending on information in both objects

Ok so I have been reviewing this and I think that the best think to do would be to have a button on the related list that will allow me to add the custom object to the related list manually.


Now to creating the button to perform the above!!!!

If you provide some more details about what should be tied to what based on what and when... then we could probably help you make this task pretty easy for a user. I'm thinking a custom button to a Visual force page that contains a search mechanism to populate a grid of wrapper objects that contain your secondary custom objects.  Then the user could search, check the ones they wanted to tie into the primary and click a button to link them together and return them to the primary object instance.


Hope it's not too much to dive into.  :smileywink:  It requires a APEX Controller, Visual Force page and a Secondary APEX Object Wrapper Class... as well as some test methods.  But, it's the slickest way I can think of to do it.


Hi, that is exactly what I want to do. 


Ok so we have one Custom Object which contains information on the service we provide to our customers, we now have some enhancements for these services and we would like to track the information for these enhancements in a related list but the information for the enhancement is the same type of object as the original, ie we only have one custom object for managing services.


When generating the list of service objects I would only need the ones matching the Account in the Primary Object.


As stated before I am pretty new to this so thanks a million for your help.


So, you have a custom lookup field on your service object that is linked to the service object type and probably called something like "Parent Service". Is that right?


Also, do you ALWAYS want to link any service objects who's account matches the parent account?  If so, then this just got way easier. Who needs visual force in this case. ;)


What about linking parents to parents? or in other words, how deep will the children go? just 1 level, 2 levels or infinate? and if not just 1 level, how do you know which are linked and which are not, or are they all linked?



Yea thats exactly what I have at the moment, I dont want to link all service objects who's account matches the parent account but I could probably do this based on location and Account or create another field in the parent that I could use to link the parent and child. I think at the moment I will only have two levels Parent and Child thats it

Ok, well finding out if you can do it on Account and Location makes a difference. 


It dawned on me that you will still need VF either way as S-Controls will not be supported for much longer. 


So, that difference is limited to if you VF page's controller just automatically does the work and returns the user to the original record, or if it keeps the user there and makes them search & choose which ones are to be linked.   


If it's only one level then I would also automatically filter out any that are already parents as well as those that are already tied to a parent. I would also put a rollup summary field on the services object that calculates the SUM of the child service records. 


So the select statement in your search would be something like this.Service__c services[] = [SELECT ID, Name, Account__c FROM Service__c WHERE Parent_Service__c = null AND Child_Service_Sum__c = 0 AND Account__c = :currentRecordAccountID AND (Your Search Criteria in here if needed)];  



Once you have them, you can load them to the grid, BUT you can't check them without loading them into a wrapper that contains a boolean like isSelected.  Then you bind the wrappers to the grid, which gets you your check boxes. 


Then on a submit, you loop through the wrappers looking for isSelected and setting those Lookups to the parent record ID and then save the list outside of the loop.  



One good piece of advice: Don't ever use select statements inside of loops.  That also goes for update, insert and delete.  You only get to use any combination of those up to 20 times for every user click before salesforce stops you and breaks your code.  So all DB actions need to remain outside of the loops.  Ok?  Otherwise you create pretty short limits for what your users can do. 

Now I know 'm babbling... so what do you need to know to start puting it together?

Message Edited by SiriusBlackOp on 06-30-2009 10:03 AM

As this is the first time I have done some development like this in Salesforce, I think I should take it one step at a time so the first step would be to create a new list button on the Custom Object (service object in my case)which will open up a page displaying a list of service objects that match the Account in the parent object.


Can you identify the steps require to building this up.