0

This trigger is basically fired on before insert & before update. I am trying to automatically convert lead to Account,Contact & Opportunity. My condition is when an Same Name Lead is inserted then only its other name is updated without creating duplicate lead for the same name & also updating an Opportunity CloseDate to System.Today().

TRIGGER CODE:

trigger UpdateDuplicateRecords on Lead (before insert,before update) {
    public list<string> LastName = new list<string>();
    public list<string> LastName1 = new list<string>();
    public set<ID> ids = new set<ID>();
    public list<opportunity> newopp = new list<opportunity>();
    list<Lead> leads = [select ID,LastName from lead];
    system.debug('===>'+leads);

    for (lead newlead :trigger.new) {
        LastName.add(newlead.LastName);
        system.debug('===>'+LastName);
        ids.add(newlead.id);
        system.debug('===>'+ids);
    }

    for (lead oldlead :leads){
        LastName1.add(oldlead.LastName);
        system.debug('===>'+Lastname1);
    }

    if (LastName1 != LastName) {

        opportunity opp = new opportunity();
        opp.closedate = system.today();


        leadstatus convertstatus = [select Id,masterlabel from leadstatus where isconverted = true];

        for (Lead newlead : trigger.new){
            if ((newlead.isConverted==false)) {

                database.leadconvert lc = new database.leadconvert();
                lc.setleadID(NewLead.ID);

                lc.setConvertedStatus(convertStatus.MasterLabel);

                database.leadconvertresult lcr = database.convertlead(lc);
                system.assert(lcr.isSuccess());
                system.debug('===>' + lcr);
            }
        }
    }
    update leads;
}

Am Getting Following Error ========>

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger UpdateDuplicateRecords caused an unexpected exception, contact your administrator: UpdateDuplicateRecords: execution of BeforeInsert caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, valid leadId is required: []: Trigger.UpdateDuplicateRecords: line 37, column 1

  • 3
    before insert won't have a Lead.Id field; automatic lead conversion should be done in after triggers – cropredy Dec 12 '18 at 18:24
3

Your issue is on this line:

lc.setleadID(NewLead.ID);

Because the trigger is getting executed on before insert, during the execution, newLead will not have the Id populated. Id for any record is generated only when the record is inserted in the database.

As the setLeadId() method requires a valid Lead Id, you will need to move your logic in after insert trigger to get past this error and set the Id of the Lead that was created.

  • Yes I did that after insert , bt still duplicate leads are inserting. I want to prevent from inserting duplicate lead. I use (LastName.size()>0) to avoid duplicate lead but still has inserting duplicate lead. – vvpps ej Dec 13 '18 at 7:42
  • The question here was around the exception. The answer addresses specifically the exception that you were facing. The way the forum works is if you want to seek further advice around how to avoid duplicates, you should open that as a new question. – Jayant Das Dec 13 '18 at 13:23

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.