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
louisa barrett 7louisa barrett 7 

Apex trigger to update field

Hi,

I was wondering if anyone could offer some advice whether the below trigger is sufficent or whether additonal checks or improvements are required.

trigger UpdatePurchaseOrderNumber on Opportunity (before update) {
    for(Opportunity opp:trigger.new){
        opp.Purchase_Order_No__c = opp.Sales_Order_Number__C;
    }
}

Basically, I need to simply copy the Sales_Order_Number field to the Purchase Order No field before any validations are performed on the Opportunity when the Save is pressed. The purchase order has a validation rule set on it that it cannot be null when the Opportunity is saved, so I cannot use a workflow.
Thanks
Best Answer chosen by louisa barrett 7
BM 5BM 5
trigger UpdatePurchaseOrderNumber on Opportunity (before update) {
    for(Opportunity opp:trigger.new){
        opp.Purchase_Order_No__c = opp.Sales_Order_Number__C;
        update opp;
    }
}

All Answers

BM 5BM 5
trigger UpdatePurchaseOrderNumber on Opportunity (before update) {
    for(Opportunity opp:trigger.new){
        opp.Purchase_Order_No__c = opp.Sales_Order_Number__C;
        update opp;
    }
}
This was selected as the best answer
louisa barrett 7louisa barrett 7
Sorry, I forgot the update part. Everything else is ok then? Many thanks Sent from Samsung Mobile on O2