• Manjunath SC 18
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 1
    Replies
Hello All

i need a small help, i am trying to design an apex batch class where when lead is not modified for more than 24Hrs, Then Automatically email should fire to  lead owner and lead owners email address, For lead owner emaill address, i have created a process builder where lead owners manager email is populated when lead is newly created, i tried this with workflow and Timebased workflow rule, but still not working properly

Below is my Batch apex class
 
global class Emailalertbatchclass implements Database.Batchable<sObject>, Schedulable, Database.Stateful {
    
    //Variable Section
    global FINAL String strQuery;
    global FINAL String leadid;
    global List<String> errorMessages = new List<String>();
    
    global Emailalertbatchclass() { 
        this.strQuery = getBatchQuery();
    }
    
    //Returns the Query String to Batch constructor to fetch right records.
    private String getBatchQuery() {
        String strQuery = 'SELECT Id,Name,Status,Email,owner.email,owner.name,ownerid,No_Enquiry_Email_Sent__c,Manager_Email__c FROM Lead where No_Enquiry_Email_Sent__c=false AND Status=\'Enquiry\' And (CreatedDate = YESTERDAY OR LastModifiedDate = YESTERDAY) limit 1';
        return strQuery;
    }
    
    //Batch Start method
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(strQuery);
    }
    
    //Batch Execute method calls findCostForWoD method
    global void execute(Database.BatchableContext BC, List<sObject> scopeList) {
        System.debug(LoggingLevel.INFO, '== scopeList size ==' + scopeList.size());
        
        List<Lead> ld = (List<Lead>) scopeList;
        List<Lead> updatedld = new List<Lead>();
        if(!ld.isEmpty()) { 
            List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
            for (Lead prod : ld)
            {               
                // Step 1: Create a new Email
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                
                // Step 2: Set list of people who should get the email
                String[] toAddresses = new String[] {prod.owner.Email,prod.Manager_Email__c,'chandra.s@proseraa.com'};
                    mail.setToAddresses(toAddresses);
                
                // Step 3: Set who the email is sent from
                mail.setReplyTo(prod.owner.Email);
                mail.setSenderDisplayName('No Activity on Leads for 24hrs');
                
                // (Optional) Set list of people who should be CC'ed
                List<String> ccTo = new List<String>();
                ccTo.add('manjunath.s@proseraa.com');
                mail.setCcAddresses(ccTo);
                
                // Step 4. Set email contents - you can use variables!
                mail.setSubject('No Activity on Lead for 24hrs');
                String body = 'Dear ' + prod.owner.name + ', <br><br>';
                body += 'This is to notify you that there is no activity done on the respective <b> Lead Name: ';
                body +=prod.Name+'</b>  please find the link below..<br><br>';
                body += 'link to file: https://moengage--proseraa.lightning.force.com/lightning/r/Lead/'+prod.id+'/view'+'<br><br><br> Thanks,<br>Moengage Team</body></html>';
                mail.setHtmlBody(body);
                
                // Step 5. Add your email to the master list
                mailList.add(mail);
                prod.No_Enquiry_Email_Sent__c = true;
                updatedld.add(prod);
                
            }
            if(!mailList.isEmpty()) {
                try{
                    Messaging.sendEmail(mailList);
                    update updatedld;
                }
                catch (Exception ex) {
                    errorMessages.add('Unable to send email to Tech: '+ ex.getStackTraceString());
                }
            }
        }
    }  
    
    //Batch Finish method for after execution of batch work
    global void finish(Database.BatchableContext BC) { 
        
    }
    
    //Method which schedules the ProductDownloadBatch
    global void execute(SchedulableContext sc) {        
        Emailalertbatchclass snInstance = new Emailalertbatchclass();
        ID batchprocessid = Database.executeBatch(snInstance);
    }
}

and Below is my schedular class
 
global class Scheduleerclassemailalert implements Schedulable
{
    global void execute(SchedulableContext SC) { 
         Emailalertbatchclass snInstance = new Emailalertbatchclass();
        ID batchprocessid = Database.executeBatch(snInstance); }
}
so how do i resolve this issue??
 
Hey Guys

i have designed a trigger, when i create a lead it checks weather the custom field Company_Domain_Name__c already exists in the Existing accounts, if it exists,it will not create a lead, it creates contact which is associated to that existing accounts
How do i write test classes for this Trigger
 
trigger AutoConvertLeadAccount on Lead (after insert) {
    Set<String> LeadCompanyDomainNamec = new Set<String>();
    List<Lead> newLeads = new List<Lead>();
    
    // Get all the new leads
    for(Lead l : system.trigger.new){
        newLeads.add(l);
        LeadCompanyDomainNamec.add(l.Company_Domain_Name__c);
    }
    
    /* Make some maps of account and Company Domain */
    List<Account> AccountList = [select Id, Company_Domain_Name__c, OwnerId,Private__c from Account where Company_Domain_Name__c IN: LeadCompanyDomainNamec AND Private__c=true];
    system.debug('AccountList '+AccountList);
    Map<ID, String> peAccounts = new Map<ID, String>();
    Map<ID, ID> peAccountsOwner = new Map<ID, ID>();
    
    if(!AccountList.isEmpty()){
        // Generic map for preventing loss of ids
        for(Account a : AccountList){
            peAccounts.put(a.id, a.Company_Domain_Name__c);
            peAccountsOwner.put(a.id, a.OwnerId);
            system.debug('peAccounts '+peAccounts);
        }
        
        // We will need this to get the id from the email address
        Map<String, ID> peAccountsFlipped = new Map<String, ID>();
        for(ID i : peAccounts.keyset()){
            peAccountsFlipped.put(peAccounts.get(i), i);
            system.debug('peAccountsFlipped '+peAccountsFlipped);
        }
        
        /* System Conversion Requirements */
        leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
        Database.LeadConvert lc = new Database.LeadConvert();
        
        /* Configuring Payload */    
        for (Lead nl : newleads) {
            
            // Check to see if account already exists
            if(!peAccounts.isEmpty()){
                if(peAccountsFlipped.get(nl.Company_Domain_Name__c)!=null){
                    lc = new Database.LeadConvert();
                    lc.setLeadId(nl.id);
                    lc.setOverwriteLeadSource(false);
                    lc.setConvertedStatus(convertStatus.MasterLabel);
                    lc.setAccountId(peAccountsFlipped.get(nl.Company_Domain_Name__c));
                    lc.setOwnerId(peAccountsOwner.get(peAccountsFlipped.get(nl.Company_Domain_Name__c)));
                    lc.setDoNotCreateOpportunity(true);
                    system.debug('lc '+lc);
                    leadConverts.add(lc);
                }    
            } 
            
            system.debug(leadConverts);
        }
        
        // Fire Payload
        if(!leadConverts.isEmpty()){
            Database.LeadConvertResult[] lcr = Database.convertLead(leadConverts);
            System.debug(LoggingLevel.INFO, lcr);
        }
    }
}


 
Hello Guys

i am new to the trigges and apex development, so here  is my requirement - There is checkbox Called "Private__c" in account object
Once locked, if a new Lead is created with this Private Locked Account which already has a Allocated Owner, then a new Contact should be created for this Account under the allocated Account owner name. Lead will not be created. 

And If a new lead is added without a Private Account checkbox being checked then allow the Lead to be created with Lead creator as the Lead owner. 

so far i have tried to design a trigger, but its not meeting up my goal
 
trigger AutoConvertLeadAccount on Lead (after insert) {
    Set<String> leademails = new Set<String>();
    List<Lead> newLeads = new List<Lead>();

    // Get all the new leads
    for(Lead l : system.trigger.new){
        newLeads.add(l);
        leademails.add(l.email);
    }

    /* Make some maps of account and email addresses */
    List<Account> AccountList = [select Id, Website, OwnerId from Account where Website IN: leademails];
    Map<ID, String> peAccounts = new Map<ID, String>();
    Map<ID, ID> peAccountsOwner = new Map<ID, ID>();

if(AccountList.size()>0){
    // Generic map for preventing loss of ids
    for(Account a : AccountList){
        peAccounts.put(a.id, a.Website);
        peAccountsOwner.put(a.id, a.OwnerId);
    }

    // We will need this to get the id from the email address
    Map<String, ID> peAccountsFlipped = new Map<String, ID>();
    for(ID i : peAccounts.keyset()){
        peAccountsFlipped.put(peAccounts.get(i), i);
    }

    /* System Conversion Requirements */
    leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
    Database.LeadConvert lc = new Database.LeadConvert();

    /* Configuring Payload */    
    for (Lead nl : newleads) {
        lc = new Database.LeadConvert();
        lc.setLeadId(nl.id);
        lc.setOverwriteLeadSource(false);
        lc.setConvertedStatus(convertStatus.MasterLabel);

        // Check to see if account already exists
        if(!peAccounts.isEmpty()){
            if(peAccountsFlipped.get(nl.email)!=null){
                lc.setAccountId(peAccountsFlipped.get(nl.email));
                lc.setOwnerId(peAccountsOwner.get(peAccountsFlipped.get(nl.email)));
                lc.setDoNotCreateOpportunity(true);
            }    
        } else {
            // In the event an account doesn't exist
            lc.setOwnerId(nl.OwnerId);
            lc.setDoNotCreateOpportunity(false);
            lc.setOpportunityName(nl.Name);
        }
        leadConverts.add(lc);
    }

    // Fire Payload
    Database.LeadConvertResult[] lcr = Database.convertLead(leadConverts);
    System.debug(LoggingLevel.INFO, lcr);
 }
}

 
hello Trialblazers

how are you all

i need help regarding lwc, i am working on the real estate project, i have a UI in opportunity detail page, whenever i click on one of those buttons, the same should showcase on the related object detail page(Appartments), i have been advised that this can be done by saving a button states in the opportunity object and javascript needs to be used, can anyone please suggest how to achive this??

below is my snapshot of UI buttons
User-added image

Appartments
Hello Guys

i am new to the trigges and apex development, so here  is my requirement - There is checkbox Called "Private__c" in account object
Once locked, if a new Lead is created with this Private Locked Account which already has a Allocated Owner, then a new Contact should be created for this Account under the allocated Account owner name. Lead will not be created. 

And If a new lead is added without a Private Account checkbox being checked then allow the Lead to be created with Lead creator as the Lead owner. 

so far i have tried to design a trigger, but its not meeting up my goal
 
trigger AutoConvertLeadAccount on Lead (after insert) {
    Set<String> leademails = new Set<String>();
    List<Lead> newLeads = new List<Lead>();

    // Get all the new leads
    for(Lead l : system.trigger.new){
        newLeads.add(l);
        leademails.add(l.email);
    }

    /* Make some maps of account and email addresses */
    List<Account> AccountList = [select Id, Website, OwnerId from Account where Website IN: leademails];
    Map<ID, String> peAccounts = new Map<ID, String>();
    Map<ID, ID> peAccountsOwner = new Map<ID, ID>();

if(AccountList.size()>0){
    // Generic map for preventing loss of ids
    for(Account a : AccountList){
        peAccounts.put(a.id, a.Website);
        peAccountsOwner.put(a.id, a.OwnerId);
    }

    // We will need this to get the id from the email address
    Map<String, ID> peAccountsFlipped = new Map<String, ID>();
    for(ID i : peAccounts.keyset()){
        peAccountsFlipped.put(peAccounts.get(i), i);
    }

    /* System Conversion Requirements */
    leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
    Database.LeadConvert lc = new Database.LeadConvert();

    /* Configuring Payload */    
    for (Lead nl : newleads) {
        lc = new Database.LeadConvert();
        lc.setLeadId(nl.id);
        lc.setOverwriteLeadSource(false);
        lc.setConvertedStatus(convertStatus.MasterLabel);

        // Check to see if account already exists
        if(!peAccounts.isEmpty()){
            if(peAccountsFlipped.get(nl.email)!=null){
                lc.setAccountId(peAccountsFlipped.get(nl.email));
                lc.setOwnerId(peAccountsOwner.get(peAccountsFlipped.get(nl.email)));
                lc.setDoNotCreateOpportunity(true);
            }    
        } else {
            // In the event an account doesn't exist
            lc.setOwnerId(nl.OwnerId);
            lc.setDoNotCreateOpportunity(false);
            lc.setOpportunityName(nl.Name);
        }
        leadConverts.add(lc);
    }

    // Fire Payload
    Database.LeadConvertResult[] lcr = Database.convertLead(leadConverts);
    System.debug(LoggingLevel.INFO, lcr);
 }
}

 
Hello all

I want to  send an automatic email when lead status is converted to "Qualified" , i tried it in workflow and process, it worked fine, now i want to try it in apex , i have ready made email template named "XXX" which shows as 

Hello {!Lead.LastName} 

Thank you for the registering with us 
Soon we will get back to you 

Team EduProserra

my apex class is

public class emailSending {

    public String Lead { get; set; }
    public string toMail { get; set;}
    public string ccMail { get; set;}
    public string repMail { get; set;}
    
    public void sendMail(){
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        string[] to = new string[] {toMail};
        string[] cc = new string[] {ccMail};
        
        email.setToAddresses(to);
        if(ccMail!=null && ccMail != '')
            email.setCcAddresses(cc);
        if(repmail!=null && repmail!= '')
            email.setInReplyTo(repMail);
        
        email.setSubject('Thank you for the Registration');
        
        email.setHtmlBody('Hello, {!Lead.LastName} <br/><br/>Thank you for Registration. <br/>We will get back to you for more details<br/><br/>Regards<br/> EduPro Team');
        try{
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        }catch(exception e){
            apexpages.addmessage(new apexpages.message(apexpages.severity.error,e.getMessage()));
        }
        
        toMail = '';
        ccMail = '';
        repMail = '';
    }
}

how do i design a trigger for this??