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
Nicholas MelonasNicholas Melonas 

Custom Object Look-Up Relationship Field on Standard Object

Hello,

On my managed custom object called, "Projects" (MPM4_BASE__Milestone1_Project__c), we manually fill in the Opportunity Name (Opportunity_Name__c). However now whenever the opportunity name is filled in on the project object, I want the project to autopopulate the Project Name look-up relationship field (Project__c) on the Account standard object. How do I write this trigger?

Thanks!

Nick
Best Answer chosen by Nicholas Melonas
Abhishek BansalAbhishek Bansal
Hi Nicholas,

Thanks for expaining your requirement clearly . Please find below your required code.
Note : Please replace API names according to your Org.

trigger fillInAccount on MPM4_BASE__Milestone1_Project__c(after insert, after update) {
    Set<Id> oppIds = new Set<Id>();
    
    for(MPM4_BASE__Milestone1_Project__c proj : trigger.new){
        if(proj.Opportunity_Name__c != null){
            oppIds.add(proj.Opportunity_Name__c);
        }
    }
    Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select AccountId from Opportunity where id in :oppIds]);
    
    Set<Id> accIds = new Set<Id>();
    
    for(Opportunity opp : oppMap.values()){
        accIds.add(opp.AccountId);
    }
    
    Map<Id,Account> accMap = new Map<Id,Account>([Select Project__c from Account where Id In :accIds]);//Replace Project__c with your field API name
    
    for(MPM4_BASE__Milestone1_Project__c proj : trigger.new){
        if(oppMap.containsKey(proj.Opportunity_Name__c)) {
            accMap.get(oppMap.get(proj.Opportunity_Name__c).AccountId).Project__c  = proj.id; //Replace Project__c with your field API name
        }
    }
    update accMap.values();
}


Below are the cases which expalins the working of this trigger :

Case 1 : If you have two Project records with same Opportunity than the latest record created or updated with that opportunity will be stored in Account.

Case 2 : If you delete Opportunity from Project and that Project is populated on Account than the related Account will still be containing the Project.

Case 3 : If there are two Project records with same Opportunity and you delete one record that is existing on Account the Account Project field will be blank even after having a project record with same opportunity.

Please go through these all cases and let me know if you want any change in the trigger.

Regards,
Abhishek

 

All Answers

Abhishek BansalAbhishek Bansal
Hi Nicholas,

I understand your requirement but i have one query :

How do we get to know the Account in which we have to populate the Project field.
Does account is already having the opp name field or is there any other field on account on the basis of which we will populate project id in it.

Abhishek.
Nicholas MelonasNicholas Melonas
Hello! Thanks for your help! On the Account I have a custom look-up field that can populate the Project Name (Project__c). Right now I can do that manually, but I would like that to populate automatically. On the project we have a custom look-up field that populates the Opportunity and in our system the Opportunity and Accounts are connected. I was hoping that because the Opportunity and Account are connected and the Opportunity and Project are connected, we could now automatically populate the Account and the Project. Let me know if you need more information!
Abhishek BansalAbhishek Bansal
Hi Nicholas,

Thanks for expaining your requirement clearly . Please find below your required code.
Note : Please replace API names according to your Org.

trigger fillInAccount on MPM4_BASE__Milestone1_Project__c(after insert, after update) {
    Set<Id> oppIds = new Set<Id>();
    
    for(MPM4_BASE__Milestone1_Project__c proj : trigger.new){
        if(proj.Opportunity_Name__c != null){
            oppIds.add(proj.Opportunity_Name__c);
        }
    }
    Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select AccountId from Opportunity where id in :oppIds]);
    
    Set<Id> accIds = new Set<Id>();
    
    for(Opportunity opp : oppMap.values()){
        accIds.add(opp.AccountId);
    }
    
    Map<Id,Account> accMap = new Map<Id,Account>([Select Project__c from Account where Id In :accIds]);//Replace Project__c with your field API name
    
    for(MPM4_BASE__Milestone1_Project__c proj : trigger.new){
        if(oppMap.containsKey(proj.Opportunity_Name__c)) {
            accMap.get(oppMap.get(proj.Opportunity_Name__c).AccountId).Project__c  = proj.id; //Replace Project__c with your field API name
        }
    }
    update accMap.values();
}


Below are the cases which expalins the working of this trigger :

Case 1 : If you have two Project records with same Opportunity than the latest record created or updated with that opportunity will be stored in Account.

Case 2 : If you delete Opportunity from Project and that Project is populated on Account than the related Account will still be containing the Project.

Case 3 : If there are two Project records with same Opportunity and you delete one record that is existing on Account the Account Project field will be blank even after having a project record with same opportunity.

Please go through these all cases and let me know if you want any change in the trigger.

Regards,
Abhishek

 
This was selected as the best answer
Nicholas MelonasNicholas Melonas

@Abhishek Bansal

This is PERFECT. The trigger runs and it works great! How do I change this to an Apex Class so I can put it into production? Thank you so much!!

Abhishek BansalAbhishek Bansal
Why do u need to change it to class, You can also deploy triggers on production.
Is there any specific requirement for this?
Nicholas MelonasNicholas Melonas

We tried putting in the trigger to production, but an error appeared saying that we could not depoy triggers on an active organization. So, we deployed the trigger in our sandbox. However to get the trigger from our sandbox to our active organization, it needs to be in a test class. We are confused and are new to this! How do we write the apex class to get the trigger deployed in production? I am assuming we would need to make a test class that creates a new project and then inputs a name in the opportunity custom field. Thanks for your help!

 

@isTest
public with sharing class TestFillAccount {
  static testMethod void validateFillAccount() {
  	
  //test logic goes here
  MPM4_BASE__Milestone1_Project__c = new MPM4_BASE__Milestone1_Project__c();
  
 
  }
}
 

 

Abhishek BansalAbhishek Bansal
Hi, In order to deploy any apex class or trigger from sandbox to production your apex class or trigger must have the 75% code coverage. I guess this is the issue which you are facing. If i am right than please post this issue as a new question on forum and i will provide you a test class for the related trigger. After having the 75% code coverage you will be able to deploy your trigger along with your test class on production. Please post a new question regarding this and send me the link here so that i can help you out immediately. Regards, Abhishek
Nicholas MelonasNicholas Melonas