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.
1 Answer
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" ILR__c
record.
-
Hi peter,yes I got it corrected.any idea on how to apply the validation logic mentioned in the problem description?– user4644Commented Dec 25, 2013 at 16:36
-
Yes. You'll need to query for an existing record and if you find one handle it by displaying the appropriate error message, using ApexPages.addMessage. Also make sure not to send a redirect, but instead just return null. Take a look at the SOSL/SOQL Developer's guide for more information on how to write SOQL queries. You'll need to determine what it is that you need to put in the WHERE clause to get the correct record. Commented Dec 26, 2013 at 13:34