You need to sign in to do that
Don't have an account?
Nicholas 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
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
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
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.
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
@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!!
Is there any specific requirement for this?
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!