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
Ali MeyerAli Meyer 

Multi-object trigger

Hello,

 

I'm trying to write a trigger that will do three things: 

1. take info from a custom object 

2. create an account from that info

3. create a contact from that info

 

I lifted this code from here but I don't really know what I'm doing. I think the first problem is declaring the Account variable; my error message says "Name" does not exist.

 

Thank you for your help!

 

trigger CreateContact on Intake_Phone_Screen_Information__c (after update, after insert) {
    
    List<Intake_Phone_Screen_Information__c> IntakeList = new List<Intake_Phone_Screen_Information__c>();
    List<Account> AccList = new List<Account>();
        for(Intake_Phone_Screen_Information__c i:Trigger.new){
              Account a = new Account(Name = i.Caller_First_Name__c, RecordTypeId = '012A0000000dlDz'); 
              }
              insert a;
    //newAccount has IDs since been inserted, so parallel list to Trigger.new
              
    Integer cnt = 0;
        for(Intake_Phone_Screen_Information__c i:Trigger.new){
              if (i.Boolean_Appointment__c = TRUE){
              cnt++;
              continue;
              }
              newContact.add(FirstName = i.Caller_First_Name__c, LastName = i.Caller_Last_Name__c, Date_entered_as_new_patient__c = System.today(), Account = newAccount[cnt].Id);
              cnt++; 
              }
     if (!newContact.isEmpty()) insert newContact;
    
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

For creating the records in Account & Contact based on the field availability in Intake_Phone_Screen_Information__c, just include a condition there thats enough.

 

Example:

 

trigger CreateAccountContact on Intake_Phone_Screen_Information__c(after update, after insert){
      List<Contact> conList = new List<Contact>();   //change this line
            if(Trigger.IsInsert || Trigger.IsUpdate){
                for(Intake_Phone_Screen_Information__c i : trigger.new){

                     if(i.Field1 != null){  //Include this line

                            .................................

                     }

                }

           }

}

 

 

Do the changes in the trigger.

 

Hope this helps you...!

 

Please give kudos and mark this as a solution, If you found this answer as useful to others as well.

All Answers

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

After analysing your code I understood that you are trying to insert a, that Account insance a is onside the for loop so, its a local variable that lost its scope, while it is refered outside the for loop.

 

Check the below loop and insert statement in your code,

 

 for(Intake_Phone_Screen_Information__c i:Trigger.new){
              Account a = new Account(Name = i.Caller_First_Name__c, RecordTypeId = '012A0000000dlDz'); 
              }
              insert a;

keep th insert statement as inside the for loop. 

 

I would advice not to use two loops to initialize the trigger. You can wirte it in the following way,

 

trigger CreateAccountContact on Intake_Phone_Screen_Information__c(after update, after insert){
      List<Contact> conList = new List<Account>();
            if(Trigger.IsInsert || Trigger.IsUpdate){
                for(Intake_Phone_Screen_Information__c i : trigger.new){
                       Account a = new Account(Name = i.Caller_First_Name__c, RecordTypeId = '012A0000000dlDz');
                       insert a; //You can directly insert an object

                       //Also before inserting you can add it in a list and add insert later
                       //in the outside of loop, because list is globally declared
                       Contact c = new Contact(FirstName = i.Caller_First_Name__c,
                                                            LastName = i.Caller_Last_Name__c,
                                                            Date_entered_as_new_patient__c = System.today(),
                                                            Account = a.Id);
                       conList.add(c);
                  }
            insert conList;
         }
  }

 

In the above trigger do whatever conditions you want to check before inserting your records.

 

Please give kudos and mark this as a solution, If you found this answer as useful to others as well.

Ali MeyerAli Meyer

Thanks so much for this! When I entered your code, I got an error message Compile Error: Illegal assignment from LIST<Account> to LIST<Contact> at line 2 column 7, and then if I changed that Compile Error: Invalid initial expression type for field Account, expecting: SOBJECT:Account (or single row query result of that type) at line 11 column 71 (which is the Account = a.id line)

 

Thanks for your help!

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Sorry while typing I missed something wrong,

 

Try this code,

trigger CreateAccountContact on Intake_Phone_Screen_Information__c(after update, after insert){
      List<Contact> conList = new List<Contact>();   //change this line
            if(Trigger.IsInsert || Trigger.IsUpdate){
                for(Intake_Phone_Screen_Information__c i : trigger.new){
                       Account a = new Account(Name = i.Caller_First_Name__c, RecordTypeId = '012A0000000dlDz');
                       insert a; //You can directly insert an object

                       //Also before inserting you can add it in a list and add insert later
                       //in the outside of loop, because list is globally declared
                       Contact c = new Contact(FirstName = i.Caller_First_Name__c,
                                                            LastName = i.Caller_Last_Name__c,
                                                            Date_entered_as_new_patient__c = System.today(),
                                                            AccountID = a.Id);  //change this line
                       conList.add(c);
                  }
            insert conList;
         }
  }

 


Hope this helps you...!

 

Please give kudos and mark this as a solution, If you found this answer as useful to others as well.

Ali MeyerAli Meyer

Thanks so much for this. The code compiles without error, but nothing happens when I create a new record on the intake object--neither the account nor the contact record is created. Any ideas why?

 

Thanks again for your help.

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

If you create record in Intake_Phone_Screen_Information__c object, you will get the Account and contact created accordingly with the informations you provided.

 


Check again!!

 

Hope this helps you...!

 

Please give kudos and mark this as a solution, If you found this answer as useful to others as well.

Ali MeyerAli Meyer

Thank you so much for this! One last question: if I wanted to include a conditional (ie create the contact and account if field X on the intake object = true), where should I include that? Can it put it in here:  if(Trigger.IsInsert || Trigger.IsUpdate){  ?

 

Thank you!!

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

For creating the records in Account & Contact based on the field availability in Intake_Phone_Screen_Information__c, just include a condition there thats enough.

 

Example:

 

trigger CreateAccountContact on Intake_Phone_Screen_Information__c(after update, after insert){
      List<Contact> conList = new List<Contact>();   //change this line
            if(Trigger.IsInsert || Trigger.IsUpdate){
                for(Intake_Phone_Screen_Information__c i : trigger.new){

                     if(i.Field1 != null){  //Include this line

                            .................................

                     }

                }

           }

}

 

 

Do the changes in the trigger.

 

Hope this helps you...!

 

Please give kudos and mark this as a solution, If you found this answer as useful to others as well.

This was selected as the best answer
Ali MeyerAli Meyer

Just one more question! Is there a way that I can add the newly created contact ID into a lookup field on the intake record? I don't see how this would work in a before trigger, but I'm holding out hope. 

 

Thank you so much!

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Then try the following,

 

trigger CreateAccountContact on Intake_Phone_Screen_Information__c(after update, after insert){
      List<Contact> conList = new List<Contact>();   //change this line
            if(Trigger.IsInsert || Trigger.IsUpdate){
                  for(Intake_Phone_Screen_Information__c i : trigger.new){
                   if(i.Field1 != null){  //Include this line
                       Account a = new Account(Name = i.Caller_First_Name__c, RecordTypeId = '012A0000000dlDz');
                       insert a; //You can directly insert an object

                   
                       Contact c = new Contact(FirstName = i.Caller_First_Name__c,
                                                            LastName = i.Caller_Last_Name__c,
                                                            Date_entered_as_new_patient__c = System.today(),
                                                            AccountID = a.Id);  //change this line
                       insert c; //Directly inserting a contact, so that this id can be assigned to ContactID__c lookup field in Intake
                       
                       i.ContactID__c = c.id;  //Include this line too, So Lookup field (ContactID__c) in Intake will be assigned with contact created newly
                     }
                }        
         }
  }

 

Hope this helps you...!

 

Please give kudos and mark this as a solution, If you found this answer as useful to others as well.