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
DexPaPaDexPaPa 

Update a value of a field when a field on a different object is updated

Custom object Production_Request__c has a picklist field named Production_Status__c which contains the values New, Approved, In Progress and Completed.
Custom object Merchandise__c has a picklist field named Status__c which contains the Item Requested, Production approved, In Production and Available.
Studying salesforce apex, I have 2 objects one is name Merch and the other is Production, the production object has a look up field to get the names of items available on the Merchandise object. What I want to do is when the status field of the Production record which is picklist is set to "New" the status field on the Merchandise record will update to "Item Requested" , when the production request status is set to "Approved" the status on the merchandise record will be changed to "Prodution Approved". Each update on the Production record status field there will be matching update on the Merch record status field.
Best Answer chosen by DexPaPa
CharuDuttCharuDutt
Hii Dex 
Refresh The Dev Console And try  Save It Again
trigger ProductionRequestTrigger on Production_Request__c  (after insert,after update) {

    if(trigger.isAfter){
        if(trigger.isInsert){
            ProductionRequestTriggerHelper.insertmethod(trigger.new);
        }else if(trigger.isUpdate){
            ProductionRequestTriggerHelper.Updatemethod(trigger.new,trigger.oldmap);
        }
    }
}

############################################################################


public class ProductionRequestTriggerHelper{
    public static void insertmethod(list<Production_Request__c> newProductionRequest ){
        for(Production_Request__c  pr  : newProductionRequest){
            string updatepicklistValueinMerch;
            if(pr.Production_Status__c == 'New'){
                updatepicklistValueinMerch = 'Item Requested';
            }else if(pr.Production_Status__c == 'Approved'){
                updatepicklistValueinMerch = 'Production approved';
            }else if(pr.Production_Status__c == 'In Progress'){
                updatepicklistValueinMerch = 'In Production';
            }else if(pr.Production_Status__c == 'Completed'){
                updatepicklistValueinMerch = 'Available';
            }
            
            if(updatepicklistValueinMerch != null){
                ProductionMap.put(pr.Id,updatepicklistValueinMerch); 
            }
            
        }
        list<Merchandise__c>lstmerch = [select Id,Status__c,Production_Request__c from Merchandise__c where Production_Request__c in :ProductionMap.keySet()] ;
        for(Merchandise__c  merch  : lstmerch){
            if(ProductionMap.containsKey(merch.Production_Request__c)){
                merch.Status__c = ProductionMap.get(merch.Production_Request__c);
            }
        }
        if(lstmerch.size()>0){
            update lstmerch;
        }
    }
    public static void Updatemethod(list<Production_Request__c> newProductionRequest,map<Id,Production_Request__c> oldmap ){
        for(Production_Request__c  pr  : newProductionRequest){
            string updatepicklistValueinMerch;
            if(pr.Production_Status__c == 'New' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'Item Requested';
            }else if(pr.Production_Status__c == 'Approved' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'Production approved';
            }else if(pr.Production_Status__c == 'In Progress' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'In Production';
            }else if(pr.Production_Status__c == 'Completed' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'Available';
            }
            
            if(updatepicklistValueinMerch != null){
                ProductionMap.put(pr.Id,updatepicklistValueinMerch); 
            }
        }
        list<Merchandise__c>lstmerch = [select Id,Status__c,Production_Request__c from Merchandise__c where Production_Request__c in :ProductionMap.keySet()];
        for(Merchandise__c  merch  : lstmerch){
            if(ProductionMap.containsKey(merch.Production_Request__c)){
                merch.Status__c = ProductionMap.get(merch.Production_Request__c);
            }
        }
        if(lstmerch.size()>0){
            update lstmerch;
        }
    }
}

 

All Answers

CharuDuttCharuDutt
Hii Dex
Try Below Trigger
trigger ProductionRequestTrigger on Production_Request__c  (after insert,after update) {
    map<String,String> ProductionMap = new map<String,String>();
    if(trigger.isAfter){
        if(trigger.isInsert){
            for(Production_Request__c  pr  : trigger.new){
                string updatepicklistValueinMerch;
                if(pr.Production_Status__c == 'New'){
                    updatepicklistValueinMerch = 'Item Requested';
                }else if(pr.Production_Status__c == 'Approved'){
                    updatepicklistValueinMerch = 'Production approved';
                }else if(pr.Production_Status__c == 'In Progress'){
                    updatepicklistValueinMerch = 'In Production';
                }else if(pr.Production_Status__c == 'Completed'){
                    updatepicklistValueinMerch = 'Available';
                }
                
                if(updatepicklistValueinMerch != null){
                   ProductionMap.put(pr.Id,updatepicklistValueinMerch); 
                }
                
            }
        }else if(trigger.isUpdate){
            for(Production_Request__c  pr  : trigger.new){
                string updatepicklistValueinMerch;
                if(pr.Production_Status__c == 'New' && pr.Production_Status__c!= trigger.oldMap.get(pr.Id).Production_Status__c){
                    updatepicklistValueinMerch = 'Item Requested';
                }else if(pr.Production_Status__c == 'Approved' && pr.Production_Status__c!= trigger.oldMap.get(pr.Id).Production_Status__c){
                    updatepicklistValueinMerch = 'Production approved';
                }else if(pr.Production_Status__c == 'In Progress' && pr.Production_Status__c!= trigger.oldMap.get(pr.Id).Production_Status__c){
                    updatepicklistValueinMerch = 'In Production';
                }else if(pr.Production_Status__c == 'Completed' && pr.Production_Status__c!= trigger.oldMap.get(pr.Id).Production_Status__c){
                    updatepicklistValueinMerch = 'Available';
                }
                
                if(updatepicklistValueinMerch != null){
                   ProductionMap.put(pr.Id,updatepicklistValueinMerch); 
                }
            }
        }
    }
    
    list<Merchandise__c>lstmerch = [select Id,Status__c,Production_Request__c from Merchandise__c where Production_Request__c in :ProductionMap.keySet()]; ;
        for(Merchandise__c  merch  : lstmerch){
            if(ProductionMap.containsKey(merch.Production_Request__c)){
                merch.Status__c = ProductionMap.get(merch.Production_Request__c);
            }
        }
    if(lstmerch.size()>0){
        update lstmerch;
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!
DexPaPaDexPaPa
Thanks CharuDutt! Can this be done using a class and calling them on the trigger file? By the way tested the code and it worked. Thanks
CharuDuttCharuDutt
Hii Dex
Try Below Code
trigger ProductionRequestTrigger on Production_Request__c  (after insert,after update) {

    if(trigger.isAfter){
        if(trigger.isInsert){
            ProductionRequestTriggerHelper.insertmethod(trigger.new);
        }else if(trigger.isUpdate){
            ProductionRequestTriggerHelper.Updatemethod(trigger.new,trigger.oldmap);
        }
    }
}

######################################################################


public class ProductionRequestTriggerHelper{
    public static void insertmethod(list<Production_Request__c> newProductionRequest ){
        for(Production_Request__c  pr  : newProductionRequest){
            string updatepicklistValueinMerch;
            if(pr.Production_Status__c == 'New'){
                updatepicklistValueinMerch = 'Item Requested';
            }else if(pr.Production_Status__c == 'Approved'){
                updatepicklistValueinMerch = 'Production approved';
            }else if(pr.Production_Status__c == 'In Progress'){
                updatepicklistValueinMerch = 'In Production';
            }else if(pr.Production_Status__c == 'Completed'){
                updatepicklistValueinMerch = 'Available';
            }
            
            if(updatepicklistValueinMerch != null){
                ProductionMap.put(pr.Id,updatepicklistValueinMerch); 
            }
            
        }
        list<Merchandise__c>lstmerch = [select Id,Status__c,Production_Request__c from Merchandise__c where Production_Request__c in :ProductionMap.keySet()]; ;
            for(Merchandise__c  merch  : lstmerch){
                if(ProductionMap.containsKey(merch.Production_Request__c)){
                    merch.Status__c = ProductionMap.get(merch.Production_Request__c);
                }
            }
        if(lstmerch.size()>0){
            update lstmerch;
        }
    }
    public static void Updatemethod(list<Production_Request__c> newProductionRequest,map<Id,Production_Request__c> oldmap ){
        for(Production_Request__c  pr  : newProductionRequest){
            string updatepicklistValueinMerch;
            if(pr.Production_Status__c == 'New' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'Item Requested';
            }else if(pr.Production_Status__c == 'Approved' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'Production approved';
            }else if(pr.Production_Status__c == 'In Progress' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'In Production';
            }else if(pr.Production_Status__c == 'Completed' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'Available';
            }
            
            if(updatepicklistValueinMerch != null){
                ProductionMap.put(pr.Id,updatepicklistValueinMerch); 
            }
        }
        list<Merchandise__c>lstmerch = [select Id,Status__c,Production_Request__c from Merchandise__c where Production_Request__c in :ProductionMap.keySet()]; ;
            for(Merchandise__c  merch  : lstmerch){
                if(ProductionMap.containsKey(merch.Production_Request__c)){
                    merch.Status__c = ProductionMap.get(merch.Production_Request__c);
                }
            }
        if(lstmerch.size()>0){
            update lstmerch;
        }
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!
CharuDuttCharuDutt
Please Close Your Query By Marking It As Best Answer So It Also Help Others In Future
Thank You!
DexPaPaDexPaPa
Hi the second batch of codes did not work for me, there are problems shown on the dev console both on the trigger and the helper file. Still thanks for the help! Really appreciate it.
Error shown when the second batch of code is used.
CharuDuttCharuDutt
Hii Dex 
Refresh The Dev Console And try  Save It Again
trigger ProductionRequestTrigger on Production_Request__c  (after insert,after update) {

    if(trigger.isAfter){
        if(trigger.isInsert){
            ProductionRequestTriggerHelper.insertmethod(trigger.new);
        }else if(trigger.isUpdate){
            ProductionRequestTriggerHelper.Updatemethod(trigger.new,trigger.oldmap);
        }
    }
}

############################################################################


public class ProductionRequestTriggerHelper{
    public static void insertmethod(list<Production_Request__c> newProductionRequest ){
        for(Production_Request__c  pr  : newProductionRequest){
            string updatepicklistValueinMerch;
            if(pr.Production_Status__c == 'New'){
                updatepicklistValueinMerch = 'Item Requested';
            }else if(pr.Production_Status__c == 'Approved'){
                updatepicklistValueinMerch = 'Production approved';
            }else if(pr.Production_Status__c == 'In Progress'){
                updatepicklistValueinMerch = 'In Production';
            }else if(pr.Production_Status__c == 'Completed'){
                updatepicklistValueinMerch = 'Available';
            }
            
            if(updatepicklistValueinMerch != null){
                ProductionMap.put(pr.Id,updatepicklistValueinMerch); 
            }
            
        }
        list<Merchandise__c>lstmerch = [select Id,Status__c,Production_Request__c from Merchandise__c where Production_Request__c in :ProductionMap.keySet()] ;
        for(Merchandise__c  merch  : lstmerch){
            if(ProductionMap.containsKey(merch.Production_Request__c)){
                merch.Status__c = ProductionMap.get(merch.Production_Request__c);
            }
        }
        if(lstmerch.size()>0){
            update lstmerch;
        }
    }
    public static void Updatemethod(list<Production_Request__c> newProductionRequest,map<Id,Production_Request__c> oldmap ){
        for(Production_Request__c  pr  : newProductionRequest){
            string updatepicklistValueinMerch;
            if(pr.Production_Status__c == 'New' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'Item Requested';
            }else if(pr.Production_Status__c == 'Approved' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'Production approved';
            }else if(pr.Production_Status__c == 'In Progress' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'In Production';
            }else if(pr.Production_Status__c == 'Completed' && pr.Production_Status__c!= oldMap.get(pr.Id).Production_Status__c){
                updatepicklistValueinMerch = 'Available';
            }
            
            if(updatepicklistValueinMerch != null){
                ProductionMap.put(pr.Id,updatepicklistValueinMerch); 
            }
        }
        list<Merchandise__c>lstmerch = [select Id,Status__c,Production_Request__c from Merchandise__c where Production_Request__c in :ProductionMap.keySet()];
        for(Merchandise__c  merch  : lstmerch){
            if(ProductionMap.containsKey(merch.Production_Request__c)){
                merch.Status__c = ProductionMap.get(merch.Production_Request__c);
            }
        }
        if(lstmerch.size()>0){
            update lstmerch;
        }
    }
}

 
This was selected as the best answer
DexPaPaDexPaPa
Thank you very for helping!