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
naclee01naclee01 

Apex Trigger Help

So I'm kind of new to using Apex and lately I've been trying to build a trigger to replicate the default functionality of the NPSP that automatically creates an organization for a contact that is not associated with an organization at creation for a custom object I have called staff member. I'm trying to work with the developer book provided by SalesForce but I'm having trouble finding useful examples so I've started looking for code bits I think might work and using trial and error methods to test them. So far no luck. I was hoping someone here could point me in the right direction as I have no idea if what I'm trying is close or if I'm way off. 

 

Basically, I have three objects: a staff member, an organization, and an affiliation. Using an Apex trigger I want to be able to create an organization object and an affiliation object after an insert to staff member. The organization object will hold information from the staff member object and the affiliation object will link the staff member and organization objects with their IDs.  

 

Here's the code I have right now:

 

 

trigger CreateNewOrg on Staff_Member__c (after insert) { List<Organization__c> newOrgs = new List<Organization__c>(); // build list in memory List<Affiliation__c> newAffs = new List<Affiliation__c>(); for (Staff_Member__c member : Trigger.new) { Organization__c newOrg = new Organization__c(); newOrg.Organization_Name__c = member.Last_Name__c; newOrgs.add(neworg); Affiliation__c newAff = new Affiliation__c(); newAff.Staff_Member__c = member.ID; newAff.Organization__c = newOrg.ID; newAffs.add(newAff); } Database.SaveResult[] orgs = Database.insert(newOrgs, false); Database.SaveResult[] affs = Database.insert(newAffs, false);}

 

Just to explain how I think this code should be working, it starts off by creating two lists, one of type object and the other of type affiliation. Then for each staff member, create a new organization, set the organization name to the value of the staff member's last name and then add it to the list of organizations. Then create a new affiliation object, set the staff member field to the current staff member's ID and the organization field to the organization's ID, then add the affiliation to the list of affiliations. After going through all the new staff members, write the new lists of organization and affiliation objects to the database.  

 

I really appreciate any help and advice anyone can give me on this topic. 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
arunkarunk

Hi naclee01,

 

The problem with your code is that you are using a object in another object while it has not been inserted. 

In your code you are assigning newAff.Organization__c = newOrg.ID. But here newAff does not have an ID because it has not been inserted yet. ID will be assigned only once the object has been inserted.

What you will have to do is something like below :

 

 trigger CreateNewOrg on Staff_Member__c (after insert) {

for (Staff_Member__c member : Trigger.new) {

Organization__c newOrg = new Organization__c();

newOrg.Organization_Name__c = member.Last_Name__c;

insert newOrg;

Affiliation__c newAff = new Affiliation__c(); newAff.Staff_Member__c = member.ID;

newAff.Organization__c = newOrg.ID;

insert newAffs;

}

}

 

I agree that it is bad practise to use DML operations in Loop.

So, a better approach would be to only creatre a Organization__c object in this trigger.

And then add a after Insert trigger on Organization__c which will create the  Affiliation__c object.

 

This looks better, but the process remains same..

1) insert  Organization__c

2) insert  Affiliation__c

 

 

A third approach is :

  trigger CreateNewOrg on Staff_Member__c (after insert) {

   List<Organization__c> newOrgs = new List<Organization__c>(); 

List<Affiliation__c> newAffs = new List<Affiliation__c>(); 

 

for (Staff_Member__c member : Trigger.new) {

Organization__c newOrg = new Organization__c();

newOrg.Organization_Name__c = member.Last_Name__c;

newOrgs.add(neworg);

}

Database.SaveResult[] orgs = Database.insert(newOrgs, false);  

   for (Database.SaveResult saveResult: orgs) {

      if(saveResult.isSuccess()){

    Affiliation__c newAff = new Affiliation__c();

newAff.Staff_Member__c = member.ID;

newAff.Organization__c = saveResult.getId();

  newAffs.add(newAff);  

      } 

}

   Database.SaveResult[] affs = Database.insert(newAffs, false); 

}

 

 

 

Regards,

Arun