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 

Compile Error: Variable does not exist

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 {
          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();
}

 
Amit Chaudhary 8Amit Chaudhary 8
Make sure you have all field same in Opportunity and opportunity_clone__c.

Can you please let us know on which line you are getting error
Shivkumar SheteShivkumar Shete
Hi Amit,
I'm getting error on line no 6 &10 i.e.Variable does not exist
Amit Chaudhary 8Amit Chaudhary 8
Do you have opportunity_clone__c field on Opportunity object ?
 
Shivkumar SheteShivkumar Shete
HI,
I'm not clear about your answer could you explain in detail 
v varaprasadv varaprasad
Hi ShivKumar,

Please try once below code : 
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) {
  Opportunity_clone__c oppclone = new Opportunity_clone__c();
  oppclone.id = record.Opportunity_clone__c;  
    opp.put(record.Opportunity_clone__c,oppclone);
    for(String field:fields) {
      if(field.indexOf('__c')>-1) { // This is a custom field.
        try {
          opp.get(record.Opportunity_clone__c).put(field,record.get(field));
        } catch(exception e) { /* NOTE: This just means copy failed. */ }
      }
    }
  }
  update opp.values();
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
Shivkumar SheteShivkumar Shete
Hi,
It's not working giving lots of errors