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
Erik LarameeErik Laramee 

invalid field for SObject in trigger

I am trying to modify an existing trigger to change how a field is populated. The field is called Project PM on the Con Req Group object. Project PM is populated from the Owner field (lookup to User) on the Project object. I need to modify it so that Project PM is populated from the Project Manager field (also lookup to User) on the Site object. I believe this is done by line 63 of the code. I tried modifying it but am getting an invalid field message on line 63 - crg.Project_PM__c = pMap.get(crg.Project__c).OwnerId; . Can anyone assist in this?

trigger ConReqGroup on Con_Req_Group__c (after update, before delete, before insert, before update) {
    
    /*
     *    Before delete
     *        - only allow delete if the record is not locked
     *
     *    Before insert and before update
     *        - set the Project PM field to the OwnerId field of the associated Project
     *        - set the Ship To address fields from the related Site record
     *        - if before update, make sure the Sales Order field is not changed
     *
     *    After update
     *        - copy the "group" fields to the associated Sales Order Line records
     *
     */
    
    if (trigger.isBefore) {
        Set<Id> pIds = new Set<Id>();
        Set<Id> sIds = new Set<Id>();
        if (trigger.isDelete) {
            for (Con_Req_Group__c crg :trigger.old) {
                if (crg.IsLocked__c) {
                    crg.addError('Record is locked - cannot delete a locked record.');
                }
            }
        } else {
            for (Con_Req_Group__c crg :trigger.new) {
                if (trigger.isUpdate) {
                    // Don't allow a change to the Sales Order field
                    if (crg.Sales_Order__c != trigger.oldMap.get(crg.Id).Sales_Order__c) {
                        crg.Sales_Order__c.addError('Field is not writeable.');
                    }
                    
                    if (crg.Project__c != null && crg.Project__c != trigger.oldMap.get(crg.Id).Project__c) {
                        pIds.add(crg.Project__c);
                    }
                    
                    if (crg.Ship_To_Site__c != null && crg.Ship_To_Site__c != trigger.oldMap.get(crg.Id).Ship_To_Site__c) {
                        sIds.add(crg.Ship_To_Site__c);
                    }
                } else {
                    if (crg.Project__c != null) {
                        pIds.add(crg.Project__c);
                    }
                    if (crg.Ship_To_Site__c != null){
                        sIds.add(crg.Ship_To_Site__c);
                    }
                }
            }
            
            Map<Id, AcctSeed__Project__c> pMap = null;
            if (!pIds.isEmpty()) {
                pMap = new Map<Id, AcctSeed__Project__c>([select Id, OwnerId from AcctSeed__Project__c where Id in :pIds]);
            }
            
            Map<Id, Site__c> sMap = null;
            if (!sIds.isEmpty()) {
                sMap = new Map<Id, Site__c>([select Id, Site_Address__c, Site_City__c, Site_State__c, Site_Zip__c from Site__c where Id in :sIds]);
            }
            
            for (Con_Req_Group__c crg :trigger.new) {
                if (pMap != null && pMap.containsKey(crg.Project__c)) {
                    crg.Project_PM__c = pMap.get(crg.Project__c).OwnerId;
                }
                
                if (sMap != null && sMap.containsKey(crg.Ship_To_Site__c)) {
                    crg.Ship_To_Street__c = sMap.get(crg.Ship_To_Site__c).Site_Address__c;
                    crg.Ship_To_City__c = sMap.get(crg.Ship_To_Site__c).Site_City__c;
                    crg.Ship_To_State__c = sMap.get(crg.Ship_To_Site__c).Site_State__c;
                    crg.Ship_To_Zip__c = sMap.get(crg.Ship_To_Site__c).Site_Zip__c;
                }
            }
        }
    }
    
    if (trigger.isAfter) {
        List<Id> crgIds = new List<Id>();
        for (Con_Req_Group__c crg :trigger.new) {
            if ( (crg.Project__c != trigger.oldMap.get(crg.Id).Project__c) || 
                 (crg.Project_PM__c != trigger.oldMap.get(crg.Id).Project_PM__c) || 
                 (crg.Ship_to_Account__c != trigger.oldMap.get(crg.Id).Ship_to_Account__c) || 
                 (crg.Status__c != trigger.oldMap.get(crg.Id).Status__c) || 
                 (crg.Vendor__c != trigger.oldMap.get(crg.Id).Vendor__c) || 
                 (crg.Vendor_Contact__c != trigger.oldMap.get(crg.Id).Vendor_Contact__c) || 
                 (crg.Requested_Delivery_Date__c != trigger.oldMap.get(crg.Id).Requested_Delivery_Date__c) ) {
                crgIds.add(crg.Id);
            }
        }
        
        if (!crgIds.isEmpty()) {
            List<AcctSeedERP__Sales_Order_Line__c> solList = new List<AcctSeedERP__Sales_Order_Line__c> ();
            solList = [select Id, AcctSeedERP__Project__c, Project_PM__c, AcctSeedERP__Sales_Order__c, 
                        Ship_to_Account__c, Status__c, Vendor__c, Vendor_Contact__c, 
                        Requested_Delivery_Date__c, Con_Req_Group__c 
                        from AcctSeedERP__Sales_Order_Line__c where Con_Req_Group__c in :crgIds];
            
            for (AcctSeedERP__Sales_Order_Line__c sol :solList) {
                sol.AcctSeedERP__Project__c = trigger.newMap.get(sol.Con_Req_Group__c).Project__c;
                sol.Project_PM__c = trigger.newMap.get(sol.Con_Req_Group__c).Project_PM__c;
                sol.Ship_to_Account__c = trigger.newMap.get(sol.Con_Req_Group__c).Ship_to_Account__c;
                sol.Status__c = trigger.newMap.get(sol.Con_Req_Group__c).Status__c;
                sol.Vendor__c = trigger.newMap.get(sol.Con_Req_Group__c).Vendor__c;
                sol.Vendor_Contact__c = trigger.newMap.get(sol.Con_Req_Group__c).Vendor_Contact__c;
                sol.Requested_Delivery_Date__c = trigger.newMap.get(sol.Con_Req_Group__c).Requested_Delivery_Date__c;
            }
            
            if (!solList.isEmpty()) {
                update solList;
            }
        }
    }
}
James LoghryJames Loghry
Is this a compile error or a run time error?  Sounds like it's a compile time error.  Please double check your Con Req Group object and make sure it contains a field with an API name of Project_PM__c
Erik LarameeErik Laramee
The trigger is working correctly in production, its an older trigger that's been in use for quite some time. The Project_PM__c field does exist on the Con Req Group object. Project_PM__c is populated by the Owner field (standard field) on the Project object. I need to change the trigger so that Project_PM__c takes it's value from the Project_Manager__c field on the Site object.

I see the error in developer console while editing the trigger.