+ Start a Discussion
BencredibleBencredible 

Update account and company info on custom object

I have searched the boards looking for an answer, and can't quite find what I'm looking for.

We have a custom object called 'domains' which is simply a form listing off any domains that a user may have purchased through us.  Each contact is associated with an account, a company and an office.  In the cases section when we select a contact it auto-populates the account and office information.  In this new custom object it doesn't seem to grab the account or office info.

Tried creating a trigger to update these fields, but was not successful.  I'm a bit of a SalesForce n00b and would greatly appreciate any help in creating this function.  Basically, when we select a contact the contacts account and office information should auto populate those fields, either on save or when it is selected.

Help?

Thanks,

Benjamin Higginbotham
Best Answer chosen by Admin (Salesforce Developers) 
ryan_marplesryan_marples
Opps, a few brackets were missed in the code I pasted in before. Try this:

1: trigger SetAccountField on Domains__c (before insert, before update) {
2: for (Domains__c domain : Trigger.new) {
3: String cid = domain.Contact__c;
4: List<Contact> contacts = [SELECT AccountId FROM Contact WHERE Id = :cid];
5: domain.Account__c = contacts.get(0).AccountId;
6: }
7: }


Here's a walkthrough of it:
1: Declare this trigger to execute whenever a Domain__c is created or updated
2: Run a loop through each Domain__c that was created or updated (because sometimes things happen in batch)
3: Grab the Contact's ID that the user entered (this assumes your field name is called Contact__c, you may need to adjust this)
4: Run a query to the Contact table and find that associated account ID
5: Store this account ID in the Account__c field (again, your field name may be different)

Ryan

All Answers

ryan_marplesryan_marples
Hi Benjamin,

What about setting up the account and office fields on your domain object as formulas? Formulas can now reference related objects. The formula could be something like this:

Contact__r.Account.Country

This would retrieve the Country of the Contact's associated account.

Ryan
BencredibleBencredible
Yes, but we need to attach it to the account record... So when I John Smith purchases the domain johnsmith.com I should be able to see that in his contact record, I should see that in the office record, company and account records.  Would a formula allow me to attach it to these records in such a way?

Right now by manually selecting the account, company and office it is attaching properly... Just not automated like I would like.  Gotta make it simple for the users :)

Thanks,

B
ryan_marplesryan_marples
Ah ok, I see. Yeah so the Case object's functionality to auto-fill in the account is a bit of internal voodoo that we do and I'm not aware of a method to reproduce it directly. However, here's a potential work-around:

Leave the Account and Office fields as lookups but make them read-only (via Field-level security). This way, they will only show up when viewing a domain, not when creating or editing one, but the Contact field will show on both view and edit. Then setup an Apex trigger that will update these fields based on the Contact that was entered. This way those fields will be automatically updated for you.

Here's an example trigger that updates the Account field on a "Thing" object based on the Contact field.

trigger SetAccountField on Thing__c (before insert, before update) {
for (Thing__c thing : Trigger.new) {
String cid = thing.Contact__c;
List contacts = [SELECT AccountId FROM Contact WHERE Id = :cid];
thing.Account__c = contacts.get(0).AccountId;
}
}


Ryan
BencredibleBencredible
OK, again... total n00b, so I would appreciate a bit of guidance.

I created a sandbox because it appears that I can't create a new trigger in anything but...

I modified the code to read as follows:

Code:
trigger SetAccountField on Domains__c (before insert, before update) {
for (Domains__c Domains : Trigger.new) {
String cid = domains.Contact__c;
List Contacts = [SELECT AccountId FROM Contact WHERE Id = :cid];
domains.Account__c = contacts.get(0).AccountId;
}

Pretty much changing out 'thing' for either domain or domains depending on the field used...  But when I run it I get an error 'Compile Error: expecting a left angle bracket, found 'Contacts' at line 4 column 14'

 Tried adding brackets around 'Contacts' to no avail.

Thoughts?

ryan_marplesryan_marples
Opps, a few brackets were missed in the code I pasted in before. Try this:

1: trigger SetAccountField on Domains__c (before insert, before update) {
2: for (Domains__c domain : Trigger.new) {
3: String cid = domain.Contact__c;
4: List<Contact> contacts = [SELECT AccountId FROM Contact WHERE Id = :cid];
5: domain.Account__c = contacts.get(0).AccountId;
6: }
7: }


Here's a walkthrough of it:
1: Declare this trigger to execute whenever a Domain__c is created or updated
2: Run a loop through each Domain__c that was created or updated (because sometimes things happen in batch)
3: Grab the Contact's ID that the user entered (this assumes your field name is called Contact__c, you may need to adjust this)
4: Run a query to the Contact table and find that associated account ID
5: Store this account ID in the Account__c field (again, your field name may be different)

Ryan
This was selected as the best answer
BencredibleBencredible
YOU ARE MY HERO!  Not only did your APEX Trigger work, but with that wonderful explanation I was able to reverse engineer the code and add updates to our own custom fields at the same time.

Thank you!
BencredibleBencredible
Spoke too soon ;)

So this APEX Trigger works great in the sandbox.  After reading a bit I learned that I needed Eclipse to push to production.  Installed that and got it linked to my sandbox.  The trigger I added to the sandbox downloaded.  Yeah!

I right-click on the trigger, select 'deploy' and enter all my info... Get to a validate screen and it comes back with a 0% validation.

After reading up on the forums a bit it sounded like I just needed to fire off the trigger a couple of times.  So I went through, added a couple of domains, saw the account and company info get updated (w00t) but I was unable to get Eclipse to say that it was more than 0% verified.

Not sure if it helps, but this is the code I ended up using:
Code:
trigger SetAccountField on Domains__c (before insert, before update) {
    for (Domains__c domain : Trigger.new) {
        String cid = domain.Contact__c;
        List<Contact> contacts = [SELECT AccountId FROM Contact WHERE Id = :cid];
        List<Contact> company = [SELECT Company__c FROM Contact WHERE Id = :cid];
        domain.Account__c = contacts.get(0).AccountId;
        domain.Company__c = company.get(0).Company__c;
    }
 }

 I must be doing something very simple wrong.

Thoughts?

ryan_marplesryan_marples
It may have to do with the fact that you haven't written unit tests for the Apex triggers. Here's an example unit test that I've got in my org in addition to the trigger:

@isTest
private class TriggersTest {

// Tests the SetAccountFieldTrigger which should fill in the Account field
// when a Contact is filled in.
static testMethod void testSetAccountFieldTrigger() {
// Create a dummy account and contact
Account a = new Account(Name='test account');
insert a;

Contact c = new Contact(LastName='test contact', AccountId=a.Id);
insert c;

// Create a 'Thing' and assign the contact
Thing__c t = new Thing__c(Name='test thing', Contact__c=c.Id);
insert t;

// Now select Thing back from the database
t = [SELECT Account__c FROM Thing__c WHERE Id=:t.Id];

// Ensure that after insertion, the account ID is filled in correctly
System.assert(t.Account__c == a.Id);
}
}

If not, fire me a screenshot of the validation error at rmarples at salesforce.com.

Ryan

Message Edited by ryan_marples on 11-06-2008 07:45 AM
sfdevnicksfdevnick

Ben,

 

Did you get this to work? If so, could you outline the steps you took from beginning to end?

 

Thanks!

MPetersenMPetersen

Hi Ryan, I work with Ben and this worked perfectly.  Thank you so much. 

 

Testing is the most confusing part of the SalesForce development process.  Could you point me towards a few good sources to learn what needs to be done in order to get through testing.

 

Thanks again!