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 fields from opportunity object to my custom object i.e opportunity_clone_cc i'm writing below code but getting error

trigger CloneOpp1 on Opportunity( after insert, after update ) {
  Set<String> field = Opportunity.getDescribe().fields.getMap().keySet();
  Map<Id,Opportunity_clone__c> opp = new Map<Id,Opportunity_clone__c>();
  for(Opportunity record: Trigger.new) {
    opp.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.get(field));
        } catch(exception e) { /* NOTE: This just means copy failed. */ }
      }
    }
  }
  update Opportunity_clone__c.values();
}

 
Niraj Kr SinghNiraj Kr Singh
Hi Shivkumar,

can you share the error what you are getting.

Thanks
Niraj
Shivkumar SheteShivkumar Shete
Hi Niraj,
erorr is : Variable does not exist: opportunity_clone__c 
thanks
Shivkumar SheteShivkumar Shete
Hi niraj
I resolved that issue but getting error as Non static method cannot be referenced from a static context: Object opportunity_clone__c.get(String)
Niraj Kr SinghNiraj Kr Singh
Ok.
But this error context is not matching with your script posted above.
Any how this syntax is not proper:
"Opportunity_clone__c.get(record.Opportunity_clone__c)".
This "record.Opportunity_clone__c" will return field value where "get" methos expecting "field api name".
 
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 havw written above code it's not giving error but it is inserting null