You need to sign in to do that
Don't have an account?
Mapping the Parent Relationships from one object to another
I have added a simple trigger and it works great.
trigger AddConversionUponAccountCustomer on Account (after insert, after update) {
Conversions__c[] cList = new Conversions__c[]{};
for(Account a : trigger.new)
{
if(a.Type == 'Customer')
cList.add(new Conversions__c (Name = a.name, phone__c = a.Phone, email__c = a.Email__c, Account__c = a.Id));
}
insert cList;
As you can see, the way the trigger works is when the Type under an Account is marked 'Customer', I am adding a new custom object, Conversions__c.
The problem lies in the fact that I want to be be able to transfer the Contacts that are associated with the Account over to the new Conversions__c custom object that is created.
From my very small amount of exerpience that I have, I am thinking I need to use a List to query the Account.Contacts, grab the Contact.ID, and then map this over to Conversions__c.Contacts__r. Is this the proper thinking?
Couple of things here:
All Answers
First, note that your trigger fires on the 'after update' event also - in other words, a new Conversions__c record will be created every time that an Account record is updated (and not just when a new Account record is created). Not sure if that's what you expect to happen, but just thought I'd point it out. If you only want a new Conversions__c record to be created when a new Account record is created, then remove the 'after update' event from your trigger definition. Keeping Conversion records updated if the parent Account record is updated or deleted is another ball of wax, but I'll leave that one alone for now :)
Now to the question at hand. First, can you please clarify the relationship between the Conversions__c custom object and the Contact object? Is there a new custom lookup field on Contact that links to the Conversions__c custom object (which would make Conversions__c the parent and Contact the child in the relationship)?
The 'after update' is exactly what I am looking for as I only want this to fire once the account meets the criteria. After insert is not necesarry because an Account will never meet the criteria until after it is added.
As for your other question, there is a custom field under the Contact called "Conversions" Therefore, the Conversions__c is the parent and Contact is the child.
Thank you in advance for your help.
Couple of things here:
As the Admin pointed out your trigger will create multiple Conversation records everytime Account is updated(and meets the condition). To avoid it before the for loop you can write a SOQL statement which retreives the Conversation record whose Account__c is equal to current Account's Id.
This will work fine (since the SOQL is inside for loop I am not sure its the right practice)
Abhi,
Writing a SOQL in a for loop is not abvisable and this trigger will break if more than 20 Accounts are update using some tool. This is a bad practise and should not be done.
Ok thanks Gulshan.
Can I write like this?
Does that make sense?
Or will this trigger fire only when one Account record is updated at a time?
Ahhh, I completely mis read the original post by the admin...Thank you SO MUCH for pointing this out!! (its great being new)
I have updated the trigger to accomdate.
As for the Parent/Child setup, I do want the Conversion as a Parent record as I want to be able to assign more than one contact to a Conversion record, but not vice versa.
I appreciate everyones help in this matter; I just hope I can be this helfpful to newbies later down the road :)