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
Tarun KaushikTarun Kaushik 

Getting start with salesforce

Hi All

Newbie... so please be gentle.

Trying to learn coding by wrtiig classes with small scenarios in salesforce.
I have a custome object which have mendatory fields and Id of its records in a custome field,I want to create new opportunity records through a apex bacth class which will run daily and create all the new records from the custom objects.

What can be basic approach to start coding for this class.

 
pconpcon
Batch / scheduled apex is not the easiest place to start, but hopefully I can give you some resources that will help you.  I would take a look at this blog post [1] as well as the documentation on batch [2] and scheduled [3] apex.  You may also want to look at relax [4] if you start making this any more complicated.

Is there any reason you want to do this as a batch instead of a trigger when the object is created?

[1] http://blog.deadlypenguin.com/blog/2012/05/26/scheduled-actions-in-salesforce-with-apex/
[2] https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
[3] https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm
[4] https://github.com/zachelrath/Relax
Tarun KaushikTarun Kaushik
Hi pcon
I have written the below class for creating new opportunity can you please help to write the trigger which can be used to insert opprtunity records from my custom object
Nextgen__c -custom objects which contains all menadatory fields to create a new oppty

public class Create_oppty{
    List<Nextgen__c> Nextgenlist = new List<Nextgen__c>();

    public Create_oppty(){
        Nextgenlist = [select Name,Close_Date__c,Record_Id__c,Stage__c from Nextgen__c];
        system.debug(Nextgenlist);
    }
    public Void InsertOppty(){
        List<Opportunity> Opptylist = new List<Opportunity>();
        for(Nextgen__c obj:Nextgenlist){
            Opportunity objOpp=new Opportunity();
                objOpp.Name=obj.Name;
                objOpp.CloseDate=obj.Close_Date__c;
                objOpp.StageName=obj.Stage__c;
            Opptylist.add(objOpp);
        }
        if (Opptylist.size()>0) {
            Insert Opptylist;
        }
    }
}
pconpcon
You trigger would look like
 
trigger CreateOppFromNextGen on Nextgen__c (after create) {
    List<Opportunity> oppToInsert = new List<Opportunity>();

    for (Nextgen__c nextgen : Trigger.new) {
        oppToInsert.add(new Opportunity(
            Name = nextgen.Name,
            CloseDate = nextgen.Close_Date__c,
            StageName = obj.Stage__c
        ));
    }

    if (!oppToInsert.isEmpty()) {
        insert oppToInsert;
    }
}

This will create a new Opportunity for each Nextgen that is created.