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
Girbson Bijou 8Girbson Bijou 8 

Transform Trigger to Apex Class

I have this Trigger which works well but i would to use a class with a quick action instead. Please help me to transform this trigger to an apex class. 
trigger PostToAfficliateInventory on Delivery__c (after update) {
    
    list <Container__c> con = new List <Container__c>();
    list <Articles_Containers__c> contenairItems = new list <Articles_Containers__c>();
    list <Item_Distributed__c> itemList = new  list<Item_Distributed__c>();
    
    Id containerRecordTypeCaracole = Schema.SObjectType.Container__c.getRecordTypeInfosByDeveloperName().get('FFP_Caracole').getRecordTypeId();
    Id containerRecordTypePAPCRN = Schema.SObjectType.Container__c.getRecordTypeInfosByDeveloperName().get('CRN_PAP').getRecordTypeId();
    Id containerRecordTypeCanteenPAP = Schema.SObjectType.Container__c.getRecordTypeInfosByDeveloperName().get('Canteen_PAP').getRecordTypeId();
    id crt; 
    
    Id ArticleContainerRecordTypeCaracole = Schema.SObjectType.Articles_Containers__c.getRecordTypeInfosByDeveloperName().get('FFP_Caracole').getRecordTypeId();
    Id ArticleContainerPAPCRN = Schema.SObjectType.Articles_Containers__c.getRecordTypeInfosByDeveloperName().get('CRN_PAP').getRecordTypeId();
    Id ArticleContainerCanteenPAP = Schema.SObjectType.Articles_Containers__c.getRecordTypeInfosByDeveloperName().get('PAP_Canteen').getRecordTypeId();
    id ArticleContainerAdmin = Schema.SObjectType.Articles_Containers__c.getRecordTypeInfosByDeveloperName().get('Admin').getRecordTypeId();
    id acrt;
    
    Container__c c;
    for (Delivery__c d : Trigger.New){
        if (d.Is_Posted_To_Affiliate_Inventory__c){
            if(d.Affiliate_Center__c =='FFP Caracole'){
                crt = containerRecordTypeCaracole;
            }else if(d.Affiliate_Center__c =='CRN PAP'){
                crt = containerRecordTypePAPCRN;
            }else if(d.Affiliate_Center__c =='PAP Canteen'){
                crt = containerRecordTypeCanteenPAP;
            }
            c = new  Container__c();
            c.Name = d.Automatic_Code__c;
            c.FFP_Centers__c = d.Affiliate_Center__c ;
            c.RecordTypeId = crt;
            c.Distribution_Center__c = d.AffiliateCenterId__c;
            c.Is_Owner_Shipper__c = 'No';
            c.Provenance__c = 'FFP PAP';
            c.Type__c = 'Food';
            c.Shipment_Status__c = 'A - AWAITING ARRIVAL';
            c.Is_Automatic_Creation__c = TRUE;
            
            insert c;
            
            itemList = [SELECT id,Name, Product__r.Expiration_Date__c,Product__r.Unit_Cost__c,   
                        Product__r.Lot_Number__c,  Product__r.Unit_Weight__c ,
                        Product__r.UM__c, Product__r.Product__r.Id , Quantity__c 
                        FROM Item_Distributed__c  where Delivery__c  =: d.id ];             
            
            for (Item_Distributed__c OrderItems: itemList){
                Articles_Containers__c ac = new Articles_Containers__c();
                if(c.FFP_Centers__c == 'FFP Caracole'){
                    acrt = ArticleContainerRecordTypeCaracole;
                }else if(c.FFP_Centers__c == 'CRN PAP'){
                    acrt = ArticleContainerPAPCRN;
                }else if (c.FFP_Centers__c == 'PAP Canteen'){
                    acrt = ArticleContainerCanteenPAP;
                }else acrt = ArticleContainerAdmin;
                
                ac.FFP_Centers__c = d.Affiliate_Center__c ;
                ac.RecordTypeId = acrt; 
                ac.Container__c =c.Id;
                ac.Product__c = OrderItems.Product__r.Product__r.Id;
                ac.Number__c= 0;
                ac.Quantity__c = OrderItems.Quantity__c;
                ac.Unit_Of_Measure_Paking__c= OrderItems.Product__r.UM__c;
                ac.UM__c = OrderItems.Product__r.UM__c;
                ac.Expiration_Date__c = OrderItems.Product__r.Expiration_Date__c;
                ac.Lot_Number__c = OrderItems.Product__r.Lot_Number__c;
                ac.Unit_Cost__c = OrderItems.Product__r.Unit_Cost__c;
                ac.Unit_Weight__c =OrderItems.Product__r.Unit_Weight__c;
                ac.DeliveryItemsReferences__c = OrderItems.id;
                
                contenairItems.add(ac);
            }
            
            Insert contenairItems; 
            
        }
        
    } 
}

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Girbson,

You could refer to this link where you can use a custom button to call a apex class, I think you should be able to create a class with that same code.

>> https://help.salesforce.com/articleView?id=000325250&type=1&mode=1

I hope this helps and in case if this comes handy can you please choose this as best answer so that it can be used by others in the future.

Regards,
Anutej