illegal assignment from object to id Error?
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
Why do I get this error? How do I fix it?
1 Answer
1
So the field juncRec.PDCN__c
is a Lookup field, (Consider it as a text/ID field)
juncRec.PDCN__c
where as in your loop pdcn
is an object/record.(Not a text field)
pdcn
In your code
juncRec.PDCN__c = pdcn;
What you are trying to do is assign an Object to a Text/Id field and hence you get an error. You can solve this by using id juncRec.PDCN__c = pdcn.Id;
juncRec.PDCN__c = pdcn.Id;
for( PDCN__c pdcn : lstAddId )
system.debug('Inside For Loop-->'+pdcn);
PDCNGrpJunc__c juncRec = new PDCNGrpJunc__c();
***juncRec.PDCN__c = pdcn.Id;***
juncRec.PDCN_Group__c = groupId;
lstJunc.add(juncRec);
Thanks for contributing an answer to Salesforce Stack Exchange!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.