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
gonzalolgonzalol 

How to get the ID of a recored based on a value in a field using APEX

In our integration we bring over sales orders (Master Relationship) and sales order lines (Child) both are different objects, we also bring credits and returns into the same objects. 
When we right a credit or return we bring over the original sales order number and it lands on the child record (line).  What I need is to convert that original sales order number into an ID so that I can link the credit or return to the original sales order.  I need the ID to land in a lookup field using some type of apex code.
Any help is appreciated, new to apex coding.
 
anto nirmalanto nirmal
Hi Gonzal,

You can write an apex trigger in the Order line item for insert and update event.
You can also use the process builder to trigger a flow to perform this action.

The following code establishes the relationship between Order(Child) and Opportunity(Parent), based on the CONC_NO__c value which is a similar field as your ordernumber in orderline item
 
trigger Order_Opportunity_Update on Order (before update,after insert) 
{
    List<String> Lst_Str_OrderOpportunity = new List<String>();
    Map<String,String> map_OrderOpportunity = new Map<String,String>();

    for(Order Obj_Order: Trigger.new)
    {
            Lst_Str_OrderOpportunity.add(Obj_Order.CONC_NO__c);
    }

    if(!Lst_Str_OrderOpportunity.isEmpty())
    {
        for(Opportunity LeadMap: [Select Name,Id From Opportunity where Name in :Lst_Str_OrderOpportunity])
            map_OrderOpportunity.put(LeadMap.Name,LeadMap.Id);
        
        if (Trigger.isInsert)
        {
            System.debug('Insert');
            List<Order> lst_Order =  [select Id,Name,CONC_NO__c,OpportunityNumber__c from Order 
                                                 where Id IN :Trigger.new];
            for(Order Obj_Order: lst_Order)
            {
                Obj_Order.OpportunityNumber__c = map_OrderOpportunity.get(Obj_Order.CONC_NO__c);
            }
            update (lst_Order);
            System.debug('Lead' + Lst_Str_OrderOpportunity);
            System.debug('Map' + map_OrderOpportunity);
            System.debug('ListOpportunity' + lst_Order);
        }
        if (Trigger.isUpdate)
        {   
            System.debug('Update');
            for(Order Obj_Order: Trigger.new)
            {
                Obj_Order.OpportunityNumber__c = map_OrderOpportunity.get(Obj_Order.CONC_NO__c);
                System.debug(Obj_Order.OpportunityNumber__c);
            }
            System.debug('Lead' + Lst_Str_OrderOpportunity);
            System.debug('Map' + map_OrderOpportunity);
        }
    }
    
}

Let me know if this helps.

As a common practice, if your question is answered, please choose 1 best answer.
Additionally you can give every answer a like if that answer is helpful to you.

Regards,
Anto Nirmal