You need to sign in to do that
Don't have an account?
Steve Chadbourn
SOQL for cloning an object
I'm passed an object id and want to clone it. I'm guessing I need to write a soql query to get an instance of the object then clone it but I don't want to hard code all the field names as they may change. Is there a way to do a select * or a way of getting the object that doesn't involve hard coding?
Function: clone(Boolean opt_preserve_id, Boolean opt_IsDeepClone)
ex.
Thanks. That's exactly what I want to do.
The problem is that where you typed select... I have to type the name of all the fields I want to clone.There could be a lot in Account. And If I add new ones in the future I will have to update the select statement.
I'm hoping there is a way to do select * or write some DML that can generate a select for me that includes all the fields.
Any ideas anyone?
I'm facing the same situation. Did you find a solution for this?
Schema.DescribeSObjectResult dsoResult = Account.sObjectType.getDescribe();
Schema.SObjectType. fieldMap = Schema.SObjectType.Account.fields.getMap());
List<String> fieldList = getFieldList(fieldMap,false);
String fieldNames = getFieldNamesFromList(fieldList);
String q = 'select '+fieldNames+' from Account where Id=\''+acctId+'\'';
//guery using dynamic query
Account acctObj = Database.query(q);
List<String> fieldList = new List<String>();
//build dynamic list of fieldnames
for (String fieldKey : fieldMap.keySet()) {
Schema.SObjectField fsObj = fieldMap.get(fieldKey);
Schema.DescribeFieldResult f = fsObj.getDescribe();
String fieldName = f.getName();
if (selectAllFields) {
fieldList.add(fieldName);
} else {
if (f.getName()=='Id' || f.isNameField() || f.isCustom()) {
fieldList.add(fieldName);
}
}
}
return fieldList;
}
String fieldNames = '';
for (String field : fieldList) {
if (fieldNames.length()>0) {
fieldNames += ',';
}
fieldNames += field;
}
if (fieldNames.length()>10000) {
throw new ApprovalException('Fieldnames length > 10000 characters');
}
return fieldNames;
}
Message Edited by gregs on 10-02-2008 07:19 AM
Message Edited by gregs on 10-02-2008 07:21 AM
Message Edited by gregs on 10-02-2008 07:22 AM
Message Edited by gregs on 10-02-2008 07:24 AM
Message Edited by gregs on 10-02-2008 07:24 AM
I figured out a different way to clone and object without going through all the fields: Using the ajax toolkit and a custom button, I force an update on a hidden control field on the record to clone. A trigger attached to it fires, and sends the full record object to an appex class where I just use the clone method. This method makes makes a full copy of the object without iterating through its fields. The new challenge here is to let the user/browser know the id of the new record and automatically navigate to it, so I'm working on it now.
Thanks
Raghu
Message Edited by Raghu_dev on 12-03-2008 02:15 PM