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
AprilRzAprilRz 

Trigger help: New detail record when change is made to the Opportunity

Hello everyone. I am trying my hand at my first trigger and was wondering if anyone could help start me off.

What I want to do is create a new custom object record if any of a set of fields on the opportunity are changed. The custom object is a "Provisioning Change", basically a ticket for our provisioning department, to let them know that something relevant has changed.
I would like to be able to list or link the fields that have changed, and the prior and new values. Like a replica of the history object, basically.

Any help would be much appreciated! Thanks!

VKrishVKrish

This is not exact code.. just a sample

trigger myTrigger on Opportunity(before update){
  List<CustomObject__c> coList = new List<CustomObject__c>();
  for(Opportunity o : Trigger.new){
    // provide values for all the necessary fields
    CustomObject__c co = new CustomObject__c();
    co.name = 'xxx';
    co.updateTime__c = system.now();
    co.field__c = 'customField__c';
    co.oldValue__c = trigger.oldMap.get(c.Id).customField__c;
    ....
    coList.add(co);
  }
  insert coList;
}

 

AprilRzAprilRz

What does the 'custom_field__c' on Line 9 refer to?

VKrishVKrish

Its just a sample custom field name I gave. You can specify what ever custom field in your custom object there.