+ Start a Discussion
Jeremy DeseezJeremy Deseez 

Upsert list with values of another list

Hi SFDC Dev,

I'm stuck in my dev, I have 4 columns of User Forecasting Quota. With two different picklist, I want to choose the Quarter and copy the values (Quota Amount) into the chosen Quarter picklist, for each users. See in my screenshot, I want to choose the Quarter that I want to copy on selected second Quarter.
quota
 
public void updateQuotas() {
		Map<Id, ForecastingQuota> mapOnlyForecastQuotaQ1 = new Map<Id, ForecastingQuota>();
		Map<Id, ForecastingQuota> mapOnlyForecastQuotaQ2 = new Map<Id, ForecastingQuota>();
		Map<Id, ForecastingQuota> mapOnlyForecastQuotaQ3 = new Map<Id, ForecastingQuota>();
		Map<Id, ForecastingQuota> mapOnlyForecastQuotaQ4 = new Map<Id, ForecastingQuota>();

		Map<Id, ForecastingQuota> mapQuotaQ1 = new Map<Id, ForecastingQuota>();
		Map<Id, ForecastingQuota> mapQuotaQ2 = new Map<Id, ForecastingQuota>();
		Map<Id, ForecastingQuota> mapQuotaQ3 = new Map<Id, ForecastingQuota>();
		Map<Id, ForecastingQuota> mapQuotaQ4 = new Map<Id, ForecastingQuota>();

		Set<Id> userIdSet = new Set<Id>();

		Date selectedQuarterQ1 = Date.valueOf(quarterList.get(0).getValue());
		Date selectedQuarterQ2 = Date.valueOf(quarterList.get(1).getValue());
		Date selectedQuarterQ3 = Date.valueOf(quarterList.get(2).getValue());
		Date selectedQuarterQ4 = Date.valueOf(quarterList.get(3).getValue());
		Date selectedQuarterQ5 = Date.valueOf(quarterList.get(4).getValue());

		if (selectedQuarter != null && selectedQuarter != '') {
			Integer selectedQuarterPosition = getSelectedQuarterPosition();
			selectedQuarterQ1 = Date.valueOf(quarterList.get(selectedQuarterPosition).getValue());
			selectedQuarterQ2 = Date.valueOf(quarterList.get(selectedQuarterPosition + 1).getValue());
			selectedQuarterQ3 = Date.valueOf(quarterList.get(selectedQuarterPosition + 2).getValue());
			selectedQuarterQ4 = Date.valueOf(quarterList.get(selectedQuarterPosition + 3).getValue());
			selectedQuarterQ5 = Date.valueOf(quarterList.get(selectedQuarterPosition + 4).getValue());

		}
		if (selectedUserRole == '' || selectedUserRole == null)
			selectedUserRole = 'Sales%';

		if (selectedUserRegion == '' || selectedUserRegion == null)
			selectedUserRegion = '%%';

		List<User> userList = [SELECT Id FROM User WHERE isActive =: selectedActiveUser AND ForecastEnabled = true AND UserRole.Name LIKE :selectedUserRole AND RTS_Region__c LIKE :selectedUserRegion ORDER BY Id ASC];
		for (User usr : userList)
			userIdSet.add(usr.id);
		List<ForecastingType> forecastTypeList = [SELECT Id, DeveloperName, IsActive, isAmount, isQuantity, Language, MasterLabel FROM ForecastingType WHERE DeveloperName = 'OpportunitySplitRevenue' LIMIT 1];

		List<ForecastingQuota> queryQuotaListQ1 = [SELECT Id, QuotaAmount, QuotaOwnerId, QuotaOwner.UserRole.Name, StartDate FROM ForecastingQuota WHERE ForecastingTypeId = :forecastTypeList.get(0).Id AND StartDate >=:selectedQuarterQ1 AND StartDate <:selectedQuarterQ2 AND QuotaOwnerId IN : userIdSet ORDER BY QuotaOwnerId ASC];
		List<ForecastingQuota> queryQuotaListQ2 = [SELECT Id, QuotaAmount, QuotaOwnerId, QuotaOwner.UserRole.Name, StartDate FROM ForecastingQuota WHERE ForecastingTypeId = :forecastTypeList.get(0).Id AND StartDate >=:selectedQuarterQ2 AND StartDate <:selectedQuarterQ3 AND QuotaOwnerId IN : userIdSet ORDER BY QuotaOwnerId ASC];
		List<ForecastingQuota> queryQuotaListQ3 = [SELECT Id, QuotaAmount, QuotaOwnerId, QuotaOwner.UserRole.Name, StartDate FROM ForecastingQuota WHERE ForecastingTypeId = :forecastTypeList.get(0).Id AND StartDate >=:selectedQuarterQ3 AND StartDate <:selectedQuarterQ4 AND QuotaOwnerId IN : userIdSet ORDER BY QuotaOwnerId ASC];
		List<ForecastingQuota> queryQuotaListQ4 = [SELECT Id, QuotaAmount, QuotaOwnerId, QuotaOwner.UserRole.Name, StartDate FROM ForecastingQuota WHERE ForecastingTypeId = :forecastTypeList.get(0).Id AND StartDate >=:selectedQuarterQ4 AND StartDate <:selectedQuarterQ5 AND QuotaOwnerId IN : userIdSet ORDER BY QuotaOwnerId ASC];

		for (ForecastingQuota fq1 : queryQuotaListQ1)
			mapOnlyForecastQuotaQ1.put(fq1.QuotaOwnerId, fq1);
		for (ForecastingQuota fq2 : queryQuotaListQ2)
			mapOnlyForecastQuotaQ2.put(fq2.QuotaOwnerId, fq2);
		for (ForecastingQuota fq3 : queryQuotaListQ3)
			mapOnlyForecastQuotaQ3.put(fq3.QuotaOwnerId, fq3);
		for (ForecastingQuota fq4 : queryQuotaListQ4)
			mapOnlyForecastQuotaQ4.put(fq4.QuotaOwnerId, fq4);


		for (User u : userList) {
			mapQuotaQ1.put(u.id, mapOnlyForecastQuotaQ1.containsKey(u.id) ? mapOnlyForecastQuotaQ1.get(u.id) : new ForecastingQuota(QuotaOwnerId = u.id, QuotaAmount = null, StartDate = selectedQuarterQ1, ForecastingTypeId = forecastTypeList.get(0).Id));
			mapQuotaQ2.put(u.id, mapOnlyForecastQuotaQ2.containsKey(u.id) ? mapOnlyForecastQuotaQ2.get(u.id) : new ForecastingQuota(QuotaOwnerId = u.id, QuotaAmount = null, StartDate = selectedQuarterQ2, ForecastingTypeId = forecastTypeList.get(0).Id));
			mapQuotaQ3.put(u.id, mapOnlyForecastQuotaQ3.containsKey(u.id) ? mapOnlyForecastQuotaQ3.get(u.id) : new ForecastingQuota(QuotaOwnerId = u.id, QuotaAmount = null, StartDate = selectedQuarterQ3, ForecastingTypeId = forecastTypeList.get(0).Id));
			mapQuotaQ4.put(u.id, mapOnlyForecastQuotaQ4.containsKey(u.id) ? mapOnlyForecastQuotaQ4.get(u.id) : new ForecastingQuota(QuotaOwnerId = u.id, QuotaAmount = null, StartDate = selectedQuarterQ4, ForecastingTypeId = forecastTypeList.get(0).Id));
		}

		quotaListQ1 = mapQuotaQ1.values();
		quotaListQ2 = mapQuotaQ2.values();
		quotaListQ3 = mapQuotaQ3.values();
		quotaListQ4 = mapQuotaQ4.values();
	}

public PageReference copyQuarterToQuarter(){

		Date selectedQuarterToCopy = Date.valueOf(quarterList2.get(0).getValue());
		Date selectedQuarterCopied = Date.valueOf(quarterList3.get(0).getValue());

		System.debug(quotaListQ1);
		System.debug(quotaListQ2);

		for(ForecastingQuota qq2 : quotaListQ2){
		quotaListQ2[1].QuotaAmount = quotaListQ1[1].QuotaAmount;
	}
		update quotaListQ2;
		System.debug(quotaListQ2);
		return null;
	}

Thanks all.