githubbob42 / mingle2github2

0 stars 1 forks source link

Copy price list item properties to a ticket item - Trigger #264

Open githubbob42 opened 5 years ago

githubbob42 commented 5 years ago

Mingle Card: 394 When a ticket item is created in Force.com we need to copy some price list item properties to the ticket item.

The code below should be close to a good working trigger

trigger SetupTicketItemFromPriceBookItem on Ticket_Item__c (before insert) {

    // This trigger needs to eventually be enhanced to see if this item is being added via quote item or price book item
    // Right now it assumes the item is always being added via price book item.
    for (Ticket_Item__c ti: System.Trigger.new) {
         if(ti.Price__c == null)

        if(ti.SyncID__c == null)
         {
             Price_Book_Item__c pli = [SELECT Default_Quantity__c, Included_Quantity__c, Locked_Discount__c, Locked_Price_Markup__c, 
                                              Maximum_Quantity__c, Minimum_Charge__c, Minimum_Quantity__c, Pricing_Method__c, 
                                              Surcharge_Env__c, Surcharge_Fuel__c, Price__c, Catalog_Item__c, DiscountPercent__c,
                                              Sequence_Number__c, Cost_Plus_Markup_Amount__c, Cost_Plus_Markup_Percent__c, Price_Book__c,
                                              Standby_Price__c, Description_Override__c, Catalog_Description__c
                                       FROM Price_Book_Item__c 
                                       WHERE Id = :ti.Price_Book_Item__c limit 1];
             
             Price_Book__c pb = [SELECT Surcharge_Rate_Env__c, Surcharge_Rate_Fuel__c FROM Price_Book__c WHERE Id = :pli.Price_Book__c LIMIT 1];
           
             Catalog_Item__c cat = [SELECT Classification__c, Equipment_Selector__c, Contact_Selector__c, Timespan_Selector__c, Track_Inventory__c, 
                                    UOM__c, Cost__c
                                    FROM Catalog_Item__c
                                    WHERE Id = :pli.Catalog_Item__c LIMIT 1];
                                    
             Ticket__c t = [SELECT Warehouse__c FROM Ticket__c WHERE Id = :ti.Ticket__c LIMIT 1];
             
             //User can never change these values that come from the Catalog on item create                 
             ti.Catalog_Classification__c = cat.Classification__c;
             ti.Catalog_Equipment_Selector__c = cat.Equipment_Selector__c;
             ti.Catalog_Person_Selector__c = cat.Contact_Selector__c;
             ti.Catalog_Timespan_Selector__c = cat.Timespan_Selector__c;
             ti.Catalog_Track_Inventory__c = cat.Track_Inventory__c;
             ti.Catalog_UOM__c = cat.UOM__c;
             
             //User can never change these values that come from the Price Book/Item on item create
             ti.PBI_Default_Quantity__c = pli.Default_Quantity__c;
             ti.PBI_Included_Quantity__c = pli.Included_Quantity__c;
             ti.PBI_Locked_Discount__c = pli.Locked_Discount__c;
             ti.PBI_Locked_Price_Markup__c = pli.Locked_Price_Markup__c;
             ti.PBI_Maximum_Quantity__c = pli.Maximum_Quantity__c;
             ti.PBI_Minimum_Charge__c = pli.Minimum_Charge__c;
             ti.PBI_Minimum_Quantity__c = pli.Minimum_Quantity__c;
             ti.PBI_Pricing_Method__c = pli.Pricing_Method__c;
             ti.PBI_Surcharge_Env__c = pli.Surcharge_Env__c;
             ti.PBI_Surcharge_Fuel__c = pli.Surcharge_Fuel__c;
             ti.Surcharge_Rate_Env__c = pb.Surcharge_Rate_Env__c;    // maybe one day we will let them change surcharge rates
             ti.Surcharge_Rate_Fuel__c = pb.Surcharge_Rate_Fuel__c;  // maybe one day we will let them change surcharge rates
             
             //All of these items below could be overwritten by the end user in the UI 
             //given the right user permissions and config of this item so we check if it has been overriden first
             if(ti.Cost_Plus_Markup_Amount__c == NULL) {
                 ti.Cost_Plus_Markup_Amount__c = pli.Cost_Plus_Markup_Amount__c; }
             
             if(ti.Cost_Plus_Markup_Percent__c == NULL) {
                 ti.Cost_Plus_Markup_Percent__c = pli.Cost_Plus_Markup_Percent__c; }
             
             if(ti.Sequence_Number__c == NULL) {
                 ti.Sequence_Number__c = pli.Sequence_Number__c; }
             
             if(ti.Cost__c == NULL) {
                 ti.Cost__c = cat.Cost__c;}
                 
             if(ti.Discount_Percent__c == NULL) { 
                 ti.Discount_Percent__c = pli.DiscountPercent__c; }
                 
             if(ti.Price__c == NULL) {
                 ti.Price__c = pli.Price__c; }
                 
             if(ti.Standby_Price__c == NULL) {
                 ti.Standby_Price__c = pli.Standby_Price__c; }
                 
             if(ti.Warehouse__c == NULL) {
                 ti.Warehouse__c = t.Warehouse__c; }
                 
             if(ti.Description__c == NULL) {
                 if(pli.Description_Override__c == NULL) {
                     ti.Description__c = pli.Catalog_Description__c; }
                 else { 
                     ti.Description__c = pli.Description_Override__c; }
             }   
         }
    }

}

githubbob42 commented 5 years ago

Story: #263 Schema changes for ticket and job (Mingle)