0

I have two custom objects: x__c and y__c. I have multiple records in y__c object. This object is displaying as a related list on x__c object. I want to create a button which will take records from y__c object and SHOW them on x__c object related list. This button should not create any records. Just showing already existing information. Any help will be appreciated.

The code below: the button opens a lookup search but after i pick a record it just refreshes the page, not adding any records into related list.

Thank You

Alex

<apex:page controller="AssociateVehicleExtController">
<apex:form>
    <apex:pageblock title="Associate Vehicle to Custom Object">
        <apex:pageMessages />
        <apex:pageblockbuttons location="top">
            <apex:commandbutton value="Associate" action="{!associate}" />
            <apex:commandbutton value="Cancel" action="{!cancel}"/>
        </apex:pageblockbuttons>
        <apex:pageBlockSection>
            <apex:pageBlockSectionItem>
                <apex:outputLabel>Order Name</apex:outputLabel>
                <apex:outputText>{!order.Name}</apex:outputText>   
            </apex:pageBlockSectionItem>
          <apex:pageBlockSectionItem>
                <apex:outputLabel>Vehicle</apex:outputLabel>
                <apex:inputField value="{!order.Vehicle1__c}"/>
            </apex:pageBlockSectionItem>                
        </apex:pageBlockSection>
    </apex:pageblock>
</apex:form>

public with sharing class AssociateVehicleExtController {

public ECS__eCommSource_Order__c order  { get; set; }

public AssociateVehicleExtController() {

    String orderId = ApexPages.currentPage().getParameters().get('id');
    order = [SELECT Id, Name, Vehicle1__c FROM ECS__eCommSource_Order__c WHERE Id =: orderId];

}

public PageReference associate () {

    Vehicle__c vec = new Vehicle__c (Id = order.Vehicle1__c, name = order.Id);

    try {
        Database.update(vec);
    } catch (Exception error) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error while associating.' + error.getMessage()));
    }

    PageReference page = new PageReference('/' + order.Id);

    return page.setRedirect(true);
}

public PageReference cancel () {
    PageReference page = new PageReference('/' + order.Id);
    return page.setRedirect(true);        
}

}

I think this part of code must associate records

public PageReference associate () {

Vehicle__c vec = new Vehicle__c (Id = order.Vehicle1__c, name = order.Id);

try {
    Database.update(vec);
} catch (Exception error) {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error while associating.' + error.getMessage()));
}

PageReference page = new PageReference('/' + order.Id);

return page.setRedirect(true);

}

  • Hi, welcome to SFSE, please take some time to visit How to Ask and take the tour in order to get familiar with the forum. Please point out what part of your code is suppose to add the item to the related list according to you for better assistance. Thanks! – glls Sep 3 '17 at 16:16
0

Child objects that appear in a related list of a parent object have a lookup (or master-detail) field that can hold the Id value of the parent object. It is that field that your code should aim to update.

It looks like your order object is a child of your vehicle object. Perhaps you really want it the other way round.

But as your code stands this would save the association from the order to the vehicle (as the apex:inputField will assign the vehicle Id into the order field automatically):

public PageReference associate () {
    update order;
    PageReference pr = new PageReference('/' + order.Vehicle1__c);
    return pr.setRedirect(true);
}

and the related list of orders under the vehicle should then include the new order.

PS

To add vehicle (child) to order (parent) you update the vehicle's order lookup field Order__c (that you may have to add if it isn't already present):

public PageReference associate () {
    // Its a bit unusual to use a lookup field like this rather than
    // creating your own list of candidates
    Id vehicleId = order.Vehicle1__c;
    update new Vehicle__c (Id = vehicleId, Order__c = order.Id);
    PageReference pr = new PageReference('/' + order.Id);
    return pr.setRedirect(true);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.