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
aamDevaamDev 

Controller Extension not Returning Page Message

I created a controller extension to replace the standard "New" record for a custom object. The save and all subsequent updates are working, however, my conditional statement is not working, saving regardless.

 

Here is my VF:

 

<apex:page standardController="Merged_Contact__c" extensions="mergeContact" tabStyle="Merged_Contact__c">
<apex:sectionHeader title="Merging Contacts" subtitle="Merge a Contact's Information to Another" />
<apex:form >
	<apex:pageMessages />
	<apex:pageBlock mode="edit">
		<apex:pageBlockButtons >
			<apex:commandButton action="{!mergeSave}" value="Save" />
		</apex:pageBlockButtons>
		<apex:pageBlockSection title="Select Contacts">
			<apex:inputField value="{!Merged_Contact__c.Merge_to_Contact__c}" required="true" />
			<apex:inputField value="{!Merged_Contact__c.Merge_from_Contact__c}" required="true" />
		</apex:pageBlockSection>
		<apex:pageBlockSection title="Select Optional Fields to Merge">
			<apex:inputField value="{!Merged_Contact__c.Title__c}" />
			<apex:inputField value="{!Merged_Contact__c.Phone__c}" />
			<apex:inputField value="{!Merged_Contact__c.Email__c}" />
			<apex:inputField value="{!Merged_Contact__c.Mobile__c}" />
			<apex:inputField value="{!Merged_Contact__c.Address__c}" />
			<apex:inputField value="{!Merged_Contact__c.Other_Phone__c}" />
			<apex:inputField value="{!Merged_Contact__c.Assistant__c}" />
			<apex:inputField value="{!Merged_Contact__c.Fax__c}" />
		</apex:pageBlockSection>
	</apex:pageBlock>
</apex:form>
</apex:page>

 

 Here's my extension:

 

public with sharing class mergeContact {
	
	public Merged_Contact__c mc;
	
	ApexPages.StandardController stdController;

	public mergeContact(ApexPages.StandardController controller) {
		
		stdController = controller;
		this.mc = (Merged_Contact__c)stdController.getRecord();
		
	}
	
	public PageReference mergeSave() {

		Id toId;
		Id fromId;
		Id toHomeId;
		Id fromHomeId;
	
		Boolean title;
		Boolean email;
		Boolean address;
		Boolean assistant;
		Boolean phone;
		Boolean mobile;
		Boolean otherPhone;
		Boolean fax;
		
		Merged_Contact__c newMC;
		Contact fromContact;
		Contact updateContact;
		Contact removeContact;
	
		Set<Id> newPN = new Set<Id>();
		
		List<Contact> toHome = new List<Contact>();
		List<Contact> fromHome = new List<Contact>();
		List<Production_Member__c> updatePM = new List<Production_Member__c>();
		List<Production_Member__c> removePM = new List<Production_Member__c>();
		List<Event> updateEvent = new List<Event>();
		List<Task> updateTask = new List<Task>();

		toId = mc.Merge_to_Contact__c;
		fromId = mc.Merge_from_Contact__c;
	
		toHome = [SELECT Account.ParentId FROM Contact WHERE Id =: toId];
		toHomeId = toHome[0].Account.ParentId;
		
		fromHome = [SELECT Account.ParentId FROM Contact WHERE Id =: fromId];
		fromHomeId = fromHome[0].Account.ParentId;
		
		if(toHomeId == fromHomeId) {
			
			newMC = new Merged_Contact__c(Merge_to_Contact__c = mc.Merge_to_Contact__c, Merge_from_Contact__c = mc.Merge_from_Contact__c, Title__c = mc.Title__c, Phone__c = mc.Phone__c, Email__c = mc.Email__c, Mobile__c = mc.Mobile__c, Address__c = mc.Address__c, Other_Phone__c = mc.Other_Phone__c, Assistant__c = mc.Assistant__c, Fax__c = mc.Fax__c);
			
			title = mc.Title__c;
			email = mc.Email__c;
			address = mc.Address__c;
			assistant = mc.Assistant__c;
			phone = mc.Phone__c;
			mobile = mc.Mobile__c;
			otherPhone = mc.Other_Phone__c;
			fax = mc.Fax__c;
			
			if(title || email || address || assistant || phone || mobile || otherPhone || fax) {
				
				fromContact = [SELECT Id, Title, Email, MailingStreet, MailingCity, MailingState, MailingPostalCode, AssistantName, Phone, MobilePhone, OtherPhone, Fax FROM Contact WHERE Id =: fromId];
				updateContact = [SELECT Id, Title, Email, MailingStreet, MailingCity, MailingState, MailingPostalCode, AssistantName, Phone, MobilePhone, OtherPhone, Fax FROM Contact WHERE Id =: toId];
				
				if(title) { updateContact.Title = fromContact.Title; }
				if(email) { updateContact.Email = fromContact.Email; }
				if(address) { updateContact.MailingStreet = fromContact.MailingStreet; }
				if(address) { updateContact.MailingCity = fromContact.MailingCity; }
				if(address) { updateContact.MailingState = fromContact.MailingState; }
				if(address) { updateContact.MailingPostalCode = fromContact.MailingPostalCode; }
				if(assistant) { updateContact.AssistantName = fromContact.AssistantName; }
				if(phone) { updateContact.Phone = fromContact.Phone; }
				if(mobile) { updateContact.MobilePhone = fromContact.MobilePhone; }
				if(otherPhone) { updateContact.OtherPhone = fromContact.OtherPhone; }
				if(fax) { updateContact.Fax = fromContact.Fax; }
				
			}
	
			removeContact = new Contact(Id = fromId, Active__c = False);
				
			for(Production_Member__c fpm : [SELECT Production_No__c FROM Production_Member__c WHERE Contact__c =: fromId]){
				
				newPN.add(fpm.Production_No__c);
				
			}
		
			for(Production_Member__c tpm : [SELECT Production_No__c FROM Production_Member__c WHERE Contact__c =: toId]){
				
				newPN.remove(tpm.Production_No__c);
				
			}
			
			for(Production_Member__c upm : [SELECT Id FROM Production_Member__c WHERE Contact__c =: fromId AND Production_No__c IN : newPN]){
				
				Production_Member__c pm = new Production_Member__c(Id = upm.Id, Contact__c = toId);
				updatePM.add(pm);
				
			}
		
			for(Production_Member__c rpm : [SELECT Id FROM Production_Member__c WHERE Contact__c =: fromId AND Production_No__c NOT IN : newPN]){
				
				Production_Member__c pm = new Production_Member__c(Id = rpm.Id);
				removePM.add(pm);
				
			}
		
			for (Event e : [SELECT Id FROM Event WHERE WhoId =: fromId]) {
				
				Event fe = new Event(Id = e.Id, WhoId = toId);
				updateEvent.add(fe);
				
			}
			
			for (Task t : [SELECT Id FROM Task WHERE WhoId =: fromId]) {
				
				Task te = new Task(Id = t.Id, WhoId = toId);
				updateTask.add(te);
				
			}

			if(newMC != null) {	insert newMC; }
			
			if(updatePM.size() > 0) { update updatePM; }
			
			if(removePM.size() > 0) { delete removePM; }
			
			if(updateEvent.size() > 0) { update updateEvent; }
		
			if(updateTask.size() > 0) { update updateTask; }

			if(removeContact != null) { update removeContact; }

			if(updateContact != null) { update updateContact; }
			
			PageReference curPage = ApexPages.currentPage();
			ApexPages.Message confirmMSG = new ApexPages.message(ApexPages.Severity.CONFIRM, 'The Merge was Successful');
			ApexPages.addMessage(confirmMSG);
			curPage.setRedirect(true);
			return curPage;
			
		} else {
			
			PageReference curPage = ApexPages.currentPage();
			ApexPages.Message errorMSG = new ApexPages.message(ApexPages.Severity.ERROR, 'The selected Merge from Contact is outside of the Merge to Contact Home Office');
			ApexPages.addMessage(errorMSG);
			return curPage;
			
		}
		
	}

}

 

My question is two fold:

 

  1. Why is it saving regardless of my conditional statement?
  2. I'm assuming question one answers this, but why are my messages not showing?'

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
aamDevaamDev

Thanks Avrom,

 

I found that updating my setting of the extension fixed the issue:

 

public Merged_Contact__c mc { get; private set; }

public mergeContact() {
		
	id id = ApexPages.currentPage().getParameters().get('id');
	mc = (id == null) ? new Merged_Contact__c() :
	[SELECT Name, Merge_to_Contact__c, Merge_from_Contact__c, Title__c, Phone__c, Email__c, Mobile__c, Address__c, Other_Phone__c, Assistant__c, Fax__c FROM Merged_Contact__c WHERE Id =: id];
		
}

 

Thanks.

 

All Answers

AvromAvrom

So, the first thing I'd check is ensuring (e.g., through System.debug()) that all your variable values are as you expect them to be. Are toHomeId and fromHomeId really distinct when they should be? If not, what about toId and fromId? Are you actually getting to all the correct places in your code? My guess is that one of these checks will, at least, narrow down the issue considerably.

aamDevaamDev

Thanks Avrom,

 

I found that updating my setting of the extension fixed the issue:

 

public Merged_Contact__c mc { get; private set; }

public mergeContact() {
		
	id id = ApexPages.currentPage().getParameters().get('id');
	mc = (id == null) ? new Merged_Contact__c() :
	[SELECT Name, Merge_to_Contact__c, Merge_from_Contact__c, Title__c, Phone__c, Email__c, Mobile__c, Address__c, Other_Phone__c, Assistant__c, Fax__c FROM Merged_Contact__c WHERE Id =: id];
		
}

 

Thanks.

 

This was selected as the best answer