You need to sign in to do that
Don't have an account?
tulasiram ch
tell me what mistakes i have done....
My requirement is incremnet of "No_of_JobApps__c" when i inserted/Undeleted Job_Application__c related to a Position__c and decrement of No_of_JobApps__c when i deleted Job_Application__c related to a Position__c...
1. No_of_JobApps__c is a number data type on Position__c custom object
2.Job_Application__c is related with Position__c(Master object). code is fine but i am not getting No_of_JobApps__c field updated.
Trigger code:
trigger countJobApp on Job_Application__c (after insert, after delete, after undelete) {
if(trigger.isAfter){
set<id> jobAppIds= new set<id>();
if(trigger.isInsert || trigger.isUnDelete){
for(Job_Application__c jobs:trigger.new){
jobAppIds.add(jobs.Position__r.id);
}
}
if(trigger.isDelete){
for(Job_Application__c jobs:trigger.old){
jobAppIds.add(jobs.Position__r.id);
}
}
list<Position__c> pos= new list<Position__c>();
list<Position__c> jobsforPos=[SELECT No_of_JobApps__c, id, (SELECT id FROM Job_Applications__r) FROM Position__c where ID IN :jobAppIds];
for(Position__c jobPos:jobsforPos){
integer count=0;
for(Job_Application__c japps:jobPos.Job_Applications__r){
count++;
}
if(jobPos.No_of_JobApps__c !=count){
jobPos.No_of_JobApps__c = count;
pos.add(jobPos);
}
}
if(pos.size()>0){
update pos;
}
}
}
1. No_of_JobApps__c is a number data type on Position__c custom object
2.Job_Application__c is related with Position__c(Master object). code is fine but i am not getting No_of_JobApps__c field updated.
Trigger code:
trigger countJobApp on Job_Application__c (after insert, after delete, after undelete) {
if(trigger.isAfter){
set<id> jobAppIds= new set<id>();
if(trigger.isInsert || trigger.isUnDelete){
for(Job_Application__c jobs:trigger.new){
jobAppIds.add(jobs.Position__r.id);
}
}
if(trigger.isDelete){
for(Job_Application__c jobs:trigger.old){
jobAppIds.add(jobs.Position__r.id);
}
}
list<Position__c> pos= new list<Position__c>();
list<Position__c> jobsforPos=[SELECT No_of_JobApps__c, id, (SELECT id FROM Job_Applications__r) FROM Position__c where ID IN :jobAppIds];
for(Position__c jobPos:jobsforPos){
integer count=0;
for(Job_Application__c japps:jobPos.Job_Applications__r){
count++;
}
if(jobPos.No_of_JobApps__c !=count){
jobPos.No_of_JobApps__c = count;
pos.add(jobPos);
}
}
if(pos.size()>0){
update pos;
}
}
}
add
jobAppIds.add(jobs.Position__c);
in the jobAppIds insteadof jobAppIds.add(jobs.Position__r.id);
use below code
let me inform if it helps you And mark it best answer if it helps you so it make proper solution for others
thanks
All Answers
add
jobAppIds.add(jobs.Position__c);
in the jobAppIds insteadof jobAppIds.add(jobs.Position__r.id);
use below code
let me inform if it helps you And mark it best answer if it helps you so it make proper solution for others
thanks