salesforce clone

global with sharing class QuoteCloneController {


	webservice static String cloneQuote(ID oId, boolean withChildren) {
		Quote quote=(Quote)queryForRecords(Quote.getSObjectType(),oId);
		List quoteLineItemList=(List)queryChildrenRecords(QuoteLineItem.getSObjectType(),oId);
		Map> idQuoteListMap=new Map>();

		Integer cloneCount=0;

		Quote quoteClone=quote.clone(false, false, false, false);
		String query = 'Select ID FROM Quote WHERE Name LIKE ' +'\'%'+quoteClone.Name+'%'+'\'';
		System.debug('sql'+query);
		List quotelist=database.query(query);
		if(quotelist.size()>0){
			FOR(Quote quoteNum: quotelist){
				cloneCount++;
			}
		}
		quoteClone.Name = quoteClone.Name+'_clone'+cloneCount;
		insert quoteClone;
		List quoteLineItemCloneList=new List();
		for(QuoteLineItem item:quoteLineItemList){
			QuoteLineItem itemClone=item.clone(false,false);
			itemClone.QuoteId=quoteClone.Id;
			quoteLineItemCloneList.add(itemClone);
		}

		List backupListStore=new List();
		Integer backupSize=0;
		if(quoteLineItemList.size()==0){

			QuoteLineItem item = new QuoteLineItem();

			for(QuoteLineItem i :quoteLineItemCloneList){
				item.Id=i.Id;
			}
			item.UnitPrice=20;
			item.Sales_Price_T__c=30;
			item.Quantity=50;
			backupListStore.add(item);
			backupSize=backupListStore.size();
			item.Customer_Code__c=String.valueOf(backupSize);

			QuoteLineItem cloneItem=item.clone(false,false,false);
			backupListStore.add(cloneItem);
			backupSize+=1;
		}



        
        System.debug('----->'+quoteLineItemCloneList);
		insert quoteLineItemCloneList;

		return New PageReference('/' + quoteClone.Id).getURL();

	}
	private static List queryChildrenRecords(Schema.sObjectType oType, ID quoteId){
		String[] fldsToClone = getCreateableFields(oType);
		system.debug(logginglevel.error, fldsToClone);

		String query = 'Select ID, ';

		query += string.join(fldsToClone, ',');


		query += ' From QuoteLineItem' ; // + ' Where ID IN :rIds';


		query += ' Where QuoteId = :quoteId';

		return database.query(query);

	}

	private static sObject queryForRecords(Schema.sObjectType oType, ID rId) {

		String[] fldsToClone = getCreateableFields(oType);
		system.debug(logginglevel.error, fldsToClone);

		String query = 'Select ID, ';

		query += string.join(fldsToClone, ',');


		query += ' From Quote' ; // + ' Where ID IN :rIds';


		query += ' Where ID = :rId';

		return database.query(query);

	}
	private static String[] getCreateableFields(Schema.sObjectType obj) {
		String[] results = New String[]{
				};
		for (Schema.sObjectField fld : obj.getDescribe().fields.getMap().values()) {

			Schema.DescribeFieldResult dfr = fld.getDescribe();

			//ignore Unique fields. They will have to be filled in manually
			if (!dfr.isUnique() && (dfr.isCreateable() || dfr.isUpdateable())) {
				results.add(dfr.getName());
			}
		}


		return results;

	}



		public QuoteCloneController(ApexPages.StandardController controller){

			Id recordId = ((Quote)controller.getRecord()).Id;
			Quote quote = [
					SELECT Id, Name, AccountId, Account.Name,OpportunityId
					FROM Quote
					WHERE Id =:recordId
			];
			List quoteList=new List();

			quoteList.add(quote.clone());

			insert quoteList;

		}

}

 

你可能感兴趣的:(salesforce)