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
pushpendra yadav 16pushpendra yadav 16 

Create Lead for all the Days that have passed for this year with a format "CA-Lead-{MM}{DD}{YYYY}" i.e; CA-Lead-01012018 as LastName in apex.

Best Answer chosen by pushpendra yadav 16
Ajay K DubediAjay K Dubedi
Hi Pushpendra,
Please refer to below code.

    List<Lead> listOfLead = new List<Lead>();
    date startDate = date.parse('01/01/2018');
    Date endDate = System.Today();
    for(dateTime d=startDate ; d<=endDate;  d=d.addDays(1)){
        string dateFormate = d.formatGmt('MM'+'dd'+'yyyy');
        system.debug(dateFormate);
        Lead ld = new Lead();
        ld.LastName ='CA-Lead-'+dateFormate;
        ld.Company='CA-Company-'+dateFormate;
        ld.Status='Working-Contacted';
        listOfLead.add(ld);
    }
    if(listOfLead.size()>0){
        insert listOfLead;
        System.debug('list of leads'+listOfLead);
    }

Please mark it as best if you find it helpful.

Thank You
Ajay Dubedi

All Answers

Raj VakatiRaj Vakati
can you give me more insights how do you trying to achieve? i mean trying to update the existing data or you want to do it with the trigger for new records?

You can do it even with the process builder and trigger 

 
trigger Lead1 on Lead (before insert) {
    
    for( Lead l : Trigger.new){
        l.LastName ='CA-Lead'+''+System.today().day() +''+System.today().month()+''+System.today().year(); 
        
    }
    
    
}

 
Ajay K DubediAjay K Dubedi
Hi Pushpendra,
Please refer to below code.

    List<Lead> listOfLead = new List<Lead>();
    date startDate = date.parse('01/01/2018');
    Date endDate = System.Today();
    for(dateTime d=startDate ; d<=endDate;  d=d.addDays(1)){
        string dateFormate = d.formatGmt('MM'+'dd'+'yyyy');
        system.debug(dateFormate);
        Lead ld = new Lead();
        ld.LastName ='CA-Lead-'+dateFormate;
        ld.Company='CA-Company-'+dateFormate;
        ld.Status='Working-Contacted';
        listOfLead.add(ld);
    }
    if(listOfLead.size()>0){
        insert listOfLead;
        System.debug('list of leads'+listOfLead);
    }

Please mark it as best if you find it helpful.

Thank You
Ajay Dubedi
This was selected as the best answer