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
Jessica MockJessica Mock 

Setting Address Fields

I have a custom checkbox field Address Same as Patient on Guest Record. When field =True then populate the address fields on Guest Record from Patient Record. The issue I'm having it that the Guest_Street__c field is not populating after save. It appears on the Guest Record page when checkbox is check; however, after save it does not appear on the Guest Record.

    public void setAddress() {
        if (guest.Address_Same_As_Patient__c){
          guest.Guest_Street__c = patient.Street__c;
          guest.Guest_City__c = patient.City__c;
          guest.Guest_State_Province__c = patient.State_Province__c;
          guest.Guest_Zip_Postal_Code__c = patient.Zip_Postal_Code__c;
          guest.Guest_Country__c = patient.Country__c;
        }
    }

 
James LoghryJames Loghry
Do you have additional code you could share?  It's impossible to tell what the problem is from this code snippet.  Also, are you encountering any errors when the save operation should be made?  Have you enabled debug logs and tried to debug the state of the Guest_Street__c field before saving, etc?
Jessica MockJessica Mock
I have not received any errors. I'm not sure how to debug.

public class NewGuestController {

  private final Contact guest;

  private final Contact patient;

  private final npe4__Relationship__c relationship;

  

  public Contact getGuest() {

        return guest;

    }

    

    public Contact getPatient() {

        return patient;

    }

    

    public npe4__Relationship__c getRelationship() {

        return relationship;

    }

    

    public NewGuestController(ApexPages.StandardController stdController) {

        guest = (Contact)stdController.getRecord();

        guest.RecordTypeId = '012o0000000FwVk';

        

        this.patient = [SELECT Id, Name, AccountId,

          Street__c, City__c, State_Province__c, Zip_Postal_Code__c, Country__c, npe01__Primary_Address_Type__c,

        Guest_Street__c, Guest_City__c, Guest_State_Province__c, Guest_Zip_Postal_Code__c, Guest_Country__c

      FROM Contact 

        WHERE Id = :ApexPages.currentPage().getParameters().get('pid')];

        

        guest.AccountId = patient.AccountId;

        

        relationship = new npe4__Relationship__c();

        relationship.npe4__Contact__c = patient.Id;

    }

    

    public void setAddress() {

        if (guest.Address_Same_As_Patient__c){

          guest.npe01__Primary_Address_Type__c = patient.npe01__Primary_Address_Type__c;

          guest.Guest_Street__c = patient.Street__c;

          guest.Guest_City__c = patient.City__c;

          guest.Guest_State_Province__c = patient.State_Province__c;

          guest.Guest_Zip_Postal_Code__c = patient.Zip_Postal_Code__c;

          guest.Guest_Country__c = patient.Country__c;

        }

    }




    public PageReference save()

    {   

        insert guest;

        

        relationship.npe4__RelatedContact__c = guest.Id;

        insert relationship;

        

        PageReference pageRef = new PageReference('/' + patient.Id);

        pageRef.setRedirect(true);

        

        return pageRef;

    }

}
James LoghryJames Loghry

Can you poste your Visualforce page as well?  It looks to me like your setAddress method is never called.  

In the example below, I've added debugging statements to two of your methods.  If you go to Setup->Logging->Debug Logs, and then create a new debug log for the user you're executing the code as, you should see the statements in your logs, if your code is executed.

public void setAddress() {
        System.debug('In set address method, guest before: ' + guest);
        if (guest.Address_Same_As_Patient__c){
          guest.npe01__Primary_Address_Type__c = patient.npe01__Primary_Address_Type__c;
          guest.Guest_Street__c = patient.Street__c;
          guest.Guest_City__c = patient.City__c;
          guest.Guest_State_Province__c = patient.State_Province__c;
          guest.Guest_Zip_Postal_Code__c = patient.Zip_Postal_Code__c;
          guest.Guest_Country__c = patient.Country__c;
        }
        System.debug('In set address method, guest after: ' + guest);
    }

    public PageReference save(){   
        System.debug('In save method, guest is: ' + guest);
        insert guest;

        relationship.npe4__RelatedContact__c = guest.Id;
        insert relationship;

        PageReference pageRef = new PageReference('/' + patient.Id);
        pageRef.setRedirect(true);
        return pageRef;
    }

}
Jessica MockJessica Mock
<apex:page standardController="Contact" extensions="NewGuestController" showheader="false" sidebar="false" id="page">

    <apex:form id="form">

        <apex:pageBlock mode="edit" id="block1" title="Add New Guest">

            <apex:pageBlockSection title="Guest Details" columns="1" id="details">

                <apex:inputField value="{!guest.Date__c}"/>

                <apex:outputField value="{!patient.name}" label="Patient Name"/>

                <apex:inputField value="{!guest.salutation}"/>

                <apex:inputField value="{!guest.firstname}"/>

                <apex:inputField value="{!guest.lastname}"/>

                <apex:inputField value="{!guest.Guest_DOB__c}"/>

                <apex:inputField value="{!guest.Gender__c}"/>

                <apex:inputField value="{!guest.Phone}"/>

                <apex:inputField value="{!guest.Email}"/>

                <apex:inputField value="{!relationship.npe4__Type__c}"/>

                <apex:inputField value="{!guest.Demographic_Information__c}"/>

                <apex:inputField value="{!guest.Other_Demographic_Information__c}"/>

            </apex:pageBlockSection>

            <apex:pageBlockSection title="Address Information" columns="1" id="address">

                <apex:inputField value="{!guest.Address_Same_As_Patient__c}">

                    <apex:actionSupport event="onchange" action="{!setAddress}" reRender="block1"/>

                </apex:inputField>

                <apex:inputField value="{!guest.Guest_Street__c}"/>

                <apex:inputField value="{!guest.Guest_City__c}"/>

                <apex:inputField value="{!guest.Guest_State_Province__c}"/>

                <apex:inputField value="{!guest.Guest_Zip_Postal_Code__c}"/>

                <apex:inputField value="{!guest.Guest_Country__c}"/>

            </apex:pageBlockSection>

            <apex:pageBlockSection title="Permission and Marketing" columns="1" id="permission">

                <apex:inputField value="{!guest.Contact_Receive_updates_about_RMFR__c}"/>

                <apex:inputField value="{!guest.Contact_Updates_about_RMFR__c}"/>

                <apex:inputField value="{!guest.Contact_Photo_Release_Permission__c}"/>

                <apex:inputField value="{!guest.Permission_to_contact_hospital_directly__c}"/>

            </apex:pageBlockSection>

            <apex:pageBlockButtons location="bottom" id="blockButtons">

                <apex:commandButton value="Save" action="{!save}"/>

            </apex:pageBlockButtons>

        </apex:pageBlock>

    </apex:form>

</apex:page>
Jessica MockJessica Mock
setAddress is called when the checkbox is checked, the fields being set in the UI.