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
ckellieckellie 

How do I fix my trigger to commit the update to my record?

In this trigger, I am looking up the strategic industry based on the market segment in the Industry Definition table. My problem is I cannot commit the resulting industry value to the original record. I have tried to achieve my objective through the admin tools and cannot. How do I save the resulting strategic industry value to customer_product_line_item___c record?

Thank you very much.

trigger IndustrytoAcctOpp on Customer_Product_Line_Item__c (Before Insert, Before Update) {

    Set<Id> iid = new Set<Id>();
    Set<Id> aid = new Set<Id>();
    Set<Id> oid = new Set<Id>();
    String Industry;
    String Marketsegment;
    string StrategicIndustry;
   
    For(Customer_Product_Line_Item__c cpli : trigger.new){
        iid.add(cpli.id);
        aid.add(cpli.Account__c);
        oid.add(cpli.Opportunity__c);
        MarketSegment = cpli.industry_segment__c;
        system.debug('##########marketsegment:'+ marketsegment);
    }

    List<Industry_Definition__c> idlist = new List<Industry_Definition__c> ([select id, Market_segment__c, Strategic_Industry__c from Industry_Definition__c]);
   
    Map<string, Industry_Definition__c> imap = new Map<string, Industry_Definition__c>();
   
    for(Industry_Definition__c i : [select id, Market_Segment__c, Strategic_Industry__c from Industry_Definition__c]){
        imap.put(i.Market_Segment__c, i);
        system.debug('##########i.Market_segment__c:'+ i.market_segment__c);
    }
                     

    For(Customer_Product_Line_Item__c cpl : [select id, Bulk_Density__c, Industry_Segment__c, Strategic_Industry__c from Customer_Product_Line_Item__c where id=:iid]){
        system.debug('##########imap.get(cpl.industry_Segment__c):'+ cpl.industry_Segment__c);
        system.debug('##########imap.get(cpl.industry_Segment__c).Strategic_Industry__c:'+imap.get(cpl.industry_Segment__c).Strategic_Industry__c);
        cpl.Strategic_Industry__c = imap.get(cpl.industry_Segment__c).Strategic_Industry__c;

    }

}
Best Answer chosen by ckellie
Damien Phillippi033905702927186443Damien Phillippi033905702927186443
You don't need those insert or update statements in your code that the above suggested.

Your issue is that you are requering for the data in the Trigger.new instead of setting it directly on the records contained in Trigger.new.

Basically the following query is trying to update the wrong data.  You should instead be looping over Trigger.new: [select id, Bulk_Density__c, Industry_Segment__c, Strategic_Industry__c from Customer_Product_Line_Item__c where id=:iid]. 

All Answers

Vinita_SFDCVinita_SFDC
Hello,

I do not see any update or insert statement in your code. Please include update/insert statement to commit calculated changes to the record.
Damien Phillippi033905702927186443Damien Phillippi033905702927186443
You don't need those insert or update statements in your code that the above suggested.

Your issue is that you are requering for the data in the Trigger.new instead of setting it directly on the records contained in Trigger.new.

Basically the following query is trying to update the wrong data.  You should instead be looping over Trigger.new: [select id, Bulk_Density__c, Industry_Segment__c, Strategic_Industry__c from Customer_Product_Line_Item__c where id=:iid]. 
This was selected as the best answer
ckellieckellie
Thank you for your help. I made the trigger work with the following for loop.
for( Integer i = 0; i < Trigger.new.size(); i++){
        trigger.new[i].Bulk_density__c = imap.get(trigger.new[i].industry_Segment__c).Strategic_Industry__c;

    }