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
Vidhyasagar RanganathanVidhyasagar Ranganathan 

Update custom object with opportunity data

Hi All,

I am looking for some help for an one off apex class that would update the opportunity details in custom object. The custom object has the opportunity ID.

Thank you.
Abdul KhatriAbdul Khatri
Why you want to duplicate the information on the two object. Since the custom object has a relationship with Opportunity, you can use formula fields to see the information from Opportunity on the custom object layout.
PawanKumarPawanKumar
Please see below code:

-------------------
trigger UpdateOpty on YouCustomObject (after update) {
    List <Opportunity> oppToUpdate = new List <Opportunity> ();
    for (YouCustomObject eachRecordOfCustomObject : Trigger.new) {
    
        // you can put any criteria to filter
        if (eachRecordOfCustomObject.SAL__c==True) {    
            Opportunity o = new Opportunity ();   
            o.Name = m.Name;
            o.StageName = 'Generate Opportunity';
            o.Type = 'Sales - New Business';
            o.CloseDate = System.Today()+150;
            oppToUpdate.add(o);
        }// end if
        
    }// end for
    
    if(!oppToUpdate.isEmpty()){
        update(oppToUpdate);
    }
}// end trigger

--------------------

If you want to update custom object from opportunity then you can write a trigger on the opportunity and do the same as above.

Please mark it best if it helps you. Thanks in advance.

Regards,
Pawan Kumar