function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jeremy DeseezJeremy Deseez 

Upsert two Objects

Hello Dev Community,
I'm making an new app, named FAST.
I have two Objects : FAST and FAST_OPP. In FAST_OPP, there are informations about Opportunity, and in FAST there are other data link with FAST_OPP.
When I update FAST_OPP, I want to push also data in FAST. But I need to create ID in FAST during the upsert and I don't know how to do this.
Here's my controller code and visualforce :
 
public with sharing class ManageFAST {

	public List<SelectOption> 							quarterList {get;set;}
	public List<SelectOption> 							fastType{get;set;}

	public FAST__c 										fast{get;set;}
	public List<FAST_OPP__c> 							fastOppList{get;set;}
	private List<ManageOpp>								manageOppList{get;set;}
	private manageUser 									oppUser;
	private ManageOpp 									oppOpp;
	public Date 										quarter;
	public String 										selectedQuarter {get;set;}
	public Date 										selectedQuarterReturn{get;set;}
	public Date 										testdate{get;set;}

	public ManageFAST() {
		fast = new FAST__c();
		fastOppList = new List<FAST_OPP__c>();
		oppUser = new ManageUser();
		oppOpp = new ManageOpp();
		oppUser.load(UserInfo.getUserId());
		quarter = Date.today();
		quarterList = loadQuarterList();
		loadFastType();
		loadFAST();
		loadFASTOPP();
		initFASTOppList();
	}


	public void loadFAST(){
		List<FAST__c> fastList = new List<FAST__c>();
		fastList = [SELECT Id, Sales_ID__c, Forecast_Manager_ID__c, Quarter_Date__c, Commit_Manager__c, Transactionnal_Amount_Forecast_Exit__c, 
					Transactionnal_Amount_Optimistic__c, Transactionnal_Amount_Pessimistic__c,Opp_Amount_Optimist__c,
					Opp_Amount_Forecast_Exit__c, Opp_Amount_Pessimist__c
					FROM FAST__c 
					WHERE Sales_ID__c =: oppUser.getId()
					//AND Quarter_Date__c >=:selectedQuarterReturn 
					LIMIT 1
					];

		if(fastList.isEmpty()){
			fast = new FAST__c();
			fast.Sales_ID__c = oppUser.getId();
			fast.Transactionnal_Amount_Pessimistic__c = 0;
			fast.Transactionnal_Amount_Optimistic__c = 0;
			fast.Transactionnal_Amount_Forecast_Exit__c = 0;
			fast.Opp_Amount_Pessimist__c = 0;
			fast.Opp_Amount_Optimist__c = 0;
			fast.Opp_Amount_Forecast_Exit__c = 0;
			fast.Commit_Manager__c = 0;
		} else {
			fast = fastList.get(0);
		}
	}


	public void loadFASTOPP(){
			//selectedQuarterReturn = Date.valueOf(selectedQuarter);
			fastOppList = [SELECT Id, Opp_ID__c, IsClosed__c, Type__c, Opp_Amount__c, Opp_ID__r.Amount , Opp_ID__r.Account.Name, 
			FAST_ID__r.Transactionnal_Amount_Forecast_Exit__c,FAST_ID__r.Transactionnal_Amount_Pessimistic__c,
			FAST_ID__r.Transactionnal_Amount_Optimistic__c , FAST_ID__r.Opp_Amount_Pessimist__c, FAST_ID__r.Opp_Amount_Forecast_Exit__c ,
			FAST_ID__r.Opp_Amount_Optimist__c, FAST_ID__r.Id
			FROM FAST_OPP__c 
			WHERE IsClosed__c != true
			//WHERE FAST_ID__c =: fast.Id
			//AND FAST_ID__r.Sales_ID__c =: oppUser.getId()
			//AND FAST_ID__r.Quarter_Date__c >=: selectedQuarterReturn
			];
	}

	public void initFASTOppList(){
		oppUser.loadOpportunityList(quarter);
		for(ManageOpp mo : oppUser.getOpportunityList())
		{

			Map<Id, FAST_OPP__c> testMap = new Map<Id, FAST_OPP__c>();
			for(FAST_OPP__c fo : fastOppList){
				//System.debug('fastOppList before init' + fastOppList);

				testMap.put(fo.Opp_ID__c, fo);
				System.debug('testMap' + testMap);

				//System.debug('fastOppList after init' + fastOppList);

			}

			if(!testMap.containsKey(mo.getOppId())){
				FAST_OPP__c fastOpp = new FAST_OPP__c();
				fastOpp.Opp_ID__c = mo.getOppId();
				//fastOpp.FAST_ID__c = fast.Id;
				fastOpp.IsClosed__c = mo.getIsClosed();
				fastOpp.Type__c = 'none';
				//fastOpp.FAST_ID__r.Sales_ID__c = oppUser.getId();
				fastOppList.add(fastOpp);
				System.debug('FAST OPP LIST IN INIT AFTER ADD' + fastOppList);

			}
			//System.debug('FIRST USER DEBUG BEFORE ADD TO FAST_OPP__c' + fastOppList);
		}

		//System.debug('TEST FASTOPPLIST' + fastOppList.size());



	}


	public List<SelectOption> loadFastType(){
		fastType = new List<SelectOption>();
		fastType.add(new SelectOption('none', '-- Select Type --'));
		fastType.add(new SelectOption('Optimist', 'Optimist'));
		fastType.add(new SelectOption('Forecast Exit', 'Forecast Exit'));
		fastType.add(new SelectOption('Pessimist', 'Pessimist'));
		return fastType;
	}

	public void assignTypeCopyValue(){

	}



	public List<SelectOption> loadQuarterList() {
		List<SelectOption> optionsquarter = new List<SelectOption>();
		List<Period> resultsquarter = [SELECT EndDate, FullyQualifiedLabel, StartDate, Type
		FROM Period 
		WHERE Type = 'Quarter' AND StartDate >= LAST_YEAR];
		for(Period rq : resultsquarter){
		optionsquarter.add(new SelectOption(String.valueOf(rq.get('StartDate')),String.valueOf(rq.get('FullyQualifiedLabel'))));
		selectedQuarterReturn = Date.valueOf(rq.get('StartDate'));
		}
		//System.debug('selectedQuarterReturn' + selectedQuarterReturn);

		return optionsquarter;
	}





	public FAST__c getFAST(){
		return this.fast;
	}


	public PageReference goSave() {
		List<FAST_OPP__c> fastOppListToSave = new List<FAST_OPP__c>();
		for(FAST_OPP__c fol : fastOppList){
			System.debug('fastOppList during SAVE' + fastOppList);

			//fol.FAST_ID__c = fast.Id; // il est null la
			//fol.FAST_ID__r.Sales_ID__c = oppUser.getId();

			//System.debug('TEST fastOppListToSave' + fol.FAST_ID__c);

			if(fol.Type__c != 'none'){
				fastOppListToSave.add(fol);
				System.debug('fastOppListToSave during SAVE' + fastOppListToSave);
				//System.debug('SALES ID' + fol.FAST_ID__r.Sales_ID__c);
				//System.debug('TEST fastOppListToSave' + fastOppListToSave.size());

			}
		}
		try {
			upsert fastOppListToSave;
		} catch (exception ex) {
			System.debug(ex);
		}

		return null;
	}




}
 
<apex:page showHeader="true" sidebar="true" controller="ManageFAST" >

<apex:includeScript value="{!URLFOR($Resource.jquery, '/jquery-ui-1.10.3.custom/js/jquery-1.9.1.js')}" />
<apex:includeScript value="{!URLFOR($Resource.jquery, '/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js')}" />

<apex:slds />

<div class="slds-scope">

<apex:form id="formId">

<script type="text/javascript">
			$j = jQuery.noConflict();
			
			function saveComplete() {
				window.location.reload(true);
			}
	
			function testalert(){
				alert('test alert button change typelist in top');
				console.log('test');
			};

			$j(document).ready(function() {
				console.log('Test lancement JQUERY');

					$j('.type_opp').change(function(){			
					var opp_amount = $j(this).parent().parent().find('.amount_to_copy').text();

						if($j(this).parent().parent().find('.type_opp').val() == 'Pessimist'){
							$j(this).parent().parent().find('.pessimist').val(opp_amount);
							$j(this).parent().parent().find('.forecast_exit').val(opp_amount);
							$j(this).parent().parent().find('.optimist').val(opp_amount);
							console.log('change amount to pessimist');

						} else if ($j(this).parent().parent().find('.type_opp').val() == 'Forecast Exit'){
							$j(this).parent().parent().find('.pessimist').val('');
							$j(this).parent().parent().find('.forecast_exit').val(opp_amount);
							$j(this).parent().parent().find('.optimist').val(opp_amount);
							console.log('change amount to optimist');

						} else if ($j(this).parent().parent().find('.type_opp').val() == 'Optimist'){
							$j(this).parent().parent().find('.pessimist').val('');
							$j(this).parent().parent().find('.forecast_exit').val('');
							$j(this).parent().parent().find('.optimist').val(opp_amount);
							console.log('change amount to pessimist');
						}
				});
			});
		</script>

<br/><br/>
<apex:selectList value="{!selectedQuarter}"  size="1">
	<apex:selectOptions value="{!QuarterList}" />
	<apex:actionSupport event="onchange" action="{!loadFASTOPP}" reRender="formId"/>
</apex:selectList>
<br/><br/>


<apex:pageBlock>
	<apex:pageBlockTable styleClass="slds-table slds-table--bordered slds-table--cell-buffer" value="{!fastOppList}" id="table" var="f" >
				<apex:column headerValue="Opportunity Name" value="{!f.Opp_ID__c}"/>
				<apex:column headerValue="Opportunity Account" value="{!f.Opp_ID__r.Account.Name}"/>
				<apex:column headerValue="Amount" styleClass="amount_to_copy" value="{!f.Opp_Amount__c}"/>
				<apex:column headerValue="Type">
					<apex:selectList styleClass="type_opp"  value="{!f.Type__c}"  size="1">
						<apex:selectOptions value="{!fastType}" />
					</apex:selectList>
				</apex:column>
				
				<apex:column headerValue="Total Pessimistic">
				<apex:inputText styleClass="pessimist" value="{!f.FAST_ID__r.Opp_Amount_Pessimist__c}"/>
				</apex:column>

				<apex:column headerValue="Total Forecast Exit">
				<apex:inputText styleClass="forecast_exit" value="{!f.FAST_ID__r.Opp_Amount_Forecast_Exit__c}"/>
				</apex:column>

				<apex:column headerValue="Total Optimistic">
				<apex:inputText styleClass="optimist" value="{!f.FAST_ID__r.Opp_Amount_Optimist__c}"/>
				</apex:column>
				<apex:column headerValue="Is closed ?">
				<apex:inputText styleClass="slds-input" value="{!f.IsClosed__c}"/>
				</apex:column>
				<apex:column headerValue="Id du FAST" value="{!f.FAST_ID__r.Id}"/>
				<apex:column headerValue="Id du FAST OPP" value="{!f.Id}"/>


	</apex:pageBlockTable>
</apex:pageBlock>

<apex:commandButton styleClass="slds-button--selected" value="Save" action="{!goSave}" oncomplete="saveComplete()" reRender="formId"></apex:commandButton>
<br/>
<apex:commandButton styleClass="slds-button--selected" value="TEST loadFAST" action="{!loadFAST}" oncomplete="saveComplete()" reRender="formId"></apex:commandButton>


<apex:pageBlock>
<apex:pageBlockTable styleClass="slds-table slds-table--bordered slds-table--cell-buffer" value="{!fast}" var="fr" >
				<apex:column headerValue="Opportunity Name" value="{!fr.Sales_ID__c}"/>
				
			</apex:pageBlockTable>
		</apex:pageBlock>



</apex:form>
</div>



</apex:page>

I want to copy the value stored in the visualforce table into FAST and FAST_OPP.

Best Regards,
Jeremy
 
Jeremy DeseezJeremy Deseez
User-added image
Alain CabonAlain Cabon
Hi,

What do you see in the view state?

More clearly for your problem, have you lose the ID of fast line 152  ( public FAST__c fast{get;set;} )

152            // fol.FAST_ID__c = fast.Id; // il est null la = NULL VALUE ?
153            // fol.FAST_ID__r.Sales_ID__c = oppUser.getId();

Salesforce allows you to examin the View State (encrypted data otherwise for firebug).

The data in the view state should be sufficient to recreate the state of the page when the postback is received. To do this, it stores the following data:
  1. All non-transient data members in the associated controller (either standard or custom) and the controller extensions.
  2. Objects that are reachable from a non-transient data member in a controller or controller extension.
  3. The component tree for that page, which represents the page's component structure and the associated state, which are the values applied to those components.
  4. A small amount of data for Visualforce to do housekeeping.
  5. View state data is encrypted and cannot be viewed with tools like Firebug. The view state inspector described below lets you look at the contents of view state.
https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State

line 53: fast is loaded and a public property is in the view state normally.

 <apex:column headerValue="Opportunity Name" value="{!fr.Sales_ID__c}"/>   => is not null?

You can follow all the values in the view state ("live") for the debugging.

Best regards

Alain