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
Galeeb SKGaleeb SK 

Update Count

Hi
I am tried to update the count of Records,But it's not working Properly.Tell me where can make the Mistake,
Scenario:
I have two objects Position,Job Application.JobApplication has the Status field it contans picklist values Open,New,Closed.when i create record in JobApplication,I need  only 'Closed ' records count that should be update in Position .
Apart from that  i have 5 Closed Records ,5 Open  Records then how can i display the count individually in same Trigger.
Tell me Please.
My Code Is:

trigger countupdate on Job_Application__c (before insert,before update){

set<ID>sid=new set<ID>();

list<Position__c> PosRec=new list<Position__c>();

for(Job_Application__c job :trigger.new)
{
//sid.add(job.Position__c);


list<Position__c>Pos =[SELECT ID,Count__c,(SELECT ID,Status__c FROM Job_Applications2__r) FROM  Position__c WHERE ID =:job.id];

for(Position__c P:Pos){

if(job.Status__c=='Closed')
{
P.Count__c=P.Job_Applications2__r.size();
}
else{
//P.Count__c=P.Job_Applications2__r.size();
P.Count__c=0;
}

PosRec.add(P);
}
}
update PosRec;
}
Thanks
Galeeb SK
v varaprasadv varaprasad
Hi ,

If master detail relation ship is there then no need to write Trigger.

Please try once rollup summary field.   like below...

User-added image

let me know this is correct or not..


Thanks
 
Galeeb SKGaleeb SK
Hi V Varaprasad,
Not configuration,we will achieve through coding.In Lookup also we can perform record count.but here we can display perticular picklist record count.
Thanks
Galeeb Sk
Paul S.Paul S.
Try this:
trigger countupdate on job_application__c (after insert, after update){

    set<id> sid = new set<id>();

    for(job_application__c job : trigger.new){
        sid.add(job.position__c);
    }

    list<position__c> pList = new list<position__c>();

    for(position__c pos : [select id, count__c, (select id, status__c from job_applications2__r where status__c = 'closed') from position__c where id IN : sid]){
        position__c pNew = new position__c(
            id = pos.id,
            count__c = pos.job_applications2__r.size()
        );
        pList.add(pNew);
    }

    update pList;

}
You should probably also set this up to run after delete or undelete.