Skip to content

Instantly share code, notes, and snippets.

@jheth
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jheth/271ab8b4ad82b2b07218 to your computer and use it in GitHub Desktop.
Save jheth/271ab8b4ad82b2b07218 to your computer and use it in GitHub Desktop.
Salesforce: Clone QuoteLineItems from Quote
try {
String sourceId = '0Q0i00000019TdOCAU';
String destinationId = null; //'0Q0i00000019TdTCAU';
String query = 'select Quoteid, PricebookEntryId, Quantity, UnitPrice, Discount, Description, ServiceDate, ListPrice, Subtotal, TotalPrice ';
// get all of the custom, writeable fields
Map<String, Schema.SObjectField> fieldMap = QuoteLineItem.getSObjectTypE().getDescribe().fields.getMap();
for (String key : fieldMap.keySet()) {
Schema.SObjectField field = fieldMap.get(key);
Schema.DescribeFieldResult result = field.getDescribe();
if (result.isCustom() && !result.isCalculated()) {
query += ' ,'+result.getName();
}
}
query += ' from QuoteLineItem where QuoteId = :sourceId LIMIT 122';
List<QuoteLineItem> q1List = Database.query(query);
for (QuoteLineItem q1 : q1List) {
QuoteLineItem q2 = q1.clone();
if (destinationId != null) {
q2.QuoteId = destinationId;
}
insert q2;
}
} catch(DMLException e) {
System.debug(e.getMessage());
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment