0

I am trying to fetch all values from a related list and show them in a custom field on the Product page layout.

Product should contain a text field that will sort the values like this:

  1. Value 1
  2. Value 2
  3. Value 3 and so on

and my related list Bundle Line Items should contain the lines of those values. So when I add a Bundle Line Item to that related list the name of the bundle item i want to be shown in the Bundle Items text field in the Product page layout.

Thanks, Darko

1

It seems like this is still not possible using just a formula field. I think you should write a trigger that changes the custom field with the list of related items, everytime this list changes.

Example: Show the assignments of a Contact: - An assignment is added to the system - Trigger runs, updates custom field in Contact mask

I would use a trigger because it's easy to access the related items of an object, formatting them as you need (in your case with the numbered list) and storing them in a text field in the Contact related mask.

Just because you've been nice, here's the trigger you can start with:

trigger BundleLineItemsTrigger on BundleLineItem__c (before insert, before update, before delete)
{
    // Prepare string to be inserted in the custom field
    String cFieldContent = "";
    // cycle the old items and add them to the string

    for(BundleLineItem__c item: Trigger.New)
    {
        // Get old items, remember to filter out the item that is going to be updated.
        String where = "";
        if(Trigger.isUpdate)
        {
            where = " WHERE Id <> "+item.Id;
        }
        BundleLineItem__c items = [SELECT fields FROM BundleLineItem__c :where];
        // Add old items to the cFieldContent string:
        for(items...)
        {
            cFieldContent += c+". "+items.Name\n";
        }
        // Do stuff with your BundleLineItem item.
        // you can easily access parameters of the new item:
        if(item.Name != NULL)
        {
            // format your new item to be inserted in the custom field.
            // add the newly formatted item to the cFieldContent string
            cFieldContent += c+". "+items.Name\n";
        }
    }

    Product p = [SELECT Id FROM Product WHERE ...];
    p.customFieldYouCreated = cFieldContent;

    update p;
}

I'm using before insert, update and delete because in this way you can select all existing items without the newly created one. If the action is an update, you should filter out the one that is going to be updated (you should put the items query in the Trigger.new for and use item.Id to filter out the already existing item from the resultSet).

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.