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
Afzaal HassanAfzaal Hassan 

Convert Process Builder to trigger

I  had created a Process Builder which was working fine in the lower sandboxes. In production however, it is causing errors and giving the following message: The flow tried to update these records: null. This error occurred: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: pi.LogAccountChange: System.LimitException: Apex CPU time limit exceeded. You can look up ExceptionCode values in the SOAP API Developer Guide [developer.salesforce.com] Flow Details Flow API Name: Opportunity_Recalculate_Rating Type: Record Change Process Version: 4 Status: Active Org: Lifetouch (00D15000000kLl0) Flow Interview Details Interview Label: Opportunity_Recalculate_Rating-5_InterviewLabel Current User: Integration User (005150000073iiO) Start time: 3/22/2019 1:57 AM Duration: 15 seconds How the Interview Started Integration User (005150000073iiO) started the flow interview. Some of this flow's variables were set when the interview started. myVariable_old = Opportunity (0061500000WK2cKAAT) myVariable_current = Opportunity (0061500000WK2cKAAT) ASSIGNMENT: myVariable_waitStartTimeAssignment {!myVariable_waitStartTimeVariable} Equals {!$Flow.CurrentDateTime} Result {!myVariable_waitStartTimeVariable} = "3/22/2019 1:57 AM" DECISION: myDecision Executed this outcome: myRule_1 Outcome conditions: {!formula_myRule_1} (true) Equals true RECORD UPDATE: myRule_1_A1 Find all Opportunity records where: Id Equals {!myVariable_current.Id} (0061500000WK2cKAAT) Update the records’ field values. LPP_Rating__c = {!formula_4_myRule_1_A1_9963437625} (B) Result All records that meet the filter criteria are ready to be updated when the next Screen or Wait element is executed or when the interview finishes. RECORD UPDATE: myRule_1_A2 Find all Account records where: Id Equals {!myVariable_current.AccountId} (0011500001LkYu4AAF) Update the records’ field values. Fall_Rating__c = {!formula_3_myRule_1_A2_9151511649} (B) Result All records that meet the filter criteria are ready to be updated when the next Screen or Wait element is executed or when the interview finishes. RECORD UPDATE: myRule_1_A3 Find all Account records where: Id Equals {!myVariable_current.AccountId} (0011500001LkYu4AAF) Update the records’ field values. Spring_Rating__c = {!formula_2_myRule_1_A3_6475583567} (A) Result Failed to update records that meet the filter criteria.

And this error is just a snippet. It actually gives a page long message with the same thing. After doing research, I found out that PB take twice as long to execute as opposed to Apex triggers. So my goal is to convert this Process Builder to an Apex trigger. Unfortunately, I am not a developer and would love to get some guidance/starting step into converting this PB into Apex trigger. The Process Builder is below: 
User-added imagethe build formula is: AND ( NOT(ISBLANK([Opportunity].LPP_Rating__c )), TEXT([Opportunity].Collection_Status__c) <> "Open", [Opportunity].Retake__c = false, OR ( ISCHANGED([Opportunity].Total_Payments__c), ISCHANGED([Opportunity].Sales_Tax_Rate__c), ISCHANGED([Opportunity].Reorder_Payments__c), ISCHANGED([Opportunity].Total_Production_Value__c), ISCHANGED([Opportunity].Discount__c), ISCHANGED([Opportunity].Children_Photographed__c), ISCHANGED([Opportunity].Commissions_Paid__c) ) )
The imediate action is: 
User-added imageand the value section has this written: 
User-added imageBasically, what this process is doing is that there is a Rating field that gets updated based on the Contribution Margin formula. However, if any component value in the contribution formula changes, then it updates the rating field. I am guessing this will be a before update trigger. Any help would be greatly appreciated. Thank you
Best Answer chosen by Afzaal Hassan
SATHISH REDDY.SATHISH REDDY.
Please mark as the best answer & let me know how it goes.
trigger OpportunityRecalculateRatingTrigger on Opportunity (before Update) {
    for(Opportunity opp : Trigger.New){
        if(String.isNotBlank(opp.LPP_Rating__c) && opp.Collection_Status__c != 'Open' && opp.Retake__c == false){
            if(opp.Total_Payments__c != Trigger.oldMap.get(Opp.Id).Total_Payments__c || opp.Sales_Tax_Rate__c != Trigger.oldMap.get(Opp.Id).Sales_Tax_Rate__c ||
                opp.Reorder_Payments__c != Trigger.oldMap.get(Opp.Id).Reorder_Payments__c || opp.Total_Production_Value__c != Trigger.oldMap.get(Opp.Id).Total_Production_Value__c ||
                opp.Discount__c != Trigger.oldMap.get(Opp.Id).Discount__c || opp.Children_Photographed__c != Trigger.oldMap.get(Opp.Id).Children_Photographed__c ||
                opp.Commissions_Paid__c != Trigger.oldMap.get(Opp.Id).Commissions_Paid__c){
                
                if((opp.Program_Type__c == 'Same Day Proof' || opp.Program_Type__c == 'Family Approval') && opp.Contribution_Margin__c >= 0.62)
                    opp.Rating__c = 'A';
                else if((opp.Program_Type__c == 'Same Day Proof' || opp.Program_Type__c == 'Family Approval') && opp.Contribution_Margin__c >= 0.45 && opp.Contribution_Margin__c < 0.62)
                    opp.Rating__c = 'B';
                else if(opp.Program_Type__c == 'Same Day Proof' && opp.Contribution_Margin__c >= 0.11 && opp.Contribution_Margin__c < 0.45)
                    opp.Rating__c = 'C';
                else if((opp.Program_Type__c == 'Same Day Proof' && opp.Contribution_Margin__c <= 0.1) || (opp.Program_Type__c == 'Family Approval' && opp.Contribution_Margin__c >= 0.0 && opp.Percent_of_Sale__c < 0.3))
                    opp.Rating__c = 'D';
                else if(opp.Program_Type__c == 'Family Approval' && opp.Contribution_Margin__c >= 0.01 && opp.Contribution_Margin__c < 0.45 && opp.Percent_of_Sale__c >= 0.3)
                    opp.Rating__c = 'C';
                else if()
                    opp.Rating__c = 'D';
            }
        }
    }
}

 

All Answers

SATHISH REDDY.SATHISH REDDY.
Hi Afzaal,

1. Can you copy the formula from immediate action here.
2. Do you already have any Apex trigger on Opportunity?

Thanks.
Afzaal HassanAfzaal Hassan
Hi @sathish reddy 
No there are no opportunity triggers yet. This will be the first one. Our org is brand new. The formula is below:
if((TEXT([Opportunity].Program_Type__c) = 'Same Day Proof' || TEXT([Opportunity].Program_Type__c) = 'Family Approval') && [Opportunity].Contribution_Margin__c >= 0.62, 'A', 
if((TEXT([Opportunity].Program_Type__c) = 'Same Day Proof' || TEXT([Opportunity].Program_Type__c) = 'Family Approval') && [Opportunity].Contribution_Margin__c >= 0.45 && [Opportunity].Contribution_Margin__c < 0.62, 'B', 
if(TEXT([Opportunity].Program_Type__c) = 'Same Day Proof' && [Opportunity].Contribution_Margin__c >= 0.11 && [Opportunity].Contribution_Margin__c < 0.45, 'C', 
if(TEXT([Opportunity].Program_Type__c) = 'Same Day Proof' && [Opportunity].Contribution_Margin__c <= 0.1, 'D', 
if(TEXT([Opportunity].Program_Type__c) = 'Family Approval' && [Opportunity].Contribution_Margin__c >= 0.01 && [Opportunity].Contribution_Margin__c < 0.45 && [Opportunity].Percent_of_Sale__c >= 0.3, 'C', 
if(TEXT([Opportunity].Program_Type__c) = 'Family Approval' && [Opportunity].Contribution_Margin__c <= 0.0 || [Opportunity].Percent_of_Sale__c < 0.3, 'D', null))))))
SATHISH REDDY.SATHISH REDDY.
Can you also share the events for which this trigger needs to be fired. Is it "everytime its created or edited"? For me this looks like everytime its edited (After Update)
Afzaal HassanAfzaal Hassan
@satish reddy, this should be a before update trigger because when someone changes the values of those fields in the contribution margin, before saving the record, it would need to reclaulcate the rating and then save
SATHISH REDDY.SATHISH REDDY.
Please mark as the best answer & let me know how it goes.
trigger OpportunityRecalculateRatingTrigger on Opportunity (before Update) {
    for(Opportunity opp : Trigger.New){
        if(String.isNotBlank(opp.LPP_Rating__c) && opp.Collection_Status__c != 'Open' && opp.Retake__c == false){
            if(opp.Total_Payments__c != Trigger.oldMap.get(Opp.Id).Total_Payments__c || opp.Sales_Tax_Rate__c != Trigger.oldMap.get(Opp.Id).Sales_Tax_Rate__c ||
                opp.Reorder_Payments__c != Trigger.oldMap.get(Opp.Id).Reorder_Payments__c || opp.Total_Production_Value__c != Trigger.oldMap.get(Opp.Id).Total_Production_Value__c ||
                opp.Discount__c != Trigger.oldMap.get(Opp.Id).Discount__c || opp.Children_Photographed__c != Trigger.oldMap.get(Opp.Id).Children_Photographed__c ||
                opp.Commissions_Paid__c != Trigger.oldMap.get(Opp.Id).Commissions_Paid__c){
                
                if((opp.Program_Type__c == 'Same Day Proof' || opp.Program_Type__c == 'Family Approval') && opp.Contribution_Margin__c >= 0.62)
                    opp.Rating__c = 'A';
                else if((opp.Program_Type__c == 'Same Day Proof' || opp.Program_Type__c == 'Family Approval') && opp.Contribution_Margin__c >= 0.45 && opp.Contribution_Margin__c < 0.62)
                    opp.Rating__c = 'B';
                else if(opp.Program_Type__c == 'Same Day Proof' && opp.Contribution_Margin__c >= 0.11 && opp.Contribution_Margin__c < 0.45)
                    opp.Rating__c = 'C';
                else if((opp.Program_Type__c == 'Same Day Proof' && opp.Contribution_Margin__c <= 0.1) || (opp.Program_Type__c == 'Family Approval' && opp.Contribution_Margin__c >= 0.0 && opp.Percent_of_Sale__c < 0.3))
                    opp.Rating__c = 'D';
                else if(opp.Program_Type__c == 'Family Approval' && opp.Contribution_Margin__c >= 0.01 && opp.Contribution_Margin__c < 0.45 && opp.Percent_of_Sale__c >= 0.3)
                    opp.Rating__c = 'C';
                else if()
                    opp.Rating__c = 'D';
            }
        }
    }
}

 
This was selected as the best answer
Afzaal HassanAfzaal Hassan
Thank you so much @Satish!!!