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
PRASHANT BHARTIPRASHANT BHARTI 

Illegal assignment from Integer to Id

trigger Count_Of_JobApp on job_application__c (after insert,after delete) 
{
    if((Trigger.isInsert && Trigger.isAfter) || (Trigger.isDelete && Trigger.isAfter))
    {
        Set<Id> parentIdsSet = new Set<Id>();
        List<Position__c> positionList = new List<Position__c>();
        if(Trigger.isInsert){
            for(job_application__c jobObj : Trigger.new){
                parentIdsSet.add(jobObj.Total_Job_Application__c);
            }
        }else if(Trigger.isDelete)  {
            for(job_application__c jobObj : Trigger.old){
                parentIdsSet.add(jobObj.Total_Job_Application__c);
            }   
        }
        
        System.debug('parentIdsSet-->'+parentIdsSet);
        if(parentIdsSet.size() > 0){
            positionList = [SELECT Id,
                            Name,
                            Count_Of_Job_App__c,
                            (SELECT Name
                             FROM job_applications__r)
                            FROM Position__c
                            WHERE Id IN : parentIdsSet];
        }
        System.debug('positionList-->'+positionList);
        if(positionList.size() > 0){
            for(Position__c postionObj : positionList){
                postionObj.Count_Of_Job_App__c = postionObj.job_applications__r.size();//Error on this line.
            }
            update positionList;
        }
    }
}
Somya TiwariSomya Tiwari
Hello,

As i was able to understand your issue, you were having problem in the 9th and 13th line from the top. You have used Set of ID type and assigning a field which most probably is a Numeric field of type Integer. Hence i will suggest you to look below and find the correct solution.
trigger Count_Of_JobApp on job_application__c (after insert,after delete) 
{
    if((Trigger.isInsert && Trigger.isAfter) || (Trigger.isDelete && Trigger.isAfter))
    {
        Set<Integer> parentIdsSet = new Set<Integer>();
        List<Position__c> positionList = new List<Position__c>();
        if(Trigger.isInsert){
            for(job_application__c jobObj : Trigger.new){
                parentIdsSet.add(jobObj.Total_Job_Application__c);
            }
        }else if(Trigger.isDelete)  {
            for(job_application__c jobObj : Trigger.old){
                parentIdsSet.add(jobObj.Total_Job_Application__c);
            }   
        }
        
        System.debug('parentIdsSet-->'+parentIdsSet);
        if(parentIdsSet.size() > 0){
            positionList = [SELECT Id,
                            Name,
                            Count_Of_Job_App__c,
                            (SELECT Name
                             FROM job_applications__r)
                            FROM Position__c
                            WHERE Id IN : parentIdsSet];
        }
        System.debug('positionList-->'+positionList);
        if(positionList.size() > 0){
            for(Position__c postionObj : positionList){
                postionObj.Count_Of_Job_App__c = postionObj.job_applications__r.size();//Error on this line.
            }
            update positionList;
        }
    }
}

To Summerize i have simple changed the following line 
Set<ID> parentIDsSet = new Set<ID>();
to the following line
Set<Integer> parentIDsSet = new Set<Integer>();
Or you can simply change the assignment to following:
parentIdsSet.add(jobObj.ID);
Please mark the answer as Best Answer if it helped you :)
PRASHANT BHARTIPRASHANT BHARTI
Hi Somya,

Issue is on 30th line of the code.
Still the solution provided by you is not working.

Regards,
Prashant
Somya TiwariSomya Tiwari
Hi Prashant,

Kindly do the following changes, it will start working
postionObj.Count_Of_Job_App__c =String.ValueOf(postionObj.job_applications__r.size());
Also if it doesn't work, then please let me know the type of Count_Of_Job_App__c
Mark the Solution as best answer if it helped you in any case :)
Deepali KulshresthaDeepali Kulshrestha
Hi PRASHANT,
Greetings to you!

- I have also added the code to check if postionObj.job_applications__r.size()=0.Now it is dynamic.Please use the below code : -
trigger Count_Of_JobApp on job_application__c (after insert,after delete) 
    {
        if((Trigger.isInsert && Trigger.isAfter) || (Trigger.isDelete && Trigger.isAfter))
        {
            Set<Integer> parentIdsSet = new Set<Integer>();
            List<Position__c> positionList = new List<Position__c>();
            if(Trigger.isInsert){
                for(job_application__c jobObj : Trigger.new){
                    parentIdsSet.add(jobObj.Total_Job_Application__c);
                }
            }else if(Trigger.isDelete)  {
                for(job_application__c jobObj : Trigger.old){
                    parentIdsSet.add(jobObj.Total_Job_Application__c);
                }   
            }
            
            System.debug('parentIdsSet-->'+parentIdsSet);
            if(parentIdsSet.size() > 0){
                positionList = [SELECT Id,
                                Name,
                                Count_Of_Job_App__c,
                                (SELECT Name
                                 FROM job_applications__r)
                                FROM Position__c
                                WHERE Id IN : parentIdsSet];
            }
            System.debug('positionList-->'+positionList);
            if(positionList.size() > 0){
                for(Position__c postionObj : positionList){
                    String count = String.valueof(postionObj.job_applications__r.size());
                    if(count != null){
                        postionObj.Count_Of_Job_App__c = count;//Error on this line.
                    }else{
                        count = '0';
                        postionObj.Count_Of_Job_App__c = count;
                    }
                }
                update positionList;
            }
        }
    }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
PRASHANT BHARTIPRASHANT BHARTI
Hi Somya,

I am getting an erro while inserting a record.
"Apex trigger Count_Of_JobApp caused an unexpected exception, contact your administrator: Count_Of_JobApp: execution of AfterInsert caused by: System.StringException: Invalid id: 1: Trigger.Count_Of_JobApp: line 30, column 1"

Thanks & Regards,
Prashant
Somya TiwariSomya Tiwari
Hi Prashant,

From the Exception i assume that Count_Of_JobApp is a relationship field !! Can you confirm the same?
PRASHANT BHARTIPRASHANT BHARTI
Hi Somya,
yes Count_Of_JobApp is a relationship field.
 
Somya TiwariSomya Tiwari

Hi Prashant, 

I afaik that you have used wrong field type. Here you must use Integer type of field. Kindly change the same, and you will be good to go. 

But why will you need to have a Count_Of_JobApp as a relationship field. By the name i can assume that with this you need to have a count. Hence change the Field Type. You can also use RollUp Field where you can count the number of JobApplications easily via a simple Count feature. Kindly have a look at the screenshot in case you need help on the same.
User-added image

Regards,
Somya Tiwari