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 

How to write a trigger/class between two picklist fields across objects.

Hello,

On my custom object "Project" (), I have the custom field, "Status" (MPM4_BASE__Status__c). Whenever this status is updated, I need it to instantly update to the custom "Project Status" field on the Account object (Project_Status__c). I know this can be accomplished with a formula field, but I need this trigger to happen immediately without any edits on the Account, and need the "Project Status" field to remain as a picklist field. How do I write the trigger and class for this? Thanks so much! 
pconpcon
I'm assuming that you have some sort of link from Project to Account so you'll need to use that.  Your trigger would look something like
 
trigger statusUpdate on Project__c (after update, after insert) {
    Set<Account> accountsToUpdate = new Set<Account>();

    if (Trigger.isInsert) {
        for (Project__c project : Trigger.new) {
            if (project.Account__c != null) {
                accountsToUpdate.add(new Account(
                    Id = project.Account__c,
                    Project_Status__c = project.Status__c
                ));
            }
        }
    } else {
        for (Project__c project : Trigger.new) {
            Project oldProject = Trigger.oldMap.get(project.Id);

            if (
                project.Account__c != null &&
                project.Status__c != oldProject.Status__c
            ) {
                accountsToUpdate.add(new Account(
                    Id = project.Account__c,
                    Project_Status__c = project.Status__c
                ));
            }
        }
    }

    if (!accountsToUpdate.isEmpty()) {
        update accountsToUpdate;
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors

You will obviously need to change the object and field names to match what you have in your schema.
Abhishek BansalAbhishek Bansal
Hi Nicholas,

Please try with the below trigger code :
trigger statusUpdate on MPM4_BASE__Milestone1_Project__c (after insert, after update) {
    Set<Id> accountIds = new Set<Id>();
    for(MPM4_BASE__Milestone1_Project__c project : trigger.new){
    	if(project.Account__c != null){
    		accountIds.add(project.Account__c);
    	}
    }
    
    Map<Id,Account> mapOfAccount = new Map<Id,Account>();
    for(Account acc : [Select Project_Status__c from Account where id IN :accountIds]){
    	mapOfAccount.put(acc.id, acc);
    }
    List<Account> accountToUpdateList = new List<Account>();
    for(MPM4_BASE__Milestone1_Project__c project : trigger.new){
    	if(trigger.isInsert || (trigger.isUpdate && trigger.oldMap.get(project.id).MPM4_BASE__Status__c != project.MPM4_BASE__Status__c)){
    		if(mapOfAccount.containsKey(project.Account__c)){
    			mapOfAccount.get(project.Account__c).Project_Status__c = project.MPM4_BASE__Status__c;
    			accountToUpdateList.add(mapOfAccount.get(project.Account__c));
    		}
    	}
    }
    
    if(accountToUpdateList.size() > 0){
    	update accountToUpdateList;
    }
}
//Please take care of the following things :
//1. Replace Account__c with lookup field of Account on your custom Project object.
Let me know if you have any issue.

Thanks,
Abhishek
Chris SynnottChris Synnott
Hi Abhishek,

My name is Chris and I am helping Nick with these triggers...Thank you again for helping us implement these into our salesforce!! You are a huge help!

The only thing we are having problems with now is the test class. Do you have a test class that we could use to test this trigger?

Thanks,

Chris Synnott
Abhishek BansalAbhishek Bansal
Hi Nicholas/Chris,

As your original question have been answered so i would suggest you to close this question right here by marking it as Solved.
If you want a test class for your trigger than please post the related question on another forum.
That would help others to find different solutions of different questions on different forums.

Please close this question here and send me the link of new question here and i will try my level best to provide you a feasible solution as soon as possible.
In future please take care of the thing of not mixing different questions on a single forum.

Thanks,
Abhishek
Jean-LucJean-Luc

Hello
In the Opportunity object, I created three record types and two custom fields:
- a picklist "Opportunity Sub-type" controlling 
- a picklist "Funnel Stages" with in fact exactly the same values as the picklist standard field "StageName"
The purpose is to limit in the different record types the stages to use for each Opportunity Sub-Type.
As the field StageName is mandatory on Opportunity and not possible to define field dependencies with, I created a trigger to sync the StageName value when the same one is selected in the "Funnel_Stages__c" picklist.
------------
trigger UpdateSource on Opportunity (after update) 
{                                                                      
  List<Opportunity> opportunitiesToUpdate = new List<Opportunity>();     
  for(Opportunity currOpportunity : trigger.new) 
  {
    currOpportunity.StageName= currOpportunity.Funnel_Stages__c;          
    leadsToUpdate.add(currOpportunity);                                       
  }
  update opportunitiesToUpdate;                                        
}
--------------------

Not error when compiled but well when activated as I had thefollowing error message on the Opportunity page:
-------------------
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger UpdateSource caused an unexpected exception, contact your administrator: UpdateSource: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.UpdateSource: line 6, column 1
-----------------
Can somebody help me?
Thanks already.
Best regards
Jean-Luc

pconpcon
Jean-Luc,

Please open this up as a new question on the boards.  This does not related to the question here and will only confuse the issue.
Jean-LucJean-Luc
Sorry confusing your question. 
I've indeed created a new one. 
Rgds