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
Kris WebsterKris Webster 

Trigger not working.. PLEASE HELP

BREAKDOWN 
I have a object called Project (pse__Proj__c) that has a lookup field on it called campaign (Campaign__c). I also have another object called Expense Reports (pse__Expense_Report__c) that also has the same lookup field on it, buit with a different API name (Campaign_Expense__c). Basically what I need the code to do is that when a new expense report record is created I need the value that is found in the Campaign (Campaign__c) field (on the project object) to fill the campaign object on the expense report object with the same value. 

Here is the code I have, but it is not filling the field on the expense report object with the campaign. PLEASE HELP ! 
trigger Updatecampaign on pse__Expense_Report__c (before insert,before Update) {

// Local catches for bulkified data
    private map<Id, pse__Proj__c> projMap = new map<Id, pse__Proj__c>();      

   for (pse__Expense_Report__c er: Trigger.new) (

   		projectIds.add(er.Campaign_Expense__c);
   		}

   		for{pse__Proj__c pro: [select id, Campaign__c from pse__Proj__c where Campaign__c in :projectIds]}
   		{
   		projMap.put(pro.Campaign__c, projMap)
   		}

   			for (pse__Expense_Report__c er : Trigger.new)
   			{
   			er.Campaign_Expense__c=projMap.get(er.Campaign_Expense__c).Id;
   			
   			}
   		}

 
Rohit Kumar SainiRohit Kumar Saini
Hi Kris,

I believe pse__Proj__c and pse__Expense_Report__c  are related and have lookup relationship. Can you please confirm pse__Expense_Report__c  has a lookup field for object pse__Proj__c and mention API name of that field (Field on pse__Expense_Report__c object)?

If I understand your requirements correctly, below code should do it. Please update the API name of the field on this code on line #2.
 
trigger Updatecampaign on pse__Expense_Report__c (before insert,before Update) {    
    private map<string, pse__Proj__c> projMap = new map<string, pse__Proj__c>();     
    string API_NAME_OF_ProjectFieldONExpenseReport='';
    for (pse__Expense_Report__c er: Trigger.new){
        if(er.Campaign_Expense__c!=null && er.get(API_NAME_OF_ProjectFieldONExpenseReport)!=null){
            projMap.put(er.get(API_NAME_OF_ProjectFieldONExpenseReport), new pse__Proj__c(id=er.get(API_NAME_OF_ProjectFieldONExpenseReport), Campaign__c=er.Campaign_Expense__c));
        }
    }
    update projMap.values();
}

Please let me know for any questions. Thanks.