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
tulasiram chtulasiram 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;
        }
    }
}
Best Answer chosen by tulasiram ch
sfdcMonkey.comsfdcMonkey.com
hi Tulaslram
add
jobAppIds.add(jobs.Position__c);
in the jobAppIds insteadof    jobAppIds.add(jobs.Position__r.id); 
use below 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__c);  
            }
        }
        if(trigger.isDelete){
            for(Job_Application__c jobs:trigger.old){
                jobAppIds.add(jobs.Position__c);
            }
            
        }
    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;
        }
    }
}

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

radha gorintaradha gorinta
This can be done using' roll up summary'
tulasiram chtulasiram ch
Radha i thought that but ,,,trying this
sfdcMonkey.comsfdcMonkey.com
hi Tulaslram
add
jobAppIds.add(jobs.Position__c);
in the jobAppIds insteadof    jobAppIds.add(jobs.Position__r.id); 
use below 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__c);  
            }
        }
        if(trigger.isDelete){
            for(Job_Application__c jobs:trigger.old){
                jobAppIds.add(jobs.Position__c);
            }
            
        }
    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;
        }
    }
}

let me inform if it helps you And mark it best answer if it helps you so it make proper solution for others
thanks

 
This was selected as the best answer
tulasiram chtulasiram ch
Hehe its working....Thank you piyush