2

I am attempting to adapt some code which was originallly written to work off of an Account object to now work on a custom object. Part of the code is referring to the Parent Account of whichever Account is being processed in order to see if the Owner of the Parent Account is different than this Account. In doing this, it is using the Parent and ParentId.

I am confused as to whether or not I can still use Parent and ParentId with a custom object to accomplish this same thing. This custom object (named Org_Model_c) has a Master\Detail relationship with the Account object, using its Account_c field as the connection.

Here is the current code :

public class CSH_and_Share_Pair {
        Client_Status_History__c csh;
        Client_Status_History__Share share;
        //Account acc;
        Id accountParent;
        Id accountOwnerId;
        Id accountParentOwnerId;
        String accountParentName;
        String accountParentOwnerUsername;


        CSH_and_Share_Pair(Account a, Client_Status_History__c csh) {
            this.accountParentName = a.Parent.Name;
            this.accountParentOwnerUsername = a.Parent.Owner.Username;

            this.csh = csh;
            // Ok, the CSH record is loaded and ready to be inserted.  But we also want to evaluate
            // the ownership of the Account and it's Parent Account (if present).  If they are different
            // we want to create a Share record to allow that owner access to this record.

            // So, if we have a non-null Parent Account which has a different Owner that the Account, 
            // create a share, otherwise not.
            if ((a.Parent != null) && (a.OwnerId != a.Parent.OwnerId)) {
                share = new Client_Status_History__Share(AccessLevel='Edit', UserOrGroupId= a.Parent.OwnerId);
            } else {
                share = null;
            }
        }

        private void reconcileParentId() {
            if (csh.id != null && share != null) share.ParentId = csh.id;
        }
        private String skipShare() {
            share = null;
            return 'Parent Account: ' + accountParentName + ' has an inactive owner.  Username: ' + accountParentOwnerUsername + '.';
        }
    }

So, instead of running on the Account object this will now run on the Org_Model__c custom object. But I would still like to refer to the Account associated with this Org_Model_c which is in Account_c field. Since the Account object is the Parent to Org_Model__c could I just substitute "Account" for "Org_Model_c" and continue using Parent to identify the Account, or do I need to refer to the child relationship name associated with the Account_c field such as :

Org_Model__r.Parent.OwnerID

Thank you very much for your time and help.

1 Answer 1

3

For custom objects and Custom relationsships, you need to use the relationship suffix. So if the Account lookup on your custom object is named Account__c, then you would refer to it's fields by using the following format

//your custom object
Org_Model__c o = new Org_Model__c();

//how to access fields on the account related to your object
o.Account__r.OwnerId

Here are some great references to use when dealing with relationships in SF

http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm

http://blogs.developerforce.com/systems-integrator/2009/02/navigating-relationships.html

2
  • ok, thank you. I wasn't sure if I had the ability to use Parent even with a custom object when it came to Sharing. In this case share.ParentId is used. Would I be able to just use share.Account__r.Id ... ?
    – Zoom_v
    Commented Oct 29, 2013 at 17:37
  • 1
    ParentId is a field on the share object, so that field should still be fine to use. If you want to access the AccountShare record for the account associated to your object, then you need to query for it. Commented Oct 29, 2013 at 17:49

You must log in to answer this question.

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