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
Shivkumar SheteShivkumar Shete 

i want to copy all records from standard object to custom object i have written below trigger

trigger CloneOpp1 on Opportunity( after insert, after update ) {
  Set<String> fields = Opportunity.getSobjectType().getDescribe().fields.getMap().keySet();

  Map<Id,opportunity_clone__c> co2 = new Map<Id,Opportunity_clone__c>();
  for(Opportunity record: Trigger.new) {
    co2.put(record.opportunity_clone__c,new opportunity_clone__c(id=record.opportunity_clone__c));
    for(String field:fields) {
      if(field.indexOf('__c')>-1) { // This is a custom field.
        try {
          Opportunity_clone__c.get(record.opportunity_clone__c).put(field,record.getSObject(field));
        } catch(exception e) { /* NOTE: This just means copy failed. */ }
      }
    }
  }
  update co2.values();
}

but getting error as variable does not exist :opportunity_clone_cc
RakeshRakesh (Salesforce Developers) 
HI 

I'd suggest storing the record ID for Opp on your object and the Record ID for your custom object on the Opportunity. If the custom object is a child to the opportunity you could actually just use a trigger to insert a new record related to the opportunity, then use formula fields to map any/Opportunity fields to your custom object. This would give it a real-time sync, so you'd never have to have additional triggers running if the opportunity is updated.

The only drawback is if your users will be needing to make updates to the custom object, which in turn updates the opportunity. In that case, if it's a master-detail relationship, standard WF rules can update Opportunity fields, and a trigger would update the custom object. Your triggers will be MUCH simpler if after the new custom object record is inserted, you update the opportunity with the recordID, so subsequent update triggers can simply use that recordID. You could use a standard WF rule to do the initially recorded update to the Opp.

Hope this gives some way.

Please mark it as solved if my reply was helpful.

Thanks,
Rakesh
 
Shivkumar SheteShivkumar Shete
trigger CloneOpp on Opportunity( after insert, after update ) {
  Set<String> fields = Opportunity.getSobjectType().getDescribe().fields.getMap().keySet();

  Map<Id,opportunity_clone__c> co2 = new Map<Id,Opportunity_clone__c>();
  for(Opportunity record: Trigger.new) {
    co2.put(record.opportunity_clone__c,new opportunity_clone__c(id=record.opportunity_clone__c));
    for(String field:fields) {
      if(field.indexOf('__c')>-1) { // This is a custom field.
        try {
         
            co2.get(record.opportunity_clone__c).put(field,record.getSObject(field));
        } catch(exception e) { }
      }
    }
  }
    System.debug('------------------>'+co2);
  insert co2.values(); 
}
i have written above code not getting any error but its not working