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
sfadm sfadmsfadm sfadm 

Trigger not updating a record field

In my code
trigger ContactRole on Application__c (before update) {

// Get the time between the last aTeam and bTeam
                DateTime lastUWCreated = lastCCUW.Created_Date__c;
                System.debug('lastUWCreated ' + lastUWCreated);
                DateTime lastAmTeamCreated = lastCCRiskAndChecked.Created_Date__c;
                System.debug('lastAmTeamCreated ' + lastAmTeamCreated);
                
                Double workingHours = calculateWorkingHours(lastUWCreated, lastAmTeamCreated);
                System.debug('workingHours ' + workingHours);
                Id ccAppId = lastAmTeamCreated.Id;
                
                List<Application__c> ccAppList = [SELECT CalculatedWorkingHours__c from Application__c where id = :ccAppId];
                Application__c appRecord = ccAppList.get(0);
                appRecord.CalculatedWorkingHours__c = workingHours;
                System.debug('appRecord.CalculatedWorkingHours__c ' + appRecord.CalculatedWorkingHours__c);
}

I'm using the before update trigger in order to populate the calculated working hours into a custom field called "CalculatedWorkingHours__c".

I can see in the debug log that the  "CalculatedWorkingHours__c" field is updated
 
USER_DEBUG [184]|DEBUG|workingHours 17.5

USER_DEBUG [194]|DEBUG|ccAppRecord.CalculatedWorkingHours__c 17.5
but on a record level the field remains blank

Please advise what can be the issue and why the field is not populated in the record?
Best Answer chosen by sfadm sfadm
Waqar Hussain SFWaqar Hussain SF
trigger ContactRole on Application__c (before update) {

// Get the time between the last aTeam and bTeam
                DateTime lastUWCreated = lastCCUW.Created_Date__c;
                System.debug('lastUWCreated ' + lastUWCreated);
                DateTime lastAmTeamCreated = lastCCRiskAndChecked.Created_Date__c;
                System.debug('lastAmTeamCreated ' + lastAmTeamCreated);
                
                Double workingHours = calculateWorkingHours(lastUWCreated, lastAmTeamCreated);
                System.debug('workingHours ' + workingHours);
                
                for(Application__c appRecord : trigger.new){
					appRecord.CalculatedWorkingHours__c = workingHours;
				}
}

try above one and make sure that if any class with name Application__c is present in your system, delete/rename it and then try.

Thanks

All Answers

Waqar Hussain SFWaqar Hussain SF
trigger ContactRole on Application__c (before update) {

// Get the time between the last aTeam and bTeam
                DateTime lastUWCreated = lastCCUW.Created_Date__c;
                System.debug('lastUWCreated ' + lastUWCreated);
                DateTime lastAmTeamCreated = lastCCRiskAndChecked.Created_Date__c;
                System.debug('lastAmTeamCreated ' + lastAmTeamCreated);
                
                Double workingHours = calculateWorkingHours(lastUWCreated, lastAmTeamCreated);
                System.debug('workingHours ' + workingHours);
                
                for(Application__c appRecord: trigger.new){
					appRecord.CalculatedWorkingHours__c = workingHours;
					System.debug('appRecord.CalculatedWorkingHours__c ' + appRecord.CalculatedWorkingHours__c);
				}
}

Use the above code snippet and let me know, if you have any question. 

Thanks
Nikhil Verma 6Nikhil Verma 6
The record might not be getting updated since there is no update statement in the trigger. Since the records whose fields you are updating have been queried from the database, an update command is neccessary.

Hope this helps.
sfadm sfadmsfadm sfadm
@Waqar Hussain
Hi Waqar,

I followed your suggestion and used your implementation however on line 12 I recieve the following problem:
Invalid loop variable type expected SObject was Application__c

User-added image

Any ideas how to avoid such problem?
 
Waqar Hussain SFWaqar Hussain SF
trigger ContactRole on Application__c (before update) {

// Get the time between the last aTeam and bTeam
                DateTime lastUWCreated = lastCCUW.Created_Date__c;
                System.debug('lastUWCreated ' + lastUWCreated);
                DateTime lastAmTeamCreated = lastCCRiskAndChecked.Created_Date__c;
                System.debug('lastAmTeamCreated ' + lastAmTeamCreated);
                
                Double workingHours = calculateWorkingHours(lastUWCreated, lastAmTeamCreated);
                System.debug('workingHours ' + workingHours);
                
                for(Application__c appRecord : trigger.new){
					appRecord.CalculatedWorkingHours__c = workingHours;
				}
}

try above one and make sure that if any class with name Application__c is present in your system, delete/rename it and then try.

Thanks
This was selected as the best answer