• David Cook 5
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

I'm using skyvia to update fields on Leads in job that runs once a day. That job locates the lead to update using a custom unique field that corresponds to the lead's ID in our external database. I also have an apex trigger called AutoConvertLeads that runs after updates that converts leads if they have MRR_c > 0.

/* 
After a lead is updated or inserted, it adds the lead to a list if that lead has started paying.
Then, we call convertLead on that list, converting all qualified leads. 
*/
trigger AutoConvertLeads on Lead (after update, after insert) {

    Database.LeadConvert[] leadsToConvert = new List<Database.LeadConvert>();
	LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE Id='01J1a00000DO8hfEAD' LIMIT 1];
    
    for(Lead l: Trigger.new) { //iterate through array of leads
        if(!l.isConverted && l.MRR__c > 0) {
			Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(l.Id);
            lc.setDoNotCreateOpportunity(false);
            lc.setOpportunityName(l.Company + ' - self serve');
            lc.setConvertedStatus(convertStatus.MasterLabel);
            leadsToConvert.add(lc);
        }
    }
    
    if (!leadsToConvert.isEmpty()) {
	    List<Database.LeadConvertResult> lcr = Database.convertLead(leadsToConvert);
    }
    
    // This for loop updates the opportunities that were created above with more information
    for (Database.LeadConvert l: leadsToConvert) {
        Lead lead = new Lead();
        Account acc = new Account();
        Opportunity opp = new Opportunity();
        lead = [SELECT ConvertedAccountId, LeadSource, MRR__c FROM Lead WHERE Id = :l.getLeadId()][0];
        if (lead != null) {
            acc = [SELECT Id FROM Account WHERE Id = :lead.ConvertedAccountId][0];
            if (acc != null) {
		        opp = [SELECT Id FROM Opportunity WHERE AccountId = :acc.Id and Name like '% - self serve'][0];
		        opp.CloseDate = date.today();
		        opp.LeadSource = lead.LeadSource;
		        opp.Amount = lead.MRR__c;
		        opp.StageName = 'Closed Won';
		        opp.Type = 'New Business';
                opp.Renewal_Date__c = date.today().addMonths(1);
                opp.OwnerId = '0051a000002d9h7AAA';
		        update opp;
            }
        }
    }
}


This system was working just fine, but now it seems to be broken because most Leads that skyvia tries to update end up failing with the following error:

AutoConvertLeads: execution of AfterUpdate

caused by: System.DmlException: ConvertLead failed. First exception on row 3; first error: DUPLICATE_VALUE, duplicate value found: []

()
Some of the errors say "First exception on row 0"  instead of "row 3" but I can't figure out why. One of the thing I changed around the time when this stopped working was the duplicate roles in the salesforce instance, however, the message above is not the Alert Text for any of the duplicate rules.

The above error appears for both leads that have MRR_c > 0 as well as leads that don't, which doesn't make any sense because the trigger should only be calling convertLead on leads that have MRR_c > 0. Another thing that doesn't make any sense is there aren't any duplicates. I've searched our Salesforce and can't find any objects that have the same IDs as the leads that this error occurs with.

I would really appreciate it if someone could help me with both of these unexpected behaviors.

I'm using skyvia to update fields on Leads in job that runs once a day. That job locates the lead to update using a custom unique field that corresponds to the lead's ID in our external database. I also have an apex trigger called AutoConvertLeads that runs after updates that converts leads if they have MRR_c > 0.

/* 
After a lead is updated or inserted, it adds the lead to a list if that lead has started paying.
Then, we call convertLead on that list, converting all qualified leads. 
*/
trigger AutoConvertLeads on Lead (after update, after insert) {

    Database.LeadConvert[] leadsToConvert = new List<Database.LeadConvert>();
	LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE Id='01J1a00000DO8hfEAD' LIMIT 1];
    
    for(Lead l: Trigger.new) { //iterate through array of leads
        if(!l.isConverted && l.MRR__c > 0) {
			Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(l.Id);
            lc.setDoNotCreateOpportunity(false);
            lc.setOpportunityName(l.Company + ' - self serve');
            lc.setConvertedStatus(convertStatus.MasterLabel);
            leadsToConvert.add(lc);
        }
    }
    
    if (!leadsToConvert.isEmpty()) {
	    List<Database.LeadConvertResult> lcr = Database.convertLead(leadsToConvert);
    }
    
    // This for loop updates the opportunities that were created above with more information
    for (Database.LeadConvert l: leadsToConvert) {
        Lead lead = new Lead();
        Account acc = new Account();
        Opportunity opp = new Opportunity();
        lead = [SELECT ConvertedAccountId, LeadSource, MRR__c FROM Lead WHERE Id = :l.getLeadId()][0];
        if (lead != null) {
            acc = [SELECT Id FROM Account WHERE Id = :lead.ConvertedAccountId][0];
            if (acc != null) {
		        opp = [SELECT Id FROM Opportunity WHERE AccountId = :acc.Id and Name like '% - self serve'][0];
		        opp.CloseDate = date.today();
		        opp.LeadSource = lead.LeadSource;
		        opp.Amount = lead.MRR__c;
		        opp.StageName = 'Closed Won';
		        opp.Type = 'New Business';
                opp.Renewal_Date__c = date.today().addMonths(1);
                opp.OwnerId = '0051a000002d9h7AAA';
		        update opp;
            }
        }
    }
}


This system was working just fine, but now it seems to be broken because most Leads that skyvia tries to update end up failing with the following error:

AutoConvertLeads: execution of AfterUpdate

caused by: System.DmlException: ConvertLead failed. First exception on row 3; first error: DUPLICATE_VALUE, duplicate value found: []

()
Some of the errors say "First exception on row 0"  instead of "row 3" but I can't figure out why. One of the thing I changed around the time when this stopped working was the duplicate roles in the salesforce instance, however, the message above is not the Alert Text for any of the duplicate rules.

The above error appears for both leads that have MRR_c > 0 as well as leads that don't, which doesn't make any sense because the trigger should only be calling convertLead on leads that have MRR_c > 0. Another thing that doesn't make any sense is there aren't any duplicates. I've searched our Salesforce and can't find any objects that have the same IDs as the leads that this error occurs with.

I would really appreciate it if someone could help me with both of these unexpected behaviors.