1

I am looking for fields to update in one object to other I mean there are similar fields in both the objects Test1 & Test2, Test2 had the look up to Test1 object so if we enter the details in test2 object then the fields relationships in the Test1 should reflect. how to do this. I haven't done this before eg:

objects: Test1__c              Test2__c
Fields:  stage__c(lookup)      stage__c(lookup)
         Type__c(PickList)     Type__c(PickList)    
         Field__c(lookup)      Field__c(lookup)
3

1 Answer 1

3

Something like that should get you started (it's a very stupid example because it works much better on standard page of adding new Contact but you'll get the idea):

public class ContactController{
    public Contact c {get;set;}
    private Account acc;

    public ContactController(ApexPages.StandardController ctrl){
        c = (Contact) ctrl.getRecord();
        c.LastName = 'Test'; // make sure all required fields are filled in or the request won't work
    }

    public void fetchRelatedAccountData(){
        if(c.AccountId != null){
            acc = [SELECT BillingStreet, BillingCity, BillingPostalCode, BillingCountry FROM Account WHERE Id = :c.AccountId];
            c.MailingStreet = acc.BillingStreet;
            c.MailingCity= acc.BillingCity;
            c.MailingPostalCode= acc.BillingPostalCode;
            c.MailingCountry = acc.BillingCountry;
        }
    }
}

<apex:page standardController="Contact" extensions="ContactController">
    <apex:form>
    <apex:pageMessages />
    <apex:pageBlock>
        <apex:pageBlockSection title="Pick an account!">
            <apex:inputField value="{!c.LastName}" />
            <apex:inputField value="{!c.AccountId}">
                <apex:actionSupport event="onchange" action="{!fetchRelatedAccountData}" rerender="addressData" status="requestStatus"/>
            </apex:inputField>
            <apex:actionStatus id="requestStatus" startText="(requesting...)"  stopText=" (done)"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection id="addressData" columns="1" title="and check if address data changes">
            <apex:inputField value="{!c.MailingStreet}" />
            <apex:inputField value="{!c.MailingCity}" />
            <apex:inputField value="{!c.MailingPostalCode}" />
            <apex:outputField value="{!c.MailingCountry}" />
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Once this example works for you next step would be to read up about actionSupport, actionFunction and other ajax-related tags. Also - @RemoteAction can be a nice choice. My example is a bit naive because it doesn't work if required field is not populated. You can bypass it with <apex:actionSupport immediate="true"...> parameter but this will skip the setter method as well and you won't learn the new AccountId.

So - now you have at least all the keywords to read about in the docs :) There are TONS of nice examples for Ajax in Visualforce or @RemoteAction

Last but not least: did you know about the Cookbook at developer.force.com? http://developer.force.com/cookbook/recipe/dynamically-updating-a-page is also a nice example.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .