You need to sign in to do that
Don't have an account?
Open edit fields for triggers but close for users
Have a task with triggers.
I'm have a custom object with fields, one of trigger method change fields value on values at child lookup object, sum some score.
I need to close edit for this fields, but with profiles i cant do this. When I write in trigger i can close this fields, but trigger also can't edit them.
How I can write trigger or can i try another option for close edit this fields?
Here part of trigger and object with fields
private static final String STATUS_FOR_CHECK = 'Closed - Hired'; public static void getJobApplicationsForChangeStatusAfterUpdate(List<Job_Application__c> newJobApplications, Map<Id, Job_Application__c> oldJobApplications) { Set<Id> positionsToClose = new Set<Id>(); for (Job_Application__c newJobApp : newJobApplications) { Job_Application__c oldJobApp = oldJobApplications.get(newJobApp.Id); if (newJobApp.Overall_Average_Reviwes_Score__c != oldJobApp.Overall_Average_Reviwes_Score__c || newJobApp.Overal_Reviews__c != oldJobApp.Overal_Reviews__c) { newJobApp.addError('Fields with Reviews scores can not be changed'); } else { if (newJobApp.Status__c == STATUS_FOR_CHECK && oldJobApp != null && oldJobApp.Status__c != STATUS_FOR_CHECK) { positionsToClose.add(newJobApp.Position__c); } } } List<Position__c> positionsToUpdate = [SELECT Id, Status__c FROM Position__c WHERE Id IN :positionsToClose]; for (Position__c pos : positionsToUpdate) { pos.Status__c = STATUS_FOR_CHECK; } update positionsToUpdate; }
in this part I can check this fields
if (newJobApp.Overall_Average_Reviwes_Score__c != oldJobApp.Overall_Average_Reviwes_Score__c || newJobApp.Overal_Reviews__c != oldJobApp.Overal_Reviews__c) { newJobApp.addError('Fields with Reviews scores can not be changed'); }
Sorry for code higher
private static final String STATUS_FOR_CHECK = 'Closed - Hired'
public static void getJobApplicationsForChangeStatusAfterUpdate(List<Job_Application__c> newJobApplications, Map<Id, Job_Application__c> oldJobApplications) {
Set<Id> positionsToClose = new Set<Id>();
for (Job_Application__c newJobApp : newJobApplications) {
Job_Application__c oldJobApp = oldJobApplications.get(newJobApp.Id);
if (newJobApp.Overall_Average_Reviwes_Score__c != oldJobApp.Overall_Average_Reviwes_Score__c || newJobApp.Overal_Reviews__c != oldJobApp.Overal_Reviews__c) {
newJobApp.addError('Fields with Reviews scores can not be changed');
} else {
if (newJobApp.Status__c == STATUS_FOR_CHECK && oldJobApp != null && oldJobApp.Status__c != STATUS_FOR_CHECK){
positionsToClose.add(newJobApp.Position__c);
}
}
}
List<Position__c> positionsToUpdate = [SELECT Id, Status__c FROM Position__c WHERE Id IN :positionsToClose];
for (Position__c pos : positionsToUpdate) {
pos.Status__c = STATUS_FOR_CHECK;
}
update positionsToUpdate;
}
I am trying to understand what you need.
The way I read your trigger is that you are preventing the Status value from being changed on the Job Application Object and Position Object if the Job Application either has an Overall Average Review Score or Overall Reviews
Do you want Status to be change by trigger but not by user?
Please pardon me if I misunderstood anything here, but please provide some clarity.
I made a small change just before the SOQL on the Positions Object to avoid null check in the SOQL. This will little optimize your code.
Regards,
Abdul Aziz Khatri