+ Start a Discussion
PudbrookPudbrook 

Map Custom Lead Field to Account Type

Hey

I have created a trigger to map my custom lead "Type" field to the AccountType field. I finally got the trigger to save, however when I try to convert a lead record and create a new account record I recevie the following error:

Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, accountType: execution of BeforeInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Lead.ConvertedAccountId Trigger.accountType: line 9, column 1: [] (System Code)

Coul really use some help solving. Also the trigger is below:

trigger accountType on Account (before insert){
  Set<Id> accts = new Set<Id>();
  FOR(Account a: trigger.new){
     accts.add(a.Id);
  }
   List<Lead> leads=[SELECT Id, Type__c FROM Lead WHERE convertedAccountId IN: accts];
  Map<Id,String> leadMap=new Map<Id,String>();
  FOR(Lead l: leads){
   leadMap.put(l.ConvertedAccountID,l.Type__c);
  }
  FOR(Account a2: trigger.new){
    IF(leadMap.containsKey(a2.Id)){
      a2.Type=leadMap.get(a2.Id); 
    }
  }
}
Best Answer chosen by Pudbrook
Shruthi Javare GowdaShruthi Javare Gowda
Hi

Replace the query..... 

List<Lead> leads=[SELECT Id, Type__c, convertedAccountId  FROM Lead WHERE convertedAccountId IN: accts];

It should work..

All Answers

Shruthi Javare GowdaShruthi Javare Gowda
Hi

Replace the query..... 

List<Lead> leads=[SELECT Id, Type__c, convertedAccountId  FROM Lead WHERE convertedAccountId IN: accts];

It should work..
This was selected as the best answer
Ramakrishnan AyyanarRamakrishnan Ayyanar
Hi ,

Query is the problem, bcoz the requested field missing in that ur query.the field is bold in below query.

List<Lead> leads=[SELECT Id, Type__c, convertedAccountId  FROM Lead WHERE convertedAccountId IN: accts];

Thanks,
Ramakrishnan Ayyanar.