function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
stollmeyerastollmeyera 

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?

Best Answer chosen by Admin (Salesforce Developers) 
MiddhaMiddha

Couple of things here:

 

 

  • Your trigger would create a Conversation record every time Account record is updated, even if a user updates any other field on Account while it is having Type ='Customer'. To avoid this you should have an entry criteria specified :

 

if( a.Type == 'Customer' && (Trigger.isInsert || (Trigger.IsUpdate && a.Type!= Trigger.oldMap.get(a.Id).Type))

 

  • With your requirement it seems like your Account should only have a single conversation record related to an Account, because as soon as it is created all the contacts related to that account would start following/relate it. If at any time a new Conversation gets created to that Account, the Contacts would related to the new Conversation , this means that the Account should only have a single Conversation.
  • You need to query all the Contact records and populate the Conversation Id in all those Contact records by adding the Conversation Id in the Contact record "Conversation__c" field.
BTW, I really doubt if you want this, can you cross check again that you want Conversation as a Parent and Contact as a Child and not otherwise.

 

All Answers

forecast_is_cloudyforecast_is_cloudy

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)?

 

 

stollmeyerastollmeyera

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.

MiddhaMiddha

Couple of things here:

 

 

  • Your trigger would create a Conversation record every time Account record is updated, even if a user updates any other field on Account while it is having Type ='Customer'. To avoid this you should have an entry criteria specified :

 

if( a.Type == 'Customer' && (Trigger.isInsert || (Trigger.IsUpdate && a.Type!= Trigger.oldMap.get(a.Id).Type))

 

  • With your requirement it seems like your Account should only have a single conversation record related to an Account, because as soon as it is created all the contacts related to that account would start following/relate it. If at any time a new Conversation gets created to that Account, the Contacts would related to the new Conversation , this means that the Account should only have a single Conversation.
  • You need to query all the Contact records and populate the Conversation Id in all those Contact records by adding the Conversation Id in the Contact record "Conversation__c" field.
BTW, I really doubt if you want this, can you cross check again that you want Conversation as a Parent and Contact as a Child and not otherwise.

 

This was selected as the best answer
goabhigogoabhigo

 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.

 

 

trigger AddConversionUponAccountCustomer on Account (after insert, after update) {
    List<Conversions__c> cList1;
    Conversions__c conv; 
    List<Contact> lstC;
    for(Account a : trigger.new)
    {
     cList11 = [select Name,Account__c from Conversions__c where Account__c=a.Id];
     lstC = [select AccountId,Conversation__c from Contact where AccountId=a.Id];
     if(a.Type == 'Customer') {
        if(cList1.size()==0) {                 
           conv = new Conversions__c (Name = a.name, phone__c = a.Phone, email__c = a.Email__c, Account__c = a.Id);
           insert conv;
           for(Contact c:lstC)
              c.Conversation__c - conv.Id;
        }
     }  
}

 

 

This will work fine (since the SOQL is inside for loop I am not sure its the right practice)

MiddhaMiddha

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.

goabhigogoabhigo

Ok thanks Gulshan.

Can I write like this?

 

trigger AddConversionUponAccountCustomer on Account (after insert, after update) {
    List<Conversions__c> cList1;
    Conversions__c conv; 
    List<Contact> lstC;
    Account acc;
    for(Account a : trigger.new)
    {
     acc = a;
    }
     cList11 = [select Name,Account__c from Conversions__c where Account__c=acc.Id];
     lstC = [select AccountId,Conversation__c from Contact where AccountId=acc.Id];
     if(acc.Type == 'Customer') {
        if(cList1.size()==0) {                 
           conv = new Conversions__c (Name = acc.name, phone__c = acc.Phone, email__c = acc.Email__c, Account__c = acc.Id);
           insert conv;
           for(Contact c:lstC)
              c.Conversation__c = conv.Id;
        }
     }  
}

 

Does that make sense?

 

Or will this trigger fire only when one Account record is updated at a time?

stollmeyerastollmeyera

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 :)