9

I am able to set a lead status field of the lead object to a certain value form a picklist using a trigger with the before insert argument BUT I get an error when use the following:

lead.IsConverted = TRUE;

I get the following error message: Cannot insert a converted lead (Related field: Converted)

here is my trigger:

trigger triggerDoSomething on Lead(before insert){

map<string,list<ObjectB>> BObjMap = new map<string,list<ObjectB>>();
for(ObjectB b : [Select Id, Name, fieldZ From ObjectB]){
    if(BObjMap.get(b.fieldZ) == null){
        BObjMap.put(b.fieldZ, new list<ObjectB>());
    }
    BObjMap.get(b.fieldZ).add(b);
}

for(Lead lead : trigger.new){
    if(BObjMap.containskey(lead.fieldZ)){
        //lead.status = 'Converted';
        lead.isConverted = TRUE;
    }
}

}

10

I believe you'll have to use the lead conversion process to mark a lead as converted. Something like this:

Database.LeadConvert lc;
lc = new database.LeadConvert();
lc.setLeadId(l.id);
lc.setConvertedStatus('Converted');
leadConversions.add(lc);
List<Database.LeadConvertResult> leadConversionResults = Database.convertLead( leadConversions );
  • Thanks Kevin, I've had a look through that, would you mind stepping through it with me as I have some questions such as: are setLeadId and setConvertedStatus methods or properties of the new database "LeadConvert"? What is 'leadConversions' I don't see it declared previously? What does List do? And where is the best source for me to read up on all this? Many thanks. – Bartley Oct 3 '13 at 10:50
  • 1
    Going to post a few comments here. One per topic here. Starting with most general: List<type> varName = ... is the way you create a List variable, which is similar to an array. The type of data stored in the list is defined in the < >'s You can learn more about lists here: salesforce.com/us/developer/docs/apexcode/… – Kevin P Oct 3 '13 at 10:53
  • 1
    setLeadId() and setConvertedStatus() are both instance methods on the leadConvert object created on line 2 above. You can read more about the leadConversion object, and it's instance methods here: salesforce.com/us/developer/docs/apexcode/… – Kevin P Oct 3 '13 at 10:56
  • Sorry about the lack of leadConversion definition. leadConversion is, in this case, defined as: List<Database.LeadConvert> leadConversions = new List<Database.LeadConvert>(); the code i showed you here was extracted from a project of mine, and in that project I was converting many leads in one pass by putting lines 2-5 in a loop looping over lead objects. – Kevin P Oct 3 '13 at 11:00
  • OK that's great, one question: is it wrong or incorrect to just set the lead.IsConverted to TRUE without actually converting the lead or putting the lead through the lead conversion process as you have outlined above (I have a need for that in the case where a list of leads I am upserting will need to be set to converted if they already exist as a contact)? – Bartley Oct 3 '13 at 11:13

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.