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
Patricia LimPatricia Lim 

duplicate value found: <unknown> duplicates value on record with id: <unknown> - When assigning Id

Hello all. I am trying to upload an attachment through Visualforce so that end-users can upload documents and it will be related to the record they just created.  However when I assign the parentId of the attachment to the variable in my controller, it returns the following error:
duplicate value found: <unknown> duplicates value on record with id: <unknown>

Here is the code
Apex
/*
    1) Initalize objects
    2) Initalize lists
    3) Empty constructor 
    4) Add row to recipient list
    5) Add row to scholarship list
    6) Insert all records from both lists
    */

    //1) Initalize objects

    public without sharing class finalExperiment {
        
        //Recipient
        public Recipient__c rec {
            get {
                if(rec == null)
                    rec = new Recipient__c();
                return rec;
            }
            set;
        }

        //Scholarship Award
        public Scholarship_Award__c sch {
            get {
                if(sch == null)
                    sch = new Scholarship_Award__c();
                return sch;
            }
            set;
        }

        //Recipient List
        public List<Recipient__c> recList {
            get {
                if(recList == null)
                    recList = new List<Recipient__c>();
                return recList;
            }
            set;
        }

        //Scholarship List
        public List<Scholarship_Award__c> schList {
            get {
                if(schList == null)
                    schList = new List<Scholarship_Award__c>();
                return schList;
            }
            set;
        }

        //Attachment

        public Attachment myDocument {
        get {
            if(myDocument == null)
                myDocument = new Attachment();
            return myDocument;
            }
            set;
        }
        
        //Empty constructor
        public finalExperiment(){

        }

        //Add row method for both lists
        public PageReference add_record(){
            //Recipient fields
            Recipient__c anotherRecipient = new Recipient__c();
            anotherRecipient.Name = rec.Name;
            anotherRecipient.Last_Name__c = rec.Last_Name__c;
            anotherRecipient.School__c = rec.School__c;
            anotherRecipient.Specialty__c = rec.Specialty__c;
         	
            upsert anotherRecipient;
            
            //Insert any documents into recipient
            myDocument.parentId = anotherRecipient.Id;	

            //Scholarship fields
            Scholarship_Award__c anotherScholarship = new Scholarship_Award__c();
            anotherScholarship.Award__c = sch.Award__c;
            anotherScholarship.Year__c = sch.Year__c;
            anotherScholarship.School__c = sch.School__c;
            anotherScholarship.Recipient__c = anotherRecipient.Id;
            
            upsert anotherScholarship;

            //Add to lists
            recList.add(anotherRecipient);
            schList.add(anotherScholarship);
            
            return null;
        }

The VF is working fine - it is the assignment that is throwing an error. 
Thank you
ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

I can see that you are trying to insert the records on recipient and scholorship Objects.Seems like you have duplicate rule enabled on one of these Object which is not allowing you to create the duplicate records.

I would suggest you to enable the debug logs while executing this to check which record is causing the issue.

Reference:https://help.salesforce.com/apex/HTViewHelpDoc?id=code_add_users_debug_log.htm&language=en_us

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri