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
Toby TarczyToby Tarczy 

Update a new field on the Order Object

Hi,

I have created a new object called Factory with a field called Factory.name. I would now like to update all the Order records as I have added a lookup field from Order to the Factory Object to add the Factory.name on the order. The filter would be based on the customer name where I would like to run the update script for each customer separately. The upate would be all orders with the name of "Account Name to be added" , (preference would be to include multiple account names), and the Factory Name would = "Wolverhampton". I am not very well versed with DML or SFCD sql , however if someone could kindly provide the statement structure I would be very gratelful.

Many thanks

Toby
Best Answer chosen by Toby Tarczy
Agustina GarciaAgustina Garcia
I think now I understand it better. When you say see Factory.Name on Order lookup is because that's the value you see, the Name value of the Factory, however, just for your information, this fields must be populated with the Factory Id value and automatically you would see the name.

Regarding doing it with Process Builder, this is not the best user case for this #clicksnotcode tool because you also need to modify old records and an action that start the process. If for example you want to populate the Factory lookup field everytime you create a new Order, then PB would work, the action is the creation of the Order. Or if you modify the Order then populate the the lookup with the value. The negative aspect is that you would have to go throw all Orders one by one clicking on Save button.

I would go for the script. In order to run it you can open Developer Console and under debug, Open Execute Anonymous Windows.

User-added image

Then just run:
 
MyOrderUpdateService.myMethod();

 

All Answers

Agustina GarciaAgustina Garcia
Do you already have the all Orders records with the Factory value? If so, you don't need a script. Just create a new formula field on Orders that looks at your Factory.name

Look at this example. Think that CO1 is your Factory object and CO2 is your Order object

Go to Order and create the formula field. These are the steps:

1st - Select formula type
User-added image

2nd - Select your formula type. Text would be good

User-added image

3rd - Add the formula type. In my case CO1__c.Name instead of your Factory__c.Name

User-added image

Having this, by the time you populate the lookup to Factory, the formula field would be populated automatically, getting this result:

User-added image

If you don't have your Order lookup field to Factory populated, then you need a piece of code to do it. Then the formula would work in a similar way. A simple piece of code would be something like this: Plese take into account I just compile it but I didn't try
 
public class MyOrderUpdateService
{
    public void myMethod()
    {
        //CO3 is Customer
        //CO2 is Order
        //CO1 is Factory
        
        Map<Id, CO2__c> customerIdAndOrder = new Map<Id, CO2__c>();
        for(CO2__c order : [Select Id, Name, CO1__c, CO3__c From CO2__c])
        {
            customerIdAndOrder.put(order.CO3__c, order);
        }
        
        List<CO2__c> ordersToUpdate = new List<CO2__c>();
        Set<Id> orderIds = customerIdAndOrder.keySet();
        for(CO3__c customer : [Select Id From CO3__c Where Id IN :orderIds])
        {
            CO2__c order = customerIdAndOrder.get(customer.Id);
            order.CO1__c = '<yourValue>';
            ordersToUpdate.add(order);
        }
        
        update ordersToUpdate;
    }
}

Hope this help
Toby TarczyToby Tarczy
Hi Agustina, Thank you for your response! I don’t have all orders with the factory name. I have created a lookup on the Orders field to select the Factory.name. Now I just want to update this field for certain accounts. I am wondering is there a way to do this using process builder? I think when reading through your response I need to run the code to update the existing Order records with the Factory.name as I think you have so kindly provided. How do I execute this code? I will do it first in our sandbox. I just had a thought, through process builder I could specify the condition on the order object to then execute the update record on the factory.name field that is in the Order object as a lookup. Does that make sense? Kind Regards, Toby
Agustina GarciaAgustina Garcia
I think now I understand it better. When you say see Factory.Name on Order lookup is because that's the value you see, the Name value of the Factory, however, just for your information, this fields must be populated with the Factory Id value and automatically you would see the name.

Regarding doing it with Process Builder, this is not the best user case for this #clicksnotcode tool because you also need to modify old records and an action that start the process. If for example you want to populate the Factory lookup field everytime you create a new Order, then PB would work, the action is the creation of the Order. Or if you modify the Order then populate the the lookup with the value. The negative aspect is that you would have to go throw all Orders one by one clicking on Save button.

I would go for the script. In order to run it you can open Developer Console and under debug, Open Execute Anonymous Windows.

User-added image

Then just run:
 
MyOrderUpdateService.myMethod();

 
This was selected as the best answer
Toby TarczyToby Tarczy
Thank you Augistina for your help it worked.

Toby