+ Start a Discussion
Hilarye SchoytHilarye Schoyt 

Trigger or Flow? Need to automate creation of one junction record from another.

I have a doozy of a user story, and I'm stuck.  I will try to explain as best as I can. 

Objects in Question:
Account (Standard)  
Project (Custom) - Master Detail relation to Account.  
Project Services (Custom) -  Junction object between Service & Project.  
Service (Custom) - Just a list of services we offer that need to be linked to multiple other records.   
Current Services (Custom) - Junction object between service & account.

Each 'Account' can have multiple 'Projects' (Master Detail).  Each 'Project' can have multiple 'Services' (Junction Object).  Therefore each 'Account' can have multiple 'Services' (Junction Object).  

What I desperately need: When I relate a 'Service' record to a 'Project', through junction object 'Project Services', I need an identical relation to be created between 'Account' and 'Service', on junction object 'Current Services'.  Is this doable though a Flow or a Trigger?

EX:  'Account A' has just become an active client. User goes to create the new record 'Project 1' associated with 'Account A' on the custom 'Project' object.  While creating 'Project 1', user relates 'Service 1' & 'Service 2' to that project through junction object 'Project Services'.  At the same time a new record needs to be created on the junction object 'Current Services' relating that service to 'Account A'. Each 'Service' only need be added to 'Account A' once.

Think of this as a rollup of related 'Services' to the related 'Account' listed on a 'Project' record. End result being 'Account A' will always have a list of current services in a related list.

Help please?  I've been thinking about this for 3 straight days, and my brain hurts.  Feel free to ask clarifying questions if I make absolutely zero sense.

I've never written a trigger, so if that's my path, any advice or help would be much appreciated. 

Best Answer chosen by Hilarye Schoyt
Shivani Kolhe 11Shivani Kolhe 11

To automatically connect an 'Account' and a 'Service' through the 'Current Services' junction object when a 'Service' is linked to a 'Project' via the 'Project Services' junction object, you can use an Apex trigger. This trigger activates when a new record is added to the 'Project Services' junction object and handles the creation of records in the 'Current Services' junction object.
Within this trigger, you'll need to go through the newly added 'Project Services' records and make corresponding entries in the 'Current Services' junction object. This trigger should be built to handle bulk operations, ensuring it functions correctly even when multiple 'Project Services' records are added simultaneously. The trigger may have the following body:
trigger ProjectServicesTrigger on Project_Services__c (after insert) {
    List<Current_Services__c> currentServicesToInsert = new List<Current_Services__c>();
    
    for (Project_Services__c ps : Trigger.new) {
        // Query for the associated Service record
        Service__c service = [SELECT Id FROM Service__c WHERE Id = :ps.Service__c LIMIT 1];
        
        // Create a Current Services record linking the Account and Service
        Current_Services__c currentService = new Current_Services__c(
            Account__c = ps.Project__r.Account__c,
            Service__c = service.Id
        );
        
        currentServicesToInsert.add(currentService);
    }
    
    // Insert the new Current Services records
    if (!currentServicesToInsert.isEmpty()) {
        insert currentServicesToInsert;
    }
}

Be sure to thoroughly test the trigger to confirm it correctly creates and connects the 'Current Services' records to the 'Account' and 'Service' as intended

 

Let me know if this works for you!

All Answers

VinayVinay (Salesforce Developers) 
Hi Hilarye,

You can reach out to accelerate team who can help you on your requirement if you have premeir support plan.  Below are steps to request for same.

https://help.salesforce.com/s/articleView?id=000387492&type=1

Please mark as Best Answer if above information was helpful.

Thanks,
Cecil FerrellCecil Ferrell
It looks like you are describing a data model in a Salesforce or similar CRM (Customer Relationship Management) system. Let's break down the components you've mentioned:
Account (Standard): This is typically the standard object in CRM systems used to represent your customers or companies with whom you have a business relationship.
Project (Custom): You've created a custom object called "Project," which is related to the "Account" object using a Master-Detail relationship. This means that each project is tied to a specific account and is a child record of that account.
Project Services (Custom): This is another custom object that serves as a junction object between "Service" and "Project." It's likely used to establish a many-to-many relationship between services and projects. This means that a single service can be linked to multiple projects, and a single project can have multiple services associated with it.
Service (Custom): This custom object represents the various services your organization offers. It seems to be a standalone object listing your service offerings.
Current Services (Custom): This is another junction object, similar to "Project Services," but it's used to establish a many-to-many relationship between "Service" and "Account." This allows you to associate specific services with individual accounts. It suggests that an account can have multiple services, and each service can be linked to multiple accounts. QR Code Scanner (https://qrcodescanner.org/)

Overall, your data model seems to be designed to handle the relationships between accounts, projects, services, and the specific services associated with each account and project. This type of data model is common in CRM systems where you need to track customer accounts, projects or engagements, and the services or products associated with them. It allows for flexibility in managing these relationships and associating the relevant data.
Shivani Kolhe 11Shivani Kolhe 11

To automatically connect an 'Account' and a 'Service' through the 'Current Services' junction object when a 'Service' is linked to a 'Project' via the 'Project Services' junction object, you can use an Apex trigger. This trigger activates when a new record is added to the 'Project Services' junction object and handles the creation of records in the 'Current Services' junction object.
Within this trigger, you'll need to go through the newly added 'Project Services' records and make corresponding entries in the 'Current Services' junction object. This trigger should be built to handle bulk operations, ensuring it functions correctly even when multiple 'Project Services' records are added simultaneously. The trigger may have the following body:
trigger ProjectServicesTrigger on Project_Services__c (after insert) {
    List<Current_Services__c> currentServicesToInsert = new List<Current_Services__c>();
    
    for (Project_Services__c ps : Trigger.new) {
        // Query for the associated Service record
        Service__c service = [SELECT Id FROM Service__c WHERE Id = :ps.Service__c LIMIT 1];
        
        // Create a Current Services record linking the Account and Service
        Current_Services__c currentService = new Current_Services__c(
            Account__c = ps.Project__r.Account__c,
            Service__c = service.Id
        );
        
        currentServicesToInsert.add(currentService);
    }
    
    // Insert the new Current Services records
    if (!currentServicesToInsert.isEmpty()) {
        insert currentServicesToInsert;
    }
}

Be sure to thoroughly test the trigger to confirm it correctly creates and connects the 'Current Services' records to the 'Account' and 'Service' as intended

 

Let me know if this works for you!

This was selected as the best answer