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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế