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
Taresh PandeyTaresh Pandey 

How to retrieve records from sObject without using Apex controller and trigger ?

Hello all,
    I'm new on salesforce technology.I wanna retrieve some records from sObject and I wanna insert those retrieved records into a custome object. without using Apex controller and trigger. How can I perform this task. I'll  eagerly wait for yours' reply.
  
thank you all in advance 
Best Answer chosen by Taresh Pandey
Abhishek BansalAbhishek Bansal
Hi Taresh,

Please find below your required script :

List<Opportunity> allOps = new List<Opportunity>([Select Address__c,Contact__c,Email_ID__c,Employee_ID__c,Employee_Name__c,Field_Type__c from Opportunity]);

List<Aplite_work__c> insertApliteWorkList = new List<Aplite_work__c>();
Aplite_work__c apliteWork;

for(Opportunity opp : allOps){
    apliteWork = new Aplite_work__c();
    apliteWork.Address__c = opp.Address__c;
    apliteWork.Contact__c = opp.Contact__c;
    apliteWork.Email_ID__c = opp.Email_ID__c;
    apliteWork.Employee_ID__c = opp.Employee_ID__c;
    apliteWork.Employee_Name__c = opp.Employee_Name__c;
    apliteWork.Field_Type__c = opp.Field_Type__c;
    
    insertApliteWorkList.add(apliteWork);
}

if(insertApliteWorkList.size() > 0){
    insert insertApliteWorkList;
}



Copy and paste this code into Execute Anonymous Window in Developer Console of your salesforce org.

Note : If you have large number of opportunity records than please avoid using this script and go with the suggestion provided by Lukasz.

Please let me know if you need more help on this.

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Taresh,

If you do not want to use any apex class and trigger than this should be a one time process.
You can achieve this by writing a script and running that script on Developer Console.
Please specify the Sobject from which you want to retrieve the records and also the Custom Object in which you want to insert the records.
Also specify the fields which should be populated in custom object records from retreived Sobejct.

Let me know the answers so that i can help you in writing a script.

Thanks,
Abhishek
Łukasz BieniawskiŁukasz Bieniawski
Hi Taresh,

You can use Data Loader to export rows from object to csv file.
Next, when exported, adjust rows and columns in csv file by using i.e. Notepad++ or other editor to import into new object, by using Data Loader also.

You can read about Data Loader more here (https://developer.salesforce.com/page/Data_Loader#Other_loading_options).

Hope that helps,
Lukasz
Taresh PandeyTaresh Pandey
Hey Abhishek,

Thank you for your quick reply. sObject is opportunity and my custom object is Aplite_work__c and fields in this object are [Address__c(data type 'text'), Contact__c(data type 'phone'), Email_ID__c(data type 'Email'), Employee_ID__c (data type 'auto number'), Employee_Name__c (data type 'text'), Field_Type__c (data type 'text') ]. 
Taresh PandeyTaresh Pandey
Hi Łukasz 

thank you. I'll definatly use your idea while performing tasks.
 
Abhishek BansalAbhishek Bansal
Hi Taresh,

Please find below your required script :

List<Opportunity> allOps = new List<Opportunity>([Select Address__c,Contact__c,Email_ID__c,Employee_ID__c,Employee_Name__c,Field_Type__c from Opportunity]);

List<Aplite_work__c> insertApliteWorkList = new List<Aplite_work__c>();
Aplite_work__c apliteWork;

for(Opportunity opp : allOps){
    apliteWork = new Aplite_work__c();
    apliteWork.Address__c = opp.Address__c;
    apliteWork.Contact__c = opp.Contact__c;
    apliteWork.Email_ID__c = opp.Email_ID__c;
    apliteWork.Employee_ID__c = opp.Employee_ID__c;
    apliteWork.Employee_Name__c = opp.Employee_Name__c;
    apliteWork.Field_Type__c = opp.Field_Type__c;
    
    insertApliteWorkList.add(apliteWork);
}

if(insertApliteWorkList.size() > 0){
    insert insertApliteWorkList;
}



Copy and paste this code into Execute Anonymous Window in Developer Console of your salesforce org.

Note : If you have large number of opportunity records than please avoid using this script and go with the suggestion provided by Lukasz.

Please let me know if you need more help on this.

Thanks,
Abhishek Bansal.
This was selected as the best answer
Taresh PandeyTaresh Pandey
Hello Abhishek,

Now how to write testclass for the same program ?? 
Abhishek BansalAbhishek Bansal
Since you are running this code on Developer Console, so you do not require any test class for it.
 
Taresh PandeyTaresh Pandey
I'm agree with your statement but my senior has given me the task to write the test class for the programm. And the fact is that- I haven't written tast class for any programm before today.
Abhishek BansalAbhishek Bansal
Taresh,

Test classes are written for either Apex Classes or Apex trigger and in your case there is neither a trigger nor a class.
So please let me know how can we write a test class for a component which does not exist.
Please consult with your senior and clear all your doubts.
Abhishek BansalAbhishek Bansal
Taresh if your question has been answered properly than plesae mark this as Solved, In order to avoid confusion to others.
Taresh PandeyTaresh Pandey
Yah ! sure Abhishek .
Taresh PandeyTaresh Pandey
Abhishek How to mark question as solved ??