Skip to main content
1 of 2

illegal assignment from object to id Error?

I have written following code to insert records on junction object.

public static List<PDCN__c> addSelectedPDCNs(List<String> selectedRecords, String groupId) {
        system.debug('Entering addSelectedPDCNs');
        system.debug('Selected Records are-->'+selectedRecords);
        system.debug('Group Id is-->'+groupId);
        
        List < PDCN__c > lstAddId = [SELECT Id from PDCN__c where Name IN: selectedRecords];
        system.debug('Record Ids Are-->'+lstAddId);
        
        List<PDCNGrpJunc__c> lstJunc = new List<PDCNGrpJunc__c>();
        
        for( PDCN__c  pdcn : lstAddId ) {
            system.debug('Inside For Loop-->'+pdcn);
            PDCNGrpJunc__c juncRec = new PDCNGrpJunc__c();
            juncRec.PDCN__c = pdcn;
            juncRec.PDCN_Group__c = groupId;
            lstJunc.add(juncRec);
        }
        //insert lstJunc;
        try{
            system.debug('Trying Insert');
            system.debug('');
            insert lstJunc;
                }
         catch(Exception e)
        {
            System.debug('Exception Occured='+e.getMessage());
        }
        
        return lstAddId;
    }

I am getting error in juncRec.PDCN__c = pdcn; line as :-

Illegal assignment from PDCN__c to Id

I am not sure what supposed to be problem or solution.. Thanks in Advance!!