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
The new LearnerThe new Learner 

After cloning the record , record id need to populate in another field---urgnt

Hi Experts,

I have small requirement , i need to create custom clone button on opportunity for prospecting record type opportunity record, so when ever i click on that particular button , prospecting record type opportunity need to clone and that respected record id needs to populate in a field called Prospecting_opportunity__c(field will be there in the same opportunity object

Thanks in advance
The new LearnerThe new Learner
Hi Experts,

I have requirment that is there is an Record type called Prospecting and i have custom clone button on the Prospecting record type record which is there in the Opportunity.

When ever i click on that Prospecting record type record a  new record has to create and it need to populate the parent record(original record which i cloned) that id needs to populate in field called 'Prospecting_Opportunity__c'.  i wrote code but its throwing validations rules of another record type can you guys help me pleaes.

Apex class code:

global class customClone
{
    webservice static void cloneAccount(Id acctId) // you can pass parameters
    { 
    
       
      Opportunity acc = [SELECT ID, Name,recordtypeid FROM Opportunity WHERE Id = : acctId];
      
      //Id AccRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('200+').getRecordTypeId();
      Opportunity newacc=acc.clone(false); 
       newacc.Prospecting_Opportunity__c=acc.id;
       newacc.Name = acc.Name +'-'+'Cloned';
       
       insert newacc;
        
          
   
   }
      
    }

Button code:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 
var opptyId='{!Opportunity.Id}'; 
sforce.apex.execute("customClone","cloneAccount",{acctId: opptyId});

Thanks in advance
Arshadulla.ShariffArshadulla.Shariff
Hello aditya,

The Following code will hopefully solve your problem.
global class customClone
{
    webservice static void cloneAccount(Id acctId) // you can pass parameters
    { 
    
       
      Opportunity acc = Database.query('SELECT '+selectStarFromSObject('Opportunity')+' FROM Opportunity Where Id = \''+acctId+'\'');
      Opportunity newacc=acc.clone(false,true);
      system.debug('newacc '+newacc);
       newacc.Prospecting_Opportunity__c=acc.id;
       newacc.Name = acc.Name +'-'+'Cloned';
      insert newacc;
        
          
   
   }
       global static string selectStarFromSObject(String sObjectName){
        Map<String, Schema.SObjectField> fieldMap = schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap();           
        String allFields = '';
        for(String fieldName : fieldMap.keyset()){
            if(allFields == null || allFields == ''){
                allFields = fieldName;
            }else{
                allFields = allFields + ', ' + fieldName;
            }
        }
        return allFields;
    } 
      
    }
please let us know if your still having any issues.
If your problem is solved, please mark the question as solved.

Thanks
Arshad
 
The new LearnerThe new Learner
Hi Arshad,

I am receving button related errors.

User-added image
Arshadulla.ShariffArshadulla.Shariff
Hello aditya,

I have created a custom button which looks like below image and its working fine for me.
User-added image
Please make sure, if the org has namespace.
sforce.apex.execute('namespace.customClone'....
Let me know if you still have further issues.

Thanks
Arshad 
arshadulla.shariff@gmail.com
Arshadulla.ShariffArshadulla.Shariff
Hello aditya,

Did you figure out the issue, let us know the solution.
So that it can help the community .

Thanks.
The new LearnerThe new Learner
no its still showing the problem, kindly tell me how can i insert record static way , i am trying like this to insert the record but i am getting error on button click can you help me 

 public class customClone
{
public static void cloneOpportunity(Id oldopptId) // you can pass parameters
    {           
      Opportunity oldopp= [SELECT ID, Name,recordtypeid FROM Opportunity WHERE Id = : oldopptId];
     
       //Opportunity newopp= oldopp.clone(false,true);
       Opportunity newopp=new Opportunity();
        newopp.name=oldopp.name+'-'+'Cloned';
     //newopp.recordtypeid = oldopp.recordtypeid;
       //newopp.Prospecting_Opportunity__c=oldopp.id;
       //newopp.Name = oldopp.Name +'-'+'Cloned';       
       insert newopp;           
   }
      
    }

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 

{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var newAccid = sforce.apex.execute("customClone","cloneOpportunity",{oldopptId:"{!Opportunity.Id}"}); 

window.location.href="/"+newAccid;
Arshadulla.ShariffArshadulla.Shariff
Hello aditya,

Try replacing your code with below and Test .
/*Custom Button Code*/
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 
var opptyId='{!Opportunity.Id}'; 
var newoppid = sforce.apex.execute("customClone","cloneAccount",{acctId: opptyId});
document.location.href='/'+newoppid;
 
/*APEX CLASS*/
global class customClone
{
    webservice static string cloneAccount(Id acctId) // you can pass parameters
    { 
    
       
      Opportunity acc = Database.query('SELECT '+selectStarFromSObject('Opportunity')+' FROM Opportunity Where Id = \''+acctId+'\'');// [SELECT ID, Name,ClosedDate,Stage,,recordtypeid FROM Opportunity WHERE Id = : acctId];
      Opportunity newacc=acc.clone(false,true);
      system.debug('newacc '+newacc);
       newacc.Prospecting_Opportunity__c=acc.id;
       newacc.Name = acc.Name +'-'+'Cloned';
      insert newacc;
      return newacc.id;   
          
   
   }
       global static string selectStarFromSObject(String sObjectName){
        Map<String, Schema.SObjectField> fieldMap = schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap();           
        String allFields = '';
        for(String fieldName : fieldMap.keyset()){
            if(allFields == null || allFields == ''){
                allFields = fieldName;
            }else{
                allFields = allFields + ', ' + fieldName;
            }
        }
        return allFields;
    } 
      
    }

Hope this will solve your problem.
Thanks