0

I have a checkbox field named installation when that field is checked lead is converting and it is working fine, but I also want to redirect to account record after the lead is converted but it is not redirecting.

public class autoConvertLeadHandler {

public static PageReference convertLead(List<Lead> leadData){
    System.debug(leadData);
    List<Database.LeadConvert> leadConvert = new List<Database.LeadConvert>();
    List<Account> myAcc = new List<Account>();
    List<Opportunity> myopp = new List<Opportunity>();
    List<OpportunityLineItem> myoppLineItems = new List<OpportunityLineItem>();
    List<Subscription__c> mysubscriptions = new List<Subscription__c>();
    List<Id> accountId = new List<Id>();
    List<Id> leadId = new List<Id>();
    List<Id> contactId = new List<Id>();
    List<Id> opportunityId = new List<Id>();
    Set<Id> leadIdForCard = new Set<Id>();
    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE isConverted = true LIMIT 1];
    System.debug(convertStatus);
    for(Lead leadObj : leadData){
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(leadObj.Id);
        lc.setConvertedStatus(convertStatus.MasterLabel);
        lc.setOwnerId(leadObj.CreatedById);
        leadConvert.add(lc);
        leadIdForCard.add(leadObj.Id);
    }

    System.debug(leadConvert);
    if(!leadConvert.isEmpty()){
        List<Database.LeadConvertResult> lcr = Database.convertLead(leadConvert);
        System.debug(lcr);
        for(Database.LeadConvertResult lctemp: lcr){
            accountId.add(lctemp.getAccountId());
            leadId.add(lctemp.getLeadId());
            contactId.add(lctemp.getContactId());
            opportunityId.add(lctemp.getOpportunityId());
        }
        if(accountId.size() >0){
     System.debug('accountId is :' + accountId[0]);
            Id accId = accountId[0];
            System.debug(accId);
            PageReference newPage =  new pageReference('/'+accId);
            newPage.setRedirect(true);
            //aura.redirect(newPage);
            return newPage;
    
}
    }
          
    Product2 prod = [SELECT Id, Name, Validity_in_Days__c FROM Product2 WHERE Name =: leadData[0].Starter_Package__c LIMIT 1];
    Pricebook2 pricebook = [SELECT Id FROM Pricebook2 WHERE Id =: leadData[0].Price_Book__c LIMIT 1];
    PricebookEntry pbe = [SELECT UnitPrice FROM PricebookEntry WHERE Product2Id =: prod.Id AND Pricebook2Id =: pricebook.Id LIMIT 1];
    PricebookEntry pbe2;
    if(leadData[0].Future_Subscription_Package__c != null){
        pbe2 = [SELECT UnitPrice FROM PricebookEntry WHERE Product2Id =: leadData[0].Future_Subscription_Package__c AND Pricebook2Id =: pricebook.Id LIMIT 1];
    }

    for(Lead lObj : leadData){
        Account accObj = new Account();
        accObj.Id = accountId[0];
        accObj.ShippingStreet = lObj.Service_Street__c;
        accObj.ShippingState = lObj.Service_State__c;
        accObj.ShippingCountry = lObj.Service_Country__c;
        accObj.ShippingCity = lObj.Service_City__c;
        accObj.ShippingPostalCode = lObj.Zip_Postal_Code__c;
        myAcc.add(accObj);

        Opportunity oppObj = new Opportunity();
        oppObj.Id = opportunityId[0];
        oppObj.Pricebook2Id = lObj.Price_Book__c;
        myopp.add(oppObj);

        if(lObj.Package_Language__c != null && lObj.Starter_Package__c != null){
            OpportunityLineItem opplineitem = new OpportunityLineItem();
            opplineitem.OpportunityId = opportunityId[0];
            opplineitem.Product2Id = prod.Id;
            opplineitem.Quantity = 1;
            opplineitem.UnitPrice = pbe.UnitPrice;
            myoppLineItems.add(opplineitem);

            Subscription__c sub = new Subscription__c();
            sub.Customer__c = accountId[0];
            sub.Package__c = prod.Id;
            sub.Future_Subscription_Package__c = lObj.Future_Subscription_Package__c;
            sub.Current_period_start__c = System.today();
            sub.Current_period_end__c = sub.Current_period_start__c.addDays(Integer.valueOf(prod.Validity_in_Days__c));
            if(lObj.Payment_Method__c != null){
                sub.Collection_Method__c = lObj.Payment_Method__c;
            }
            if(lObj.Recurring_Payment__c == true && (lObj.Payment_Method__c == 'IPG' || lObj.Payment_Method__c == 'Direct Deposit')){
                sub.Auto_Renew__c = true;
            }
            sub.Status__c = 'Activate';
            sub.Subscription_Flag_Status__c = 'Connect';
            mysubscriptions.add(sub);
        }
        if(lObj.Future_Subscription_Package__c != null){
            OpportunityLineItem opplineitem = new OpportunityLineItem();
            opplineitem.OpportunityId = opportunityId[0];
            opplineitem.Product2Id = lObj.Future_Subscription_Package__c;
            opplineitem.Quantity = 1;
            opplineitem.UnitPrice = pbe2.UnitPrice;
            myoppLineItems.add(opplineitem);
        }
    }

    List<Receiver_Card__c> recObj = [SELECT Id, Name, Date_Entered__c, Card_Report__c, Receiver_No__c, Comment__c, Primary_Card__c FROM Receiver_Card__c WHERE Lead__c IN: leadIdForCard];
    List<Receiver_Card__c> newRec = new List<Receiver_Card__c>();
    if(recObj.size() > 0){
        for(Receiver_Card__c rec : recObj){
            Receiver_Card__c rc = new Receiver_Card__c();
            rc.Name = rec.Name;
            rc.Primary_Card__c = rec.Primary_Card__c;
            rc.Date_Entered__c = rec.Date_Entered__c;
            rc.Card_Report__c = rec.Card_Report__c;
            rc.Receiver_No__c = rec.Receiver_No__c;
            rc.Comment__c = rec.Comment__c;
            rc.Customer__c = accountId[0];
            newRec.add(rc);
        }
    }

    if(myAcc.size() > 0){
        update myAcc;
    }
    if(newRec.size() > 0){
        insert newRec;
    }
    if(myopp.size() > 0){
        update myopp;
    }
    if(myoppLineItems.size() > 0){
        insert myoppLineItems;
    }
    if(mysubscriptions.size() > 0){
        insert mysubscriptions;

        Invoice__c inv = new Invoice__c();
        inv.Collection_Method__c = mysubscriptions[0].Collection_Method__c;
        inv.Customer__c = accountId[0];
        inv.Description__c = prod.Name;
        inv.Subscription__c = mysubscriptions[0].Id;
        inv.Invoice_Type__c = 'New Invoice';
        inv.Invoice_Amount__c = leadData[0].Total_Amount__c;
        inv.Receiver_Card__c = recObj[0].Id;
        inv.Receiver_No__c = recObj[0].Receiver_No__c;
        if(leadData[0].Paid__c == true){
            inv.Paid_Amount__c = leadData[0].Total_Amount__c;
            inv.Payment_Completed__c = true;
        }
        insert inv;

        Payment__c pay = new Payment__c();
        pay.Description__c = prod.Name;
        pay.Invoice_Number__c = inv.Id;
        pay.Payment_Amount__c = leadData[0].Total_Amount__c;
        pay.Payment_Date__c = System.today();
        pay.Payment_Method__c = mysubscriptions[0].Collection_Method__c;
        if(leadData[0].Payment_Intent_Id__c != null){
            pay.Payment_Intent_Id__c = leadData[0].Payment_Intent_Id__c;
        }
        insert pay;
    }
    return null;
}
}
2
  • 5
    You can't do that with a trigger, because triggers have no mechanism for redirecting the client to a new page. You'll need to implement this some other way that involves the client, such as a flow, component, or Visualforce page.
    – sfdcfox
    Commented Jun 22, 2023 at 15:54
  • Please don't post the same question multiple times. I've closed the previous post as a duplicate of this one.
    – David Reed
    Commented Jun 23, 2023 at 0:07

0

You must log in to answer this question.

Browse other questions tagged .