I have wrote a custom logic to clone a parent object on clicking CLONE button on VF page. Here the expected result is when I am clicking clone button for a record named "A", it should create a record for user with name "Copy of "+A with current month & year. One restriction is there suppose if any record is created or cloned with current month & year's date for a particular user , user should not be able to clone that record for that month. Next month onwards he should be able to clone that record. But my code is not giving the desired result. after cloning a record named "A", its reqturing the result as "Copy of null". Kindly suggest. thanks.
Directly after you retrieve the record into the variable you overwrite it with a new instance, so the value is null. Your query:
ilr = [ SELECT Id,OwnerId,CurrencyIsoCode,Name,dupCheck__c,PFE_Current_Attainment__c,Description__c,End_Date__c, ILR_Name__c,PFE_ILR_Month__c,PFE_Month_Number__c,PFE_Number_of_Recipients__c, PFE_Number_of_Samples_Dropped__c,Sales_Rep__c,Start_Date__c,PFE_ILR_Status__c,ParentMonth__c,ParentYear__c,PFE_ILR_Year__c FROM ILR__c WHERE Id = :presentId LIMIT 1 ];
Followed directly by this:
ilr= new ILR__c();
The value is null because you are not using the record retrieved from the SOQL query. Remove the
ilr = new ILR__C(); line of code, so that you don't overwrite the result of the SOQL query in the
ilr variable with a new "blank"