0

Just curious to know about the clone button functionality. When I create a record and click on clone button , its allowing me to change the master details lookup record. But When I have writtern custom code, and use my clone button, its not allowing me to do that( That master detail field coming as readable only). Below is my custom clone button code. My main object is Custom__c and other object is child__c

Id customId = ApexPages.currentPage().getParameters().get('id');
String RecordTypeStr = ApexPages.currentPage().getParameters().get('RecordType');

system.debug('RecordTypeStr>>'+RecordTypeStr);

RecordType recType = [SELECT Id,Name FROM RecordType WHERE SObjectType ='Custom__c' and Name=:RecordTypeStr];


List<Custom__c> cusObjList = Database.query('SELECT '+Metadata.getCommaSeparatedFieldNames(new Custom__c()) +
                            '  FROM Custom__c WHERE id=\''+customId+'\'');

Custom__c cloneRec = cusObjList[0].clone(false,false);
cloneRec.RecordTypeId = recType.Id;

insert cloneRec;

return new PageReference('/' + cloneRec.Id  +'/e?retURL=%2F' +cloneRec.Id);
2

By default, a Master-detail field is write-once (on create) and then read-only.

Because your method is creating the cloned record in database then redirecting the user to an edit page, the user is prevented from specifying a new M-D value.

The easiest way to enable this would be to tick the "Allow reparenting" option - which is found in setup by editing the field in the object.

enter image description here

Second option would be find a way to specify the new M-D value before you call your code - and set the new value when the record is inserted...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.