• Swami Deshanel
  • NEWBIE
  • 20 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 13
    Replies
I have an autoconvert trigger that will convert a lead when a checkbox is checked.  It works fine both one record at a time and in bulk except when the lead has an attachemnt and I cant figure out why.

Here is my code

trigger AutoConvert on Lead (after update) {
    list<Lead> LeadsToConvert = new list<Lead>();
    for(Lead myLead: Trigger.new){
        if((!myLead.isConverted)
        && myLead.To_Be_Converted__c== True) {
        LeadsToConvert.add(myLead);
    }
}

    list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
    for(Lead myLead : LeadsToConvert){
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Consult to be Scheduled';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(False);
        leadConverts.add(lc);
    }

    if(!leadConverts.isEmpty()){
        for(Integer i = 0; i <= leadConverts.size()/100 ; i++){
            list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
            Integer startIndex = i*100;
            Integer endIndex = ((startIndex+100) < leadConverts.size()) ? startIndex+100: leadConverts.size();
            for(Integer j=startIndex;j<endIndex;j++){
                tempList.add(leadConverts[j]);
            }
            Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
            for(Database.LeadConvertResult lcr : lcrList)
                System.assert(lcr.isSuccess());
        }
    }
}
Ideally what I would have is a visualforce page that would lauch from the Opportunity, bringing up a Contact search that allows you to input various contact and account filters, and then it brings up a list of contacts whcih could be slected with a check box, and another button that would then add those contacts to the campaign set as the Primary campaign souce of the Opportunity.  

I believe I will need a visualforce page and a class.  

I have no trouble launching a visualforce page from a button, or creating a junction object from Contact to Opportunity if necessary.   It would also be great if it could prefill some of the search criteria based on Opportunity Fields (for example, Opportunity Type is "Entertainment, then the search would already prepopulate the account search field Account Type with "Entertainment").   Then the search would bring up all the contacts that would would fit those filters.   
I have a trigger for converting leads but I can not get the code coverage past 26%.  Please Help

Trigger:
trigger AutoConvert on Lead (after update) {
    list<Lead> LeadsToConvert = new list<Lead>();
    for(Lead myLead: Trigger.new){
        if(!myLead.isConverted && myLead.status=='Consult to be Scheduled' )
            LeadsToConvert.add(myLead);
    }

    list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
    for(Lead myLead : LeadsToConvert){
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Consult to be Scheduled';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(False);
        leadConverts.add(lc);
    }

    if(!leadConverts.isEmpty()){
        for(Integer i = 0; i <= leadConverts.size()/100 ; i++){
            list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
            Integer startIndex = i*100;
            Integer endIndex = ((startIndex+100) < leadConverts.size()) ? startIndex+100: leadConverts.size();
            for(Integer j=startIndex;j<endIndex;j++){
                tempList.add(leadConverts[j]);
            }
            Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
            for(Database.LeadConvertResult lcr : lcrList)
                System.assert(lcr.isSuccess());
        }
    }
}
I have a mass convert trigger that when a lead is a Contacted it will get converted.   When I test the trigger by changing the status of a lead to contacted, it works perfectly.   However, when I mass upload leads, it converts all the records, not just those with a Contacted status.   Please help

And on a side note, if you happen to have a test class that will conver this triggfer, that would save me heaps of time.  Thanks in advance

Code Below:

trigger AutoConvert on Lead (after update) {
    list<Lead> LeadsToConvert = new list<Lead>();
    for(Lead myLead: Trigger.new){
        if(!myLead.isConverted)
            LeadsToConvert.add(myLead);
    }

    list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
    for(Lead myLead : LeadsToConvert){
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Contacted';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(true);
        leadConverts.add(lc);
    }

    if(!leadConverts.isEmpty()){
        for(Integer i = 0; i <= leadConverts.size()/100 ; i++){
            list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
            Integer startIndex = i*100;
            Integer endIndex = ((startIndex+100) < leadConverts.size()) ? startIndex+100: leadConverts.size();
            for(Integer j=startIndex;j<endIndex;j++){
                tempList.add(leadConverts[j]);
            }
            Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
            for(Database.LeadConvertResult lcr : lcrList)
                System.assert(lcr.isSuccess());
        }
    }
}
Here is my Code.   The error occurs when my users convert a lead without creating an opportuntity.


trigger checkPrimaryCampaignSource on Lead (after update) {
    //This will be a set of all opportunity IDs for leads that were just converted
    Set<Id> convertedOppIds = new Set<Id>();
    for (Lead l :Trigger.new) {
        if (l.IsConverted == true) {
            convertedOppIds.add(l.ConvertedOpportunityId);
        }
    }
  
    //Now get a set of all of the campaign members related to the converted leads who have not responded to any of the campaigns
    List<CampaignMember> campaignMembers = [SELECT Id, Lead.ConvertedOpportunityId FROM CampaignMember WHERE Lead.ConvertedOpportunityId IN :convertedOppIds AND HasResponded = false];
    //Construct a set of opportunity ids that were converted from leads who did not respond to the campaign
    Set<Id> nonResponsiveOppIds = new Set<Id>();
    for (CampaignMember cm : campaignMembers) {
        nonResponsiveOppIds.add(cm.Lead.ConvertedOpportunityId);
    }
  
    //Now we can get a list of all of the opportunities where we need to remove the primary campaign source
    List<Opportunity> oppsToUpdate = [SELECT CampaignId FROM Opportunity WHERE Id IN :nonResponsiveOppIds];
    for (Opportunity opp : oppsToUpdate) {
        opp.CampaignId = null;
    }
  
    if (oppsToUpdate.size() > 0) {
        update oppsToUpdate;
    }
}
I would like to remove primary campaign source from the contact and opportunity on lead conversion unless the lead had Responded to the campaign.  

I tried this trigger, but it doenst work, and i cant figure out why

trigger checkPrimaryCampaignSource on Opportunity (before update) {
for (Opportunity o: Trigger.new) {
Lead l = [SELECT id,converteddate FROM Lead WHERE ConvertedOpportunityId =:o.ID LIMIT 1];
try {
if(l.converteddate!=null){
CampaignMember cm = [Select id,Status, HasResponded FROM campaignmember WHERE LeadId=:l.id AND CampaignId=:o.Campaign.id];
if (cm.HasResponded ==false){
o.CampaignId=null;
}
}
} catch (Exception e){
system.debug('error: ' +e);
}
}



}
I have a trigger for converting leads but I can not get the code coverage past 26%.  Please Help

Trigger:
trigger AutoConvert on Lead (after update) {
    list<Lead> LeadsToConvert = new list<Lead>();
    for(Lead myLead: Trigger.new){
        if(!myLead.isConverted && myLead.status=='Consult to be Scheduled' )
            LeadsToConvert.add(myLead);
    }

    list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
    for(Lead myLead : LeadsToConvert){
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Consult to be Scheduled';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(False);
        leadConverts.add(lc);
    }

    if(!leadConverts.isEmpty()){
        for(Integer i = 0; i <= leadConverts.size()/100 ; i++){
            list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
            Integer startIndex = i*100;
            Integer endIndex = ((startIndex+100) < leadConverts.size()) ? startIndex+100: leadConverts.size();
            for(Integer j=startIndex;j<endIndex;j++){
                tempList.add(leadConverts[j]);
            }
            Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
            for(Database.LeadConvertResult lcr : lcrList)
                System.assert(lcr.isSuccess());
        }
    }
}
I have a mass convert trigger that when a lead is a Contacted it will get converted.   When I test the trigger by changing the status of a lead to contacted, it works perfectly.   However, when I mass upload leads, it converts all the records, not just those with a Contacted status.   Please help

And on a side note, if you happen to have a test class that will conver this triggfer, that would save me heaps of time.  Thanks in advance

Code Below:

trigger AutoConvert on Lead (after update) {
    list<Lead> LeadsToConvert = new list<Lead>();
    for(Lead myLead: Trigger.new){
        if(!myLead.isConverted)
            LeadsToConvert.add(myLead);
    }

    list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
    for(Lead myLead : LeadsToConvert){
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Contacted';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(true);
        leadConverts.add(lc);
    }

    if(!leadConverts.isEmpty()){
        for(Integer i = 0; i <= leadConverts.size()/100 ; i++){
            list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
            Integer startIndex = i*100;
            Integer endIndex = ((startIndex+100) < leadConverts.size()) ? startIndex+100: leadConverts.size();
            for(Integer j=startIndex;j<endIndex;j++){
                tempList.add(leadConverts[j]);
            }
            Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
            for(Database.LeadConvertResult lcr : lcrList)
                System.assert(lcr.isSuccess());
        }
    }
}
I would like to remove primary campaign source from the contact and opportunity on lead conversion unless the lead had Responded to the campaign.  

I tried this trigger, but it doenst work, and i cant figure out why

trigger checkPrimaryCampaignSource on Opportunity (before update) {
for (Opportunity o: Trigger.new) {
Lead l = [SELECT id,converteddate FROM Lead WHERE ConvertedOpportunityId =:o.ID LIMIT 1];
try {
if(l.converteddate!=null){
CampaignMember cm = [Select id,Status, HasResponded FROM campaignmember WHERE LeadId=:l.id AND CampaignId=:o.Campaign.id];
if (cm.HasResponded ==false){
o.CampaignId=null;
}
}
} catch (Exception e){
system.debug('error: ' +e);
}
}



}