+ Start a Discussion
dkccraigdkccraig 

Simple Problem, New User

I have a custom object with an Account setup as the detail in a master-detail relationship where the custom object is the master.

 

All I am trying to do is access the Account from the Custom Object.

 

How come I cannot just say CustomObject.Account?

Should I be using a query instead?

If I am going to use a query do I need to create a foreign key in the Account to link to the proper CustomObject?

 

Thanks in advance.

dkccraigdkccraig

Why are there so many threads with zero replies?  I am certain this is a very simple question.

 

Is there a superior community out there for this?

SuperfellSuperfell

perhaps because not many people work on sundays ?

 

Can you expand the context in which you're trying to do this? (apex, web services, etc), also if your custom object is master and account is detail, then there can be multiple accounts for a single custom object, so there can't be a simple account accessor on the custom object to access the account. 

dkccraigdkccraig

Thanks Simon.

 

I am just writing a trigger in Apex to copy one of the fields from the account to the Custom Object before insertion of a custom object.

 

So far I have what is below.

 

trigger TestTrigger on Custom_Object__c (before insert){

  for(Custom_Object__c co: Trigger.new){

    //This is all I have, but to copy 1 field shouldnt be more than a few more

    //lines of code I would imagine.

  }

dkccraigdkccraig

Alright, somehow, I think ive figured it out.

 

I am just trying to copy the various Billing fields in Account to the various Mailing fields in Contact where Account and Contact are both the details in a master-detail relationship with my Custom Object.

 

trigger InsertionTrigger on Junction_Object__c (before insert) 
{
  String currID;
  List<Junction_Object__c> billingList, mailingList;
  
  for(Junction_Object__c jo: Trigger.new)
  {
    currID = jo.ID;
    billingList = [select Account__r.BillingStreet, Account__r.BillingCity, 
                          Account__r.BillingState, Account__r.BillingPostalCode 
                          from Junction_Object__c where Junction_Object__c.ID = :currID];
                  
    mailingList = [select Contact__r.MailingStreet, Contact__r.MailingCity, 
                          Contact__r.MailingState, Contact__r.MailingPostalCode 
                          from Junction_Object__c where Junction_Object__c.ID = :currID];   
                  
    for(Integer index = 0; index < billingList.size(); index++)
    {
      mailingList[index].Contact__r.MailingStreet = billingList[index].Account__r.BillingStreet;
      mailingList[index].Contact__r.MailingCity = billingList[index].Account__r.BillingCity;
      mailingList[index].Contact__r.MailingState = billingList[index].Account__r.BillingState;
      mailingList[index].Contact__r.MailingPostalCode = billingList[index].Account__r.BillingPostalCode;
    }
  }
    
  update mailingList;
}

 

This seems to be rather convoluted to me though.  If there is a simpler solution, or I am doing something wrong, I would like to know so I can write better Apex code in the future.

 

Thanks,

Craig