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

need assistance with apex code

We have a custom object in our sf instance called projects- we use it to track our customer implementations.   When an opportunity is closed, I would like a new project to automatically open and pull over some of the data from the opportunity.  I had hoped I could do this with workflow but I was told I need to use APEX.  I have never written apex code and was hoping someone may have something to share or could point me to some beginner reference material.  Thanks in advance!

Ankit AroraAnkit Arora

This is what all you want :


This contains how you can write apex code. Also you can create trigger for your requirement.



Ankit Arora

Blog | Facebook | Blog Page


This is a sample code I used and got from a community member named Orikker.  It is extremely close to what you are trying to do I think.  The code below spontaneoulsy creates a new custom object record (the object is called "Vehicle") when a new opportunity is created with a certain value in its Type field.  I think you could change after insert below to after update  and change


if (o.Type == 'NS-TO (New Sale - Task Order Contract)') {




if (o.StageName == ''Closed") {


You might need to make other changes but this code I think is pretty close to what you need



trigger createVehicleOnOpportunityX on Opportunity (after insert) {
List <Vehicle__c> vehToInsert = new List <Vehicle__c>  ();  
    // or whatever your custom object name put instead of Vehicle__c
    for (Opportunity o : {
        // here is where you check if opportunity that is being inserted
        //meets the criteria
        if (o.Type == 'NS-TO (New Sale - Task Order Contract)') {
        Vehicle__c v = new Vehicle__c (); //instantiate the object to put values for future record
        // now map opportunity fields to new vehicle object that is being created with this opportunity
        v.Name = o.Name; // and so on so forth untill you map all the fields. 
        //once done, you need to add this new object to the list that would be later inserted. 
        //don't worry about the details for now
        }//end if
    }//end for o
    //once loop is done, you need to insert new records in SF
    // dml operations might cause an error, so you need to catch it with try/catch block.
    try {
        insert vehToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);


please check your personal mailbox,

also provide your custom object field defination and opportunity to custom object fields mapping