How to clone object record?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
Hi i have code for cloning selected record, but i need to do it without defining each field one by one.
Here is an example with 4 fields but in complete, there are almost 40 fields.
@AuraEnabled
public static List<Claimed_Transaction__c> copyTransaction(String dataJson, String transId)
List<Claimed_Transaction__c> dataList = (List<Claimed_Transaction__c>) JSON.deserialize(dataJson, List<Claimed_Transaction__c>.class);
Claimed_Transaction__c sourceTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
Claimed_Transaction__c targetTransaction = new Claimed_Transaction__c();
targetTransaction.Name = sourceTransaction.Name;
targetTransaction.Adress = sourceTransaction.Adress;
targetTransaction.Phone = sourceTransaction.Phone ;
targetTransaction.Temporary_Transaction__c = true;
insert targetTransaction;
dataList.add(targetTransaction);
return dataList;
apex lightning
add a comment |Â
up vote
1
down vote
favorite
Hi i have code for cloning selected record, but i need to do it without defining each field one by one.
Here is an example with 4 fields but in complete, there are almost 40 fields.
@AuraEnabled
public static List<Claimed_Transaction__c> copyTransaction(String dataJson, String transId)
List<Claimed_Transaction__c> dataList = (List<Claimed_Transaction__c>) JSON.deserialize(dataJson, List<Claimed_Transaction__c>.class);
Claimed_Transaction__c sourceTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
Claimed_Transaction__c targetTransaction = new Claimed_Transaction__c();
targetTransaction.Name = sourceTransaction.Name;
targetTransaction.Adress = sourceTransaction.Adress;
targetTransaction.Phone = sourceTransaction.Phone ;
targetTransaction.Temporary_Transaction__c = true;
insert targetTransaction;
dataList.add(targetTransaction);
return dataList;
apex lightning
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Hi i have code for cloning selected record, but i need to do it without defining each field one by one.
Here is an example with 4 fields but in complete, there are almost 40 fields.
@AuraEnabled
public static List<Claimed_Transaction__c> copyTransaction(String dataJson, String transId)
List<Claimed_Transaction__c> dataList = (List<Claimed_Transaction__c>) JSON.deserialize(dataJson, List<Claimed_Transaction__c>.class);
Claimed_Transaction__c sourceTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
Claimed_Transaction__c targetTransaction = new Claimed_Transaction__c();
targetTransaction.Name = sourceTransaction.Name;
targetTransaction.Adress = sourceTransaction.Adress;
targetTransaction.Phone = sourceTransaction.Phone ;
targetTransaction.Temporary_Transaction__c = true;
insert targetTransaction;
dataList.add(targetTransaction);
return dataList;
apex lightning
Hi i have code for cloning selected record, but i need to do it without defining each field one by one.
Here is an example with 4 fields but in complete, there are almost 40 fields.
@AuraEnabled
public static List<Claimed_Transaction__c> copyTransaction(String dataJson, String transId)
List<Claimed_Transaction__c> dataList = (List<Claimed_Transaction__c>) JSON.deserialize(dataJson, List<Claimed_Transaction__c>.class);
Claimed_Transaction__c sourceTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
Claimed_Transaction__c targetTransaction = new Claimed_Transaction__c();
targetTransaction.Name = sourceTransaction.Name;
targetTransaction.Adress = sourceTransaction.Adress;
targetTransaction.Phone = sourceTransaction.Phone ;
targetTransaction.Temporary_Transaction__c = true;
insert targetTransaction;
dataList.add(targetTransaction);
return dataList;
apex lightning
asked Sep 6 at 6:43
David
244
244
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
5
down vote
accepted
From the documentation:
clone(preserveId, isDeepClone, preserveReadonlyTimestamps,
preserveAutonumber)
Creates a copy of the SObject record.
So you can just do this:
Claimed_Transaction__c targetTransaction = sourceTransaction.clone(false,true,true,false);
Please read the documentation for more info on tweaking the function using the parameters it takes.
add a comment |Â
up vote
2
down vote
You can use the clone method from SObject class.
Make sure your source object has all the fields queried. So if you have queried 4 fields then it will populate those fields only in the cloned object.
Claimed_Transaction__c claimedTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
Claimed_Transaction__c clonedClaimedTransaction = claimedTransaction.clone(false, false, false, false);
To query all the fields you can run a Dynamic SOQL Query for all fields
add a comment |Â
up vote
2
down vote
you can Use clone Function to copy the selected record.Just to make code easier and not to mention the fields in code,you can use fieldset and retrieve those field in code.
PFB the sample code.
List<String> fields = new List<String>();
FieldSet fieldsToClone = SObjectType.Claimed_Transaction__c.fieldSets.FieldsToClone;
for (FieldSetMember field : fieldsToClone.getFields())
fields.add(field.getFieldPath());
String soql = 'SELECT ' + String.join(fields, ',') +
' FROM Claimed_Transaction__c WHERE Id = '' + String.escapeSingleQuotes(transId) + ''';
return Database.query(soql).Clone(...);
This is sample code.please change it according to your functionality.
New contributor
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
From the documentation:
clone(preserveId, isDeepClone, preserveReadonlyTimestamps,
preserveAutonumber)
Creates a copy of the SObject record.
So you can just do this:
Claimed_Transaction__c targetTransaction = sourceTransaction.clone(false,true,true,false);
Please read the documentation for more info on tweaking the function using the parameters it takes.
add a comment |Â
up vote
5
down vote
accepted
From the documentation:
clone(preserveId, isDeepClone, preserveReadonlyTimestamps,
preserveAutonumber)
Creates a copy of the SObject record.
So you can just do this:
Claimed_Transaction__c targetTransaction = sourceTransaction.clone(false,true,true,false);
Please read the documentation for more info on tweaking the function using the parameters it takes.
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
From the documentation:
clone(preserveId, isDeepClone, preserveReadonlyTimestamps,
preserveAutonumber)
Creates a copy of the SObject record.
So you can just do this:
Claimed_Transaction__c targetTransaction = sourceTransaction.clone(false,true,true,false);
Please read the documentation for more info on tweaking the function using the parameters it takes.
From the documentation:
clone(preserveId, isDeepClone, preserveReadonlyTimestamps,
preserveAutonumber)
Creates a copy of the SObject record.
So you can just do this:
Claimed_Transaction__c targetTransaction = sourceTransaction.clone(false,true,true,false);
Please read the documentation for more info on tweaking the function using the parameters it takes.
answered Sep 6 at 6:49
Santanu Halder
4,6671829
4,6671829
add a comment |Â
add a comment |Â
up vote
2
down vote
You can use the clone method from SObject class.
Make sure your source object has all the fields queried. So if you have queried 4 fields then it will populate those fields only in the cloned object.
Claimed_Transaction__c claimedTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
Claimed_Transaction__c clonedClaimedTransaction = claimedTransaction.clone(false, false, false, false);
To query all the fields you can run a Dynamic SOQL Query for all fields
add a comment |Â
up vote
2
down vote
You can use the clone method from SObject class.
Make sure your source object has all the fields queried. So if you have queried 4 fields then it will populate those fields only in the cloned object.
Claimed_Transaction__c claimedTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
Claimed_Transaction__c clonedClaimedTransaction = claimedTransaction.clone(false, false, false, false);
To query all the fields you can run a Dynamic SOQL Query for all fields
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can use the clone method from SObject class.
Make sure your source object has all the fields queried. So if you have queried 4 fields then it will populate those fields only in the cloned object.
Claimed_Transaction__c claimedTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
Claimed_Transaction__c clonedClaimedTransaction = claimedTransaction.clone(false, false, false, false);
To query all the fields you can run a Dynamic SOQL Query for all fields
You can use the clone method from SObject class.
Make sure your source object has all the fields queried. So if you have queried 4 fields then it will populate those fields only in the cloned object.
Claimed_Transaction__c claimedTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
Claimed_Transaction__c clonedClaimedTransaction = claimedTransaction.clone(false, false, false, false);
To query all the fields you can run a Dynamic SOQL Query for all fields
answered Sep 6 at 7:36
Naval Sharma
49728
49728
add a comment |Â
add a comment |Â
up vote
2
down vote
you can Use clone Function to copy the selected record.Just to make code easier and not to mention the fields in code,you can use fieldset and retrieve those field in code.
PFB the sample code.
List<String> fields = new List<String>();
FieldSet fieldsToClone = SObjectType.Claimed_Transaction__c.fieldSets.FieldsToClone;
for (FieldSetMember field : fieldsToClone.getFields())
fields.add(field.getFieldPath());
String soql = 'SELECT ' + String.join(fields, ',') +
' FROM Claimed_Transaction__c WHERE Id = '' + String.escapeSingleQuotes(transId) + ''';
return Database.query(soql).Clone(...);
This is sample code.please change it according to your functionality.
New contributor
add a comment |Â
up vote
2
down vote
you can Use clone Function to copy the selected record.Just to make code easier and not to mention the fields in code,you can use fieldset and retrieve those field in code.
PFB the sample code.
List<String> fields = new List<String>();
FieldSet fieldsToClone = SObjectType.Claimed_Transaction__c.fieldSets.FieldsToClone;
for (FieldSetMember field : fieldsToClone.getFields())
fields.add(field.getFieldPath());
String soql = 'SELECT ' + String.join(fields, ',') +
' FROM Claimed_Transaction__c WHERE Id = '' + String.escapeSingleQuotes(transId) + ''';
return Database.query(soql).Clone(...);
This is sample code.please change it according to your functionality.
New contributor
add a comment |Â
up vote
2
down vote
up vote
2
down vote
you can Use clone Function to copy the selected record.Just to make code easier and not to mention the fields in code,you can use fieldset and retrieve those field in code.
PFB the sample code.
List<String> fields = new List<String>();
FieldSet fieldsToClone = SObjectType.Claimed_Transaction__c.fieldSets.FieldsToClone;
for (FieldSetMember field : fieldsToClone.getFields())
fields.add(field.getFieldPath());
String soql = 'SELECT ' + String.join(fields, ',') +
' FROM Claimed_Transaction__c WHERE Id = '' + String.escapeSingleQuotes(transId) + ''';
return Database.query(soql).Clone(...);
This is sample code.please change it according to your functionality.
New contributor
you can Use clone Function to copy the selected record.Just to make code easier and not to mention the fields in code,you can use fieldset and retrieve those field in code.
PFB the sample code.
List<String> fields = new List<String>();
FieldSet fieldsToClone = SObjectType.Claimed_Transaction__c.fieldSets.FieldsToClone;
for (FieldSetMember field : fieldsToClone.getFields())
fields.add(field.getFieldPath());
String soql = 'SELECT ' + String.join(fields, ',') +
' FROM Claimed_Transaction__c WHERE Id = '' + String.escapeSingleQuotes(transId) + ''';
return Database.query(soql).Clone(...);
This is sample code.please change it according to your functionality.
New contributor
New contributor
answered Sep 6 at 9:40
Gourav
525
525
New contributor
New contributor
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f231422%2fhow-to-clone-object-record%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password