0

I have two fields.

Cost_price__c & Previous_book_value__c

Currently, I use a Trigger on my Asset object to create a bunch of other objects and related records. However, I need to map the field of Previous_book_value__c to the same as Cost_price__c to work with them both.

I can't use the Previous_book_value__c as a formula field, I need them both to remain as a Currency field.

My plan is to add a second Trigger Context variable to deal with the field before inserting as inserting afterwards won't work. I feel like it's simple and I'm over complicating the entire process.

Could I get some indication on where to get started please?

Many thanks

Code:

trigger CreateProjectfromAsset on Asset (before insert, after insert, after update) {

if(Trigger.isInsert){
    if(Trigger.isAfter){
        AssetCreationController.assetCreation(Trigger.new);
    }
    if(Trigger.isBefore){
      System.debug('Need to set the field value of Previous_book_value__c to be the same as Cost_price__c');
    }

    }

}

if(Trigger.isUpdate ){
    AssetCreationController.assetUpdate(Trigger.new);
}

}

4
  • 2
    You can still create a formula field with a return type as Currency. Looking at your requirements, it seems better to create the field as formula.
    – Jayant Das
    Commented Jan 15, 2019 at 16:24
  • 2
    Honestly if you want the field to be writable, just use a Worfklow Rule Field Update.
    – Adrian Larson
    Commented Jan 15, 2019 at 16:41
  • @JayantDas Appreciate the help, can't use the field as a formula as I'll be manipulating it in the future! Commented Jan 15, 2019 at 16:56
  • @AdrianLarson Have done this, thanks. I must have been making it too difficult and too worried about hitting SOQL limits (I was running pretty close with an API connection) Commented Jan 15, 2019 at 16:58

1 Answer 1

0

Just put this logic in a Workflow Rule Field Update. Using an Apex Trigger for this functionality is overkill, and it's usually better to use declarative functionality over code where it will work.

2
  • That said, I'd avoid mixing Process Builder with any integrations. The error handling is no good.
    – Adrian Larson
    Commented Jan 15, 2019 at 17:03
  • Already encountered Process Builder issues - that caused me to think about doing it directly within Apex but a Workflow Rule seems to fit my requirements! Will add an edit on the original question for those in the future that think the same as me Commented Jan 15, 2019 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .