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
Pranil SarodePranil Sarode 

How to update same picklist values on different custom objects using trigger

I have two different custom objects "Speaker__C" and "Session__c" both having "city__c" picklist field.
If I update City value on one object same should be populated on other object.
Both picklist have same values.
Best Answer chosen by Pranil Sarode
Ajay K DubediAjay K Dubedi
Hi Pranil,

instead of employe use write method in handler class.

trigger SessionTrigger on Session__c (after insert) {
    if(trigger.isAfter && trigger.isInsert){
        SessionTriggerApex.method(trigger.new);
    }
}

Handler class:
public class SessionTriggerApex {
    public static void method(List<Session__c> sessionList){
        Set<Id> sessionID = new Set<Id>();
        List<Speaker__c> speakerList = new List<Speaker__c>();
        
        for(Session__c s : sessionList) {
            Speaker__c speakerObj = new Speaker__c(); 
            // if name field is auto number then does not need to insert name
            speakerObj.Name = 'speaker name';
            speakerObj.City__c = s.City__c;
            speakerList.add(speakerObj);
        }
        insert speakerList;  
    }
}

Thanks,
Ajay Dubedi

All Answers

Rishabh Kumar 16Rishabh Kumar 16
You have to make workflow for that and action should be field update .In action you can update different object field.
Ajay K DubediAjay K Dubedi
Hi Pranil,

Try below code  it work according to your requirement:

trigger:
trigger SessionTrigger on Session__c (after insert) {
    if(trigger.isAfter && trigger.isInsert){
        SessionTriggerApex.method(trigger.new);
    }
}

Handler class:
public class SessionTriggerApex {
    public static void employee(List<Session__c> sessionList){
        Set<Id> sessionID = new Set<Id>();
        List<Speaker__c> speakerList = new List<Speaker__c>();
        
        for(Session__c s : sessionList) {
            Speaker__c speakerObj = new Speaker__c(); 
            // if name field is auto number then does not need to insert name
            speakerObj.Name = 'speaker name';
            speakerObj.City__c = s.City__c;
            speakerList.add(speakerObj);
        }
        insert speakerList;  
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Pranil SarodePranil Sarode
Hi Ajay,
Thank you for the solution, but I'm getting an error as " Method does not exist or incorrect signature: void method(List<Session__c>) from the type SessionTriggerApex"

-  Pranil
Ajay K DubediAjay K Dubedi
Hi Pranil,

instead of employe use write method in handler class.

trigger SessionTrigger on Session__c (after insert) {
    if(trigger.isAfter && trigger.isInsert){
        SessionTriggerApex.method(trigger.new);
    }
}

Handler class:
public class SessionTriggerApex {
    public static void method(List<Session__c> sessionList){
        Set<Id> sessionID = new Set<Id>();
        List<Speaker__c> speakerList = new List<Speaker__c>();
        
        for(Session__c s : sessionList) {
            Speaker__c speakerObj = new Speaker__c(); 
            // if name field is auto number then does not need to insert name
            speakerObj.Name = 'speaker name';
            speakerObj.City__c = s.City__c;
            speakerList.add(speakerObj);
        }
        insert speakerList;  
    }
}

Thanks,
Ajay Dubedi
This was selected as the best answer
Pranil SarodePranil Sarode
Thanks Ajay. It worked. But just used if condition as it was not populating same city name of other object.