0

Can you please check weather this statement is correct or not, I am getting error

String query = 'SELECT id'+str+' FROM Lead)';
List<Lead> leadList = database.query(query);
class ConvertLead 
{
   static void Conversion()
    {
        Schema.DescribeSObjectResult a_desc = Lead.sObjectType.getDescribe(); 
        Map<String, Schema.SObjectField> a_fields = a_desc.fields.getMap();
        List<String> str = new List<String>();
        for(Schema.sObjectField fld:a_fields.values()){
            str.add(','+fld);
            //system.debug(fld);
        }
        String query = 'SELECT id'+str+' FROM Lead)';
        List<Lead> leadList = database.query(query);

        for(Lead sObjLead : leadList){
            //New Account is Created
            Account acc = new Account();
            acc.name = sObjLead.name;
            acc.Industry = sObjLead.Industry;
            acc.AnnualRevenue = sObjLead.AnnualRevenue;
            acc.Rating = sObjLead.Rating;
            acc.Phone = sObjLead.Phone;
            acc.Fax = sObjLead.Fax;
            acc.Website = sObjLead.Website;
            acc.NumberOfEmployees = sObjLead.NumberOfEmployees;

            //New Opportunity is Created
            Opportunity opp = new Opportunity();
            opp.name = sObjLead.name;
            opp.Account.Name = acc.Name;
            opp.LeadSource = sObjLead.LeadSource;

            //New Contact is Created
            Contact con = new Contact();
            con.Lastname = sObjLead.name;
            con.Account.Name = acc.Name;
            con.Title = sObjLead.Title;
            con.LeadSource = sObjLead.LeadSource;
            con.Phone = sObjLead.Phone;
            con.MobilePhone = sObjLead.MobilePhone;
            con.Fax = sObjLead.Fax;
            con.Email = sObjLead.Email;
            insert acc;
            insert opp;
            insert con;
        }
    }
}
1
  • Please edit your post to include the error message verbatim and its location.
    – David Reed
    Jul 30 '19 at 13:50
3

The trailing ) should not be in your query.

    String query = 'SELECT id'+str+' FROM Lead';

Note that you also have other problems, such as trying to query all Lead records in the database, and having DML operations in a loop. You'll need a way to specify a Lead Id to convert just one record, and you need to use Database.convertLead to correctly convert leads.

1
  • I tried Database.convertLead and this is working fine and Button is converting Lead. Thanks to You
    – SFDC_M
    Jul 30 '19 at 14:31
1
class LeadConversion
{
    Public Lead leadObj;
    Public Id leadId;
    public LeadConversion(ApexPages.StandardController stdController)
    {
        leadId = ApexPages.currentPage().getParameters().get('id');
        leadObj = (leadId == Null) ? new Lead():[SELECT Name FROM lead WHERE Id =: leadId];
    }
    public PageReference autoConvert()
    {
        Database.LeadConvert convertLead = new database.LeadConvert();
        convertLead.setLeadId(leadId);
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        convertLead.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult convertLeadResult = Database.convertLead(convertLead);
        Id accountId = convertLeadResult.getAccountId();
        PageReference Page = new PageReference('https://playful-raccoon-a2tqu9-dev-ed.my.salesforce.com/'+accountId);
        Page.setRedirect(true);
        return Page;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.