失败消息:“System.QueryException:实体'Lead'上没有这样的列'Address'。我

时间:2014-05-21 12:55:46

标签: triggers salesforce apex-code apex

  

我正在通过沙盒中的salesforce apex传递所有测试覆盖率,但是在部署到实时获取   主题中的错误,我不知道如何处理它。        我的测试类代码是:

@isTest(SeeAllData=true)
public class myLeadTest{ 

 public static testMethod void testMyController5() {

                Account acc= new Account(name='test');
                acc.Order__c='wee';
                insert acc;

                Campaign camp = new Campaign(Name='test');
                insert camp;

                Company__c cm= new Company__c();
                cm.Company_Id__c=12;
                cm.Name='234';
                cm.Account__c=acc.id;
                insert cm;


                Lead l= new Lead();
                l.Campaign__c=camp.Id;
                l.LastName='test';
                l.Company='cmpny';
                l.Company_Id__c=cm.Company_Id__c;
                l.Email='test@yahoo.com';
                l.Status='Closed - Converted';


                l.Order__c=acc.Order__c;


               // l.Auto_Convert__c=true;
               // l.IsConverted=false;

                //l.ConvertedAccountId=acc.Id;
                Lead[] l2= new Lead[]{l};
                insert l2;

                Opportunity op= new Opportunity ();
                op.StageName='Won (Agency)';
                op.Name='test';
                op.CloseDate=Date.valueOf('2015-04-28');
                op.AccountId=acc.id;
                op.LeadSource='web';
                op.StageName= 'Won (Agency)';
                op.CampaignId=camp.id;
                op.TrialStart__c=Date.valueOf('2012-04-28');  
                insert op;

                Contact cnt= new Contact();
                cnt.LastName='test';
                cnt.AccountId=acc.id;
                cnt.ASEO_Company_Id__c=12;
                cnt.Email=l.Email;
                cnt.Company__c=cm.id;
                insert cnt;

                ///////////////
            /*    Lead l5= new Lead();
                l5.Campaign__c=camp.Id;
                l5.LastName='test';
                l5.Company='cmpny';
                l5.Company_Id__c=12;
                l5.Email='test@yahoo.com';
                l5.Status='Closed-Converted';
                l5.isConverted=false;
                //l.ConvertedAccountId=acc.Id;              
                insert l5; */
                Contact cnt1= new Contact(); 
                cnt1.LastName='test';
                cnt1.AccountId=acc.id;
                cnt1.ASEO_Company_Id__c=12;
                cnt1.Email=l.Email;  
                insert cnt1;                
                Database.LeadConvert leadConvert = new Database.LeadConvert();
                  leadConvert.setLeadId(l2[0].Id);
                  LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
                  leadConvert.setConvertedStatus(convertStatus.MasterLabel);
                  leadConvert.setOpportunityName(op.Name);
                  leadConvert.setConvertedStatus('Closed - Converted');
                  Database.LeadConvertResult lcResult = Database.ConvertLead(leadConvert);
                //  MyLead.addCampaign(l2[0], camp.Name);
                //  MyLead.updateOpportunity(l2[0], lcResult);

              //   MyLead.updateContact( l2[0], lcResult, cm) ;            
               //  myLead.addCampaignMembers(l2);


                }

}
  

和我在触发器中调用的类代码:

public class MyLead {
    public static void convertLead(Lead[] leads) {
      Database.LeadConvert leadConvert = new Database.LeadConvert();
      for (Lead lead :leads) {
        if (!(lead.IsConverted)) {
          // Check if an account exists with email. Attach Contact and Account, convert Lead.
          Contact[] myContacts = [SELECT Id, AccountId FROM Contact WHERE Email = :lead.Email Limit 1];
          if (myContacts.size() == 1) {
            System.debug('Existing Contact');
            MyLead.attachExistingConvert(lead, myContacts[0]);
          }
          else {
            System.debug('No existing contact');
            if (lead.Auto_Convert__c) {

             MyLead.addCampaign(lead, lead.Campaign__c);

              leadConvert.setLeadId(lead.Id);
              leadConvert.setConvertedStatus('Closed - Converted');
              Database.LeadConvertResult lcResult = Database.ConvertLead(leadConvert);

              // Populate the newly created records with extra data.
              MyLead.updateOpportunity(lead, lcResult);
              Company__c myCompany = [SELECT Id FROM Company__c WHERE Company_Id__c = :lead.Company_Id__c];
              MyLead.updateContact(lead, lcResult, myCompany);
              // Must update the Company before Account otherwise the Account lookup will fail.
              MyLead.updateCompany(lead, lcResult, myCompany);
              MyLead.updateAccount(lead, lcResult, myCompany);
            }
          }
        }
      }
    }

    // Attaches Lead to Contact and Account if matched by Email, converts Lead and creates opportunity if it doesn't exist.
    public static void attachExistingConvert(Lead lead, Contact contact) {
        MyLead.addCampaign(lead, lead.Campaign__c);
        Database.leadConvert leadConvert = new Database.LeadConvert();
        leadConvert.setLeadId(lead.Id);
        leadConvert.setConvertedStatus('Closed - Converted');
        leadConvert.setContactId(contact.Id);
        leadConvert.setAccountId(contact.AccountId);
        leadConvert.setSendNotificationEmail(True);
        // Do not create an opportunity if any exist.
        Opportunity[] myOpportunity = [SELECT Id FROM Opportunity WHERE AccountId = :contact.AccountId];
        if (myOpportunity.size() > 0) {
          leadConvert.setDoNotCreateOpportunity(True);
        }
        leadConvert.setLeadId(lead.Id);
        Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvert);
        // If we created an opportunity, update it with extra stuff.
        if (leadConvert.isDoNotCreateOpportunity() == False) {
          MyLead.updateOpportunity(lead, leadConvertResult);
        }
    }

    // Updates the newly created Opportunity with extra data
    public static void updateOpportunity(Lead lead, Database.LeadConvertResult lcResult) {
      Opportunity myOpportunity = [SELECT Id, StageName FROM Opportunity WHERE Id = :lcResult.opportunityId];
      myOpportunity.Name = 'Online Upgrade';
      myOpportunity.TrialStart__c = lead.CreatedDate.date();
      //myOpportunity.Amount = lead.Monthly_Revenue__c;
      myOpportunity.Amount = lead.Amount__c;
      myOpportunity.Type = 'New Business';
      myOpportunity.LeadSource = 'Website';
      myOpportunity.Order__c = lead.Order__c;
      DateTime dt = System.now();
      Date d = dt.date(); 
      myOpportunity.CloseDate = d;
      myOpportunity.StageName = lead.Agency__c ? 'Won (Agency)' : 'Won (End User)';
      myOpportunity.Probability = 100;

      update myOpportunity;
    }

    // Updates the newly created Account with extra data 
    public static void updateAccount(Lead lead, Database.LeadConvertResult lcResult, Company__c myCompany) {
      Account myAccount = [SELECT Id FROM Account WHERE Id = :lcResult.accountId];
      myAccount.Type = 'Customer';
      myAccount.ASEO_Company_Id__c = lead.Company_Id__c;
      myAccount.Company__c = myCompany.Id; // Setting the company will cause the company account lookup to fail
      myAccount.Order__c = lead.Order__c;

      update myAccount;
    }

    // Updates the newly created Contact with extra data 
    public static void updateContact(Lead lead, Database.LeadConvertResult lcResult, Company__c myCompany) {
      Contact myContact = [SELECT Id FROM Contact WHERE Id = :lcResult.contactId];
      myContact.ASEO_Company_Id__c = lead.Company_Id__c;
      myContact.Company__c = myCompany.Id;

      update myContact;
    }

    // Updates the Company with extra data 
    public static void updateCompany(Lead lead, Database.LeadConvertResult lcResult, Company__c myCompany) {
      myCompany.Account__c = lcResult.accountId;

      update myCompany;
    }

    // Adds a lead to a campaign
    public static void addCampaign(Lead lead, String campaign) {

      Campaign[] myCampaigns = [Select Id FROM Campaign WHERE Name = :campaign LIMIT 1];
      if (myCampaigns.size() == 1) {
        CampaignMember campaignMember = new CampaignMember (campaignId=myCampaigns[0].Id, leadId=lead.Id);
        insert campaignMember;
      }
    }
    // Create campaign members from leads. Campaign comes from lead.Campaign__c.
    public static void addCampaignMembers(Lead[] leads) {
      for (Lead lead :leads) {
        // Might want to query Campaigns table instead.
        //Set<String> campaigns = new Set<String>();
        //campaigns.add('Freemium');
        Campaign[] myCampaigns = [SELECT Id FROM Campaign WHERE Name = :lead.Campaign__c LIMIT 1];

        //if (campaigns.contains(lead.Campaign__c)) {
        if (myCampaigns.size() == 1) {
          MyLead.addCampaign(lead, lead.Campaign__c);
        }
      }
     } 

}
  

我的主要触发器是:

trigger LeadTrigger on Lead (after insert, after update) {
    Lead[] leads = Trigger.new;
    //MyLead.createOpportunity(leads);
    MyLead.convertLead(leads);

Please any one can help me, i tries 25 times but in deploying to production  give me error in the given subject, or any one give code coverage of this class,  thank you in advance

2 个答案:

答案 0 :(得分:0)

看起来问题是基于您的代码中未包含的soql查询。您尝试查询名为“地址”的字段,但此字段不存在。地址仍在测试中......

尝试调整您的查询并使用字段City,Street,Country和PostalCode。

您可以在那里了解有关sObject字段的更多信息:http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_lead.htm

答案 1 :(得分:0)

以防其他人在遇到此问题时提出这个问题。

正如Christian所说,代码正试图访问&#34;地址&#34; SOQL查询中的Lead字段。从API版本30开始,复合字段可用于查询(http://www.salesforce.com/us/developer/docs/api/Content/compound_fields_address.htm)。

因此,如果您在代码中遇到查询问题,请检查该类的API版本和visualforce页面。两者都必须是30或更高。