• Sai Vineeth Maddula
  • NEWBIE
  • 85 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 41
    Questions
  • 28
    Replies
I have a scenario where I have a Custom Object 'Owner Assignment' with fields Owner ID filled with different Users. I want to develop a trigger which will assign the Owner from 'Owner Assignment' object in Round Robin fashion whenever a Contact is created. Can someone help me build the logic for the above scenario since I am new to Salesforce...
Thanks in Advance...
Hi,

I would like to add opportunities whose Stage and Type is New Opportunity and New Business. I have added a new account record and these opportunities should be linked to the newly created account record.

//Batch Class

global class BatchForAddingOpps implements Database.Batchable<sObject> {
    List<Opportunity> oppList {get;set;}
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator([SELECT Name, Type, StageName, CreatedDate 
                                         FROM Opportunity 
                                         WHERE Type = 'New Business' AND StageName = 'New Opportunity']);
    }
    global void execute(Database.BatchableContext BC, List<Opportunity> oppList) {
        List<Opportunity> newList = new List<Opportunity>();
        for(Opportunity opp : oppList) {
            opp.StageName = 'Closed Lost';
        }
        update oppList;
        System.debug('newList'+oppList);
    }
    global void finish(Database.BatchableContext BC) {
        
    }
}

I've got the opportunities but unable to add these opportunities to newly created account record...Can someone help me achieve this...

Thanks in advance.
Hi, I have two different triggers on the opportunity. One will update the Total Tasks custom field and another one creates a new task when the opportunity stage is set to 'Closed Won'. Can someone help me how to merge these two functionalities into one trigger...

//Trigger 1
trigger OpportunityTaskCount on Task (after insert, after update, after delete, after undelete) {
    Set<Id> oppList = new Set<Id>();
    Set<Id> tskList = new Set<Id>();
    List<Opportunity> updateList = new List<Opportunity>();
    if(trigger.isInsert || trigger.isUndelete) {
        for(Task tsk : trigger.new) {
            if(tsk.WhatId != NULL) {
                oppList.add(tsk.WhatId);
                tskList.add(tsk.Id);
            }
        }
    }
    if(trigger.isDelete || trigger.isUpdate) {
        for(Task tsk : trigger.old) {
            if(tsk.WhatId != NULL) {
                tskList.add(tsk.WhatId);
            }
        }
    }
    for (Opportunity opp : [SELECT Id, Name, Total_Tasks__c ,(SELECT Id, Status FROM Tasks) 
                            FROM Opportunity 
                            WHERE Id in : oppList]) {
                                opp.Total_Tasks__c = opp.Tasks.size();
                                updateList.add(opp);
                            }
    update updateList;
}

//Trigger 2
trigger ClosedOpportunityTrigger on Opportunity (before insert, before update) {
    List<Task> taskList = new List<Task>();
    for(Opportunity opp : trigger.new) {
        if(trigger.isInsert || trigger.isUpdate) {
            if(opp.StageName == 'Closed Won') {
                Task tsk = new Task();
                tsk.Subject = 'Follow Up Test Task';
                tsk.WhatId = opp.Id;
                taskList.add(tsk);
            }
        }
    }
    if(taskList.size() > 0) {
        insert taskList;
    }
}

Thanks in advance :)
Hi, I have a batch class which was running continuously form a long time. Can someone tell me how to abort the job? I have tried aborting from Setup -> Apex Jobs but that didn't work. Is there any other way to abort the job. Please tell me if there is any another way...

Thanks in Advance...
Hi, I have written a batch class to send email to contact owner with Contact details created by lead conversion. I have tried Test class for the same but it was not running. Can someone help me resolve this.

//Batch Class
global class EmailWithAttachment implements Database.Batchable <sObject>, Database.Stateful{
    public List<Contact> conList {get;set;}
    public String body{get;set;}
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, FirstName, LastName, LeadSource, ConvertedDate isConverted FROM Lead';
        //system.debug('aaaaaaa'+query);
        return Database.getQueryLocator(query);
        
    }
    global void execute(Database.BatchableContext BC, Lead[] scope) {
        List<Contact> conList = new List<Contact>();
        for(Lead lead : scope) {
            conList = ([SELECT FirstName, LastName, Email, Phone
                        FROM Contact 
                        WHERE Id IN (SELECT ConvertedContactId FROM Lead)]);
        }
        system.debug('Contacts List'+conList);
        String.join(conList,',');
        messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
        body = 'Contact Details : ' +conList+ '';
        email.setPlainTextBody(body);
        email.setSubject('Contact Details from Converted Lead');
        email.setToAddresses(new string[]{'maddulasaivineeth@gmail.com'});
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        //system.debug('Contacts'+conList);
    }
    global void Finish(Database.BatchableContext BC) {
        
    }
}

//Test Class
@isTest
public class EmailWithAttachment_TC {
    public static testMethod void EmailWithAttachment_TCMethod() {
        Lead led = new Lead (
            LastName = 'Test Lead',
            Company = 'Test Company',
            Status = 'Open - Not Contacted'
        );
        insert led;
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(led.Id);
        lc.setDoNotCreateOpportunity(false);
        lc.setConvertedStatus('Converted');
        
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        
    }
}
Hi, I have a batch class which should send email to contact owner which includes contacts created from lead conversion. I was unable to use contact owner email in ToAddress field. Here is my batch class..

//Batch Class

global class EmailWithAttachment implements Database.Batchable <sObject>, Database.Stateful{
    public List<Contact> conList {get;set;}
    public String body{get;set;}
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, FirstName, LastName, LeadSource, ConvertedDate isConverted FROM Lead';
        //system.debug('aaaaaaa'+query);
        return Database.getQueryLocator(query);
        
    }
    global void execute(Database.BatchableContext BC, Lead[] scope) {
        List<Contact> conList = new List<Contact>();
        for(Lead lead : scope) {
            conList = ([SELECT FirstName, LastName, Email, Phone
                        FROM Contact 
                        WHERE Id IN (SELECT ConvertedContactId FROM Lead)]);
        }
        system.debug('Contacts List'+conList);
        String.join(conList,',');
        messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
        body = 'Contact Details : ' +conList+ '';
        email.setPlainTextBody(body);
        email.setSubject('Contact Details from Converted Lead');
        email.setToAddresses(new string[]{'maddulasaivineeth@gmail.com'});
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        //system.debug('Contacts'+conList);
    }
    global void Finish(Database.BatchableContext BC) {
        
    }

Can someone please help me write a test class for the same.

Thanks in Advance...
Hi, I have written a batch class where I would like to send an email to contact owner with the contact details created by lead conversion.

//Batch Class
global class LeadConversionEmail implements Database.Batchable <sObject>, Database.Stateful{
    //public List<Contact> conList {get;set;}
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, LeadSource, ConvertedDate FROM Lead WHERE ConvertedContactId != NULL';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, Lead[] scope) {
        List<Contact> conList = new List<Contact>();
        for(Lead ld : scope) {
            conList.add([SELECT FirstName, LastName 
                         FROM Contact 
                         WHERE Id IN (SELECT ConvertedContactId FROM Lead)]);
        }
    }
    global void Finish(Database.BatchableContext BC) {
       messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        String body = 'FirstName ' +conList.FirstName+ 'LastName ' +conList.LastName+ 'done'; 
    }
}

I am getting errors at body saying Variable does not exist. Can you help me resolve the problem and how to send email to contact owner.

Thanks in Advance.
Hi, I was stuck at a point where I need to get the contacts created by lead conversion. Can someone tell me how to query those contacts when a lead is converted...
I am new to salesforce so please help me do this.

Thanks in advance...
Hi, I have a batch class which will send email to contact owner along with attachment with some merge fields. I was able to send the attachment but the merge fields are not taking values from Contact object. Can someone help me to resolve this? 

//Batch Class

global class EmailWithAttachment implements Database.Batchable <sObject>, Database.Stateful{
    public String Subject{get;set;}
    //Public String Body{get;set;}
    Public String attname{get;set;}
    Public blob attbody{get;set;}
    Map<Id,String> contactOwnerEmail = new Map<Id,String>();
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT FirstName, LastName, Email, MobilePhone FROM Contact';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Contact> scope) {
        /*for(Contact con : scope) {
            contactOwnerEmail.add(con.Id, con.Owner.Email);
        }*/
    }
    global void Finish(Database.BatchableContext BC) {
        messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
        EmailTemplate ev=[SELECT Id, Name, Subject, Body, HtmlValue 
                          FROM EmailTemplate 
                          WHERE name = 'Test Template'];
        Blob b = Blob.valueOf(ev.Body);
        Subject = ev.Subject;
        //Body = ev.Body;
        email.setSubject(Subject);
        email.setPlainTextBody('Please Find the Attachment for Details about Contact');
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(attname);
        efa.setBody(b);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        email.setToAddresses(new String[]{'maddulasaivineeth@gmail.com'});
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}

At the setToAddress, I need to use Contact Owner Email but I was unable to get the Contact Owner Email. I have attached the image for your reference.User-added image
Hi, I want to send an email which contains attachment related to contact and some contact merge fields. Can someone help me how to do this using batch apex...
Hi, I have an apex trigger which displays total tasks related to opportunity, but my trigger is not working even it doesn't have any errors. Can someone help me with this trigger?

//Apex Trigger

trigger OpportunityTaskCount on Task (after insert, after update, after delete, after undelete) {
    Set<Id> oppList = new Set<Id>();
    Set<Id> tskList = new Set<Id>();
    List<Opportunity> updateList = new List<Opportunity>();
    if(trigger.isInsert) {
        for(Task tsk : trigger.new) {
            if(tsk.WhatId != NULL) {
                oppList.add(tsk.Id);
                tskList.add(tsk.WhatId);
            }
        }
        system.debug('Opportunities'+oppList);
    }
    if(trigger.isDelete || trigger.isUpdate ) {
        for(Task tsk : trigger.old) {
            if(tsk.WhatId != NULL) {
                oppList.add(tsk.Id);
                tskList.add(tsk.WhatId);
            }
        }
    }
    if(trigger.isUndelete) {
        for(Task tsk : trigger.new) {
            if(tsk.WhatId != NULL) {
                tskList.add(tsk.WhatId);
            }
        }
    }
    for (Opportunity opp : [SELECT Id, Name, Total_Tasks__c ,(SELECT Id, Status FROM Tasks) 
                           FROM Opportunity 
                           WHERE Id in : oppList]) {
                               opp.Total_Tasks__c = opp.Tasks.size();
                               updateList.add(opp);
                           }
    update updateList;
}
Hi, I have a wrapper class where it displays contacts related to accounts along with checkboxes. I am adding new records into it with a custom button. I want to add an error message if there are duplicates in record names. Can someone help me to solve this.

//Wrapper Class Code
public class AccountContactWrapper {
    List<Contact> selectedRecords {get;set;}
    public List<WrapperClass> wrapperList {get;set;}
    public List<Account> accList {get;set;}
    public AccountContactWrapper (ApexPages.StandardController stdController) {
        accList = [SELECT Id, Name FROM Account];
        wrapperList = new List<WrapperClass>();
        for(Contact c : [SELECT FirstName, LastName, Phone FROM Contact WHERE AccountId =: ApexPages.currentPage().getParameters().get('id')]) {
            wrapperList.add(new WrapperClass(c));
        }
    }
    //Deleting SelectedRecords
    public void deleteRecords() {
        List<Contact> selectedRecords = new List<Contact>();
        for (WrapperClass wrap : wrapperList) {
            if(wrap.selected==true) {
                selectedRecords.add(wrap.con);
            }
        }
        delete selectedRecords;
    }
    //Creating New Contact Record
    public void createNewContact() {
        wrapperList = createNewContact(wrapperList);
    }
    public List<WrapperClass> createNewContact (List<WrapperClass> insertList) {
        WrapperClass newRecord = new WrapperClass();//To display new Row
        Contact newContactRecord = new Contact();//Creates new Record
        newContactRecord.AccountId = ApexPages.currentPage().getParameters().get('id');
        newRecord.con = newContactRecord;
        insertList.add(newRecord);
        return insertList;
    }
    //Saving New Records
    public void saveRecords() {
        List<Contact> selectedRecords = new List<Contact>();
        for(WrapperClass wp : wrapperList) {
            if(wp.selected) {
                selectedRecords.add(wp.con);
            }
        }
        insert selectedRecords;
    }
    public class WrapperClass {
        public Contact con {get;set;}
        public Boolean selected {get;set;}
        public WrapperClass (Contact c) {
            con=c;
            selected=false;
        }
        public WrapperClass(){}
    }
}

//VF Page

<apex:page standardController="Account" extensions="AccountContactWrapper">
    <apex:form >
        <apex:pageBlock id="block">
            <apex:pageBlockButtons location="bottom">
                <!--<apex:commandButton value="Edit" action="{!processedRecords}"/>-->
                <apex:commandButton value="Delete Selected Records" action="{!deleteRecords}"/>
                <apex:commandButton value="Create Contact" action="{!createNewContact}"/>
                <apex:commandButton value="Save" action="{!saveRecords}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!wrapperList}" var="wrap" id="table">
                    <apex:column >
                        <apex:inputCheckbox value="{!wrap.selected}"/>
                    </apex:column>
                    <apex:column headerValue="First Name">
                        <apex:inputField value="{!wrap.con.FirstName}"/>
                    </apex:column>
                    <apex:column headerValue="Last Name">
                        <apex:inputField value="{!wrap.con.LastName}"/>
                    </apex:column>
                    <apex:column headerValue="Phone">
                        <apex:inputField value="{!wrap.con.Phone}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
I have a Batch Class where it sends an email when Expiry Date (Custom Date Field) is today. The mail body contains the list of opportunities whose Expiry Date is Today. I have created a list and added the opportunities whose Expiry Date is today. But I am stuck at calling Opportunity Name from the List. Can someone help me to solve this.
//Batch Class

global class OpportunityExpiryDate_New implements DataBase.Batchable <sObject> {
    global DataBase.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Name, Expiry_Date__c  FROM Opportunity WHERE Expiry_Date__c = TODAY';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Opportunity> scope) {
        List<Opportunity> oppList = new List<Opportunity>();
        for(Opportunity opp : scope) {
            if(opp.Expiry_Date__c == date.today()) {
                oppList.add(opp);
            }
        }
        update oppList;
        messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
        String body = 'The Opportunity ' +oppList+ ' is expiring today.';
        email.setToAddresses(new String[]{'maddulasaivineeth@gmail.com'});
        email.setSubject('Opportunities Expiring Today');
        email.setPlainTextBody(body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
    global void finish(Database.BatchableContext BC) {   
    }
}
Hi, I have a batch class that will send mail if Opportunity Expiry Date (Custom Field) is Today. I have tried a batch class but it isn't working for me. Can someone tell me how to achieve this.

//Batch Class
global class OpportunityExpiryDate_New implements DataBase.Batchable <sObject> {
    global DataBase.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, Name, Expiry_Date__c FROM Oppportunity WHERE Expiry_Date__c = TODAY';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Opportunity> scope) {
        messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
        for(Opportunity opp : scope) {
            String body = 'The Opportunity'+opp.Name+'is expiring today.';
            email.setToAddresses(new String[]{'maddulasaivineeth@gmail.com'});
            email.setSubject('Opportunities Expiring Today');
            email.setPlainTextBody(body);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        }
    }
    global void finish(Database.BatchableContext BC) {
        
    }
}
Hi, I have a wrapper class where it displays contacts related to account object with a checkbox. I want to add a button which should create a new contact with the same accountId...
//Wrapper Class

public class AccountContactWrapper {
    List<Contact> selectedRecords {get;set;}
    public List<WrapperClass> wrapperList {get;set;}
    public List<Account> accList {get;set;}
    public AccountContactWrapper (ApexPages.StandardController stdController) {
        accList = [SELECT Id, Name FROM Account];
        wrapperList = new List<WrapperClass>();
        for(Contact c : [SELECT FirstName, LastName, Phone FROM Contact WHERE AccountId =: ApexPages.currentPage().getParameters().get('id')]) {
            wrapperList.add(new WrapperClass(c));
        }
    }
    //Deleting SelectedRecords
    public void deleteRecords() {
        List<Contact> selectedRecords = new List<Contact>();
        for (WrapperClass wrap : wrapperList) {
            if(wrap.selected==true) {
                selectedRecords.add(wrap.con);
            }
        }
        delete selectedRecords;
    }
    //Editing Selected Records
    /*public void editRecords() {
        List<Contact> selectedRecords = new List<Contact>();
        for (WrapperClass wrap : wrapperList) {
            if(wrap.selected==true) {
                selectedRecords.add(wrap.con);
            }
        }
        selectedRecords.isEdit=true;
    }*/
    //Creating New Contact
    public void createNewContact() {
        
    }
    public class WrapperClass {
        public Contact con {get;set;}
        public Boolean selected {get;set;}
        public WrapperClass (Contact c) {
            con=c;
            selected=false;
        }
    }
}

//Apex Page


<apex:page standardController="Account" extensions="AccountContactWrapper">
    <apex:form>
        <apex:pageBlock id="block">
            <apex:pageBlockButtons location="bottom">
                <!--<apex:commandButton value="Edit" action="{!processedRecords}"/>-->
                <apex:commandButton value="Delete Selected Records" action="{!deleteRecords}"/>
                <apex:commandButton value="Create Contact" action="{!createNewContact}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                <apex:pageBlockTable value="{!wrapperList}" var="wrap" id="table">
                    <apex:column>
                        <apex:inputCheckbox value="{!wrap.selected}"/>
                    </apex:column>
                    <apex:column value="{!wrap.con.FirstName}" headerValue="First Name"/>
                    <apex:column value="{!wrap.con.LastName}" headerValue="Last Name"/>
                    <apex:column value="{!wrap.con.Phone}" headerValue="Phone"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Create a field called Expiry Date(Date field) on opportunity. Write a batch to get the opportunities whose expiry date is today and send those opportunity details to the email(use custom label for this) and this batch should run every day at 8 am ist
Hi, Can someone tell me in detail about Remote Action in Salesforce. 
Hi,
I have an apex trigger to count the total Opportunities related to an Account and Sum of Opportunity Amount related to Account. I am trying to implement "after undelete" in my trigger. But it isn't working. Can someone help to do this?
Below is my Trigger

trigger OpportunityCount on Opportunity (after insert, after delete, after update, after Undelete) {
    Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
    Set<Id> acctIds = new Set<Id>();
    List<Opportunity> opptyList = new List<Opportunity>();
    if(trigger.isUpdate && trigger.isInsert){
        for(Opportunity oppty : trigger.New){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Opportunity oppty : trigger.old){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(acctIds.size() > 0){
        opptyList = [SELECT Amount, AccountId 
                     FROM Opportunity 
                     WHERE AccountId IN : acctIds];
        for(Opportunity oppty : opptyList){
            if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
                acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
            }
            acctIdOpptyListMap.get(oppty.AccountId).add(oppty); 
        }
       
        List<AggregateResult> lstResult = [SELECT AccountId, COUNT(Id) countId 
                                           FROM Opportunity 
                                           WHERE AccountId IN:acctIds
                                           GROUP BY AccountId];
        
        List<Account> lstAccount = new List<Account>();
        for(AggregateResult result:lstResult){
            Account acct = new Account (Id=(Id)result.get('AccountId'), Total_Opportunities__c = (Integer)result.get('countId'));
            lstAccount.add(acct);
        }
        update lstAccount;  
        
        List<Account> acctList = new List<Account>();
        acctList = [SELECT Total_Amount__c 
                    FROM Account 
                    WHERE Id IN: acctIds];
        for(Account acct : acctList){
            List<Opportunity> tempOpptyList = new List<Opportunity>();
            tempOpptyList = acctIdOpptyListMap.get(acct.Id);
            Double totalOpptyAmount = 0;
            for(Opportunity oppty : tempOpptyList){
                if(oppty.Amount != null){
                    totalOpptyAmount += oppty.Amount;
                }
            }
            acct.Total_Amount__c = totalOpptyAmount;
        }
        update acctList;
    }
}
I would like to send an email when account-related opportunity stage changes using triggers. The email should contain the updated stage value in the mail template. Can someone help me write the trigger. Thank you...
Hi, I've created a custom, VF Page. It is having two pageBlockTables inside it. When I click a button "Get Records" the two tables should get refreshed. I am trying to refresh my entire page Block but that's not working n my case. I am attaching my page here along with a screenshot of my page.
 <apex:page standardController="Account" extensions="newWrapperClass">
    <apex:form >
        <apex:pageMessages></apex:pageMessages>
        <apex:pageBlock id="block">
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Get Records" action="{!processedRecords}" reRender="table1,table2"/>
                <apex:commandButton value="Send Email" action="{!sendEmail}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Detail Records">
                <apex:pageBlockTable value="{!wrapperTaskList}" var="wrap" id="table1">
                <apex:column >
                    <apex:inputCheckbox value="{!wrap.selected}"/>
                </apex:column>
                <apex:column value="{!wrap.chg.Name}" headerValue="Name"/>
                <apex:column value="{!wrap.chg.Phone__c}" headerValue="Phone"/>
                </apex:pageBlockTable>
                <apex:pageBlockTable value="{!existingRecords}" var="sel" id="table2">
                    <apex:column value="{!sel.Name}" headerValue="Name"/>
                    <apex:column value="{!sel.Phone__c}" headerValue="Phone"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>User-added image
I have a scenario where I have a Custom Object 'Owner Assignment' with fields Owner ID filled with different Users. I want to develop a trigger which will assign the Owner from 'Owner Assignment' object in Round Robin fashion whenever a Contact is created. Can someone help me build the logic for the above scenario since I am new to Salesforce...
Thanks in Advance...
Hi, I have a batch class which was running continuously form a long time. Can someone tell me how to abort the job? I have tried aborting from Setup -> Apex Jobs but that didn't work. Is there any other way to abort the job. Please tell me if there is any another way...

Thanks in Advance...
Hi, I have written a batch class to send email to contact owner with Contact details created by lead conversion. I have tried Test class for the same but it was not running. Can someone help me resolve this.

//Batch Class
global class EmailWithAttachment implements Database.Batchable <sObject>, Database.Stateful{
    public List<Contact> conList {get;set;}
    public String body{get;set;}
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, FirstName, LastName, LeadSource, ConvertedDate isConverted FROM Lead';
        //system.debug('aaaaaaa'+query);
        return Database.getQueryLocator(query);
        
    }
    global void execute(Database.BatchableContext BC, Lead[] scope) {
        List<Contact> conList = new List<Contact>();
        for(Lead lead : scope) {
            conList = ([SELECT FirstName, LastName, Email, Phone
                        FROM Contact 
                        WHERE Id IN (SELECT ConvertedContactId FROM Lead)]);
        }
        system.debug('Contacts List'+conList);
        String.join(conList,',');
        messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
        body = 'Contact Details : ' +conList+ '';
        email.setPlainTextBody(body);
        email.setSubject('Contact Details from Converted Lead');
        email.setToAddresses(new string[]{'maddulasaivineeth@gmail.com'});
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        //system.debug('Contacts'+conList);
    }
    global void Finish(Database.BatchableContext BC) {
        
    }
}

//Test Class
@isTest
public class EmailWithAttachment_TC {
    public static testMethod void EmailWithAttachment_TCMethod() {
        Lead led = new Lead (
            LastName = 'Test Lead',
            Company = 'Test Company',
            Status = 'Open - Not Contacted'
        );
        insert led;
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(led.Id);
        lc.setDoNotCreateOpportunity(false);
        lc.setConvertedStatus('Converted');
        
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        
    }
}
Hi, I have a batch class which should send email to contact owner which includes contacts created from lead conversion. I was unable to use contact owner email in ToAddress field. Here is my batch class..

//Batch Class

global class EmailWithAttachment implements Database.Batchable <sObject>, Database.Stateful{
    public List<Contact> conList {get;set;}
    public String body{get;set;}
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, FirstName, LastName, LeadSource, ConvertedDate isConverted FROM Lead';
        //system.debug('aaaaaaa'+query);
        return Database.getQueryLocator(query);
        
    }
    global void execute(Database.BatchableContext BC, Lead[] scope) {
        List<Contact> conList = new List<Contact>();
        for(Lead lead : scope) {
            conList = ([SELECT FirstName, LastName, Email, Phone
                        FROM Contact 
                        WHERE Id IN (SELECT ConvertedContactId FROM Lead)]);
        }
        system.debug('Contacts List'+conList);
        String.join(conList,',');
        messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
        body = 'Contact Details : ' +conList+ '';
        email.setPlainTextBody(body);
        email.setSubject('Contact Details from Converted Lead');
        email.setToAddresses(new string[]{'maddulasaivineeth@gmail.com'});
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        //system.debug('Contacts'+conList);
    }
    global void Finish(Database.BatchableContext BC) {
        
    }

Can someone please help me write a test class for the same.

Thanks in Advance...
Hi, I have written a batch class where I would like to send an email to contact owner with the contact details created by lead conversion.

//Batch Class
global class LeadConversionEmail implements Database.Batchable <sObject>, Database.Stateful{
    //public List<Contact> conList {get;set;}
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, LeadSource, ConvertedDate FROM Lead WHERE ConvertedContactId != NULL';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, Lead[] scope) {
        List<Contact> conList = new List<Contact>();
        for(Lead ld : scope) {
            conList.add([SELECT FirstName, LastName 
                         FROM Contact 
                         WHERE Id IN (SELECT ConvertedContactId FROM Lead)]);
        }
    }
    global void Finish(Database.BatchableContext BC) {
       messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        String body = 'FirstName ' +conList.FirstName+ 'LastName ' +conList.LastName+ 'done'; 
    }
}

I am getting errors at body saying Variable does not exist. Can you help me resolve the problem and how to send email to contact owner.

Thanks in Advance.
Hi, I was stuck at a point where I need to get the contacts created by lead conversion. Can someone tell me how to query those contacts when a lead is converted...
I am new to salesforce so please help me do this.

Thanks in advance...
I have a Batch Class where it sends an email when Expiry Date (Custom Date Field) is today. The mail body contains the list of opportunities whose Expiry Date is Today. I have created a list and added the opportunities whose Expiry Date is today. But I am stuck at calling Opportunity Name from the List. Can someone help me to solve this.
//Batch Class

global class OpportunityExpiryDate_New implements DataBase.Batchable <sObject> {
    global DataBase.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Name, Expiry_Date__c  FROM Opportunity WHERE Expiry_Date__c = TODAY';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Opportunity> scope) {
        List<Opportunity> oppList = new List<Opportunity>();
        for(Opportunity opp : scope) {
            if(opp.Expiry_Date__c == date.today()) {
                oppList.add(opp);
            }
        }
        update oppList;
        messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
        String body = 'The Opportunity ' +oppList+ ' is expiring today.';
        email.setToAddresses(new String[]{'maddulasaivineeth@gmail.com'});
        email.setSubject('Opportunities Expiring Today');
        email.setPlainTextBody(body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
    global void finish(Database.BatchableContext BC) {   
    }
}
Hi,
I have an apex trigger to count the total Opportunities related to an Account and Sum of Opportunity Amount related to Account. I am trying to implement "after undelete" in my trigger. But it isn't working. Can someone help to do this?
Below is my Trigger

trigger OpportunityCount on Opportunity (after insert, after delete, after update, after Undelete) {
    Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
    Set<Id> acctIds = new Set<Id>();
    List<Opportunity> opptyList = new List<Opportunity>();
    if(trigger.isUpdate && trigger.isInsert){
        for(Opportunity oppty : trigger.New){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Opportunity oppty : trigger.old){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(acctIds.size() > 0){
        opptyList = [SELECT Amount, AccountId 
                     FROM Opportunity 
                     WHERE AccountId IN : acctIds];
        for(Opportunity oppty : opptyList){
            if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
                acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
            }
            acctIdOpptyListMap.get(oppty.AccountId).add(oppty); 
        }
       
        List<AggregateResult> lstResult = [SELECT AccountId, COUNT(Id) countId 
                                           FROM Opportunity 
                                           WHERE AccountId IN:acctIds
                                           GROUP BY AccountId];
        
        List<Account> lstAccount = new List<Account>();
        for(AggregateResult result:lstResult){
            Account acct = new Account (Id=(Id)result.get('AccountId'), Total_Opportunities__c = (Integer)result.get('countId'));
            lstAccount.add(acct);
        }
        update lstAccount;  
        
        List<Account> acctList = new List<Account>();
        acctList = [SELECT Total_Amount__c 
                    FROM Account 
                    WHERE Id IN: acctIds];
        for(Account acct : acctList){
            List<Opportunity> tempOpptyList = new List<Opportunity>();
            tempOpptyList = acctIdOpptyListMap.get(acct.Id);
            Double totalOpptyAmount = 0;
            for(Opportunity oppty : tempOpptyList){
                if(oppty.Amount != null){
                    totalOpptyAmount += oppty.Amount;
                }
            }
            acct.Total_Amount__c = totalOpptyAmount;
        }
        update acctList;
    }
}
Hi, I've created a custom, VF Page. It is having two pageBlockTables inside it. When I click a button "Get Records" the two tables should get refreshed. I am trying to refresh my entire page Block but that's not working n my case. I am attaching my page here along with a screenshot of my page.
 <apex:page standardController="Account" extensions="newWrapperClass">
    <apex:form >
        <apex:pageMessages></apex:pageMessages>
        <apex:pageBlock id="block">
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Get Records" action="{!processedRecords}" reRender="table1,table2"/>
                <apex:commandButton value="Send Email" action="{!sendEmail}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Detail Records">
                <apex:pageBlockTable value="{!wrapperTaskList}" var="wrap" id="table1">
                <apex:column >
                    <apex:inputCheckbox value="{!wrap.selected}"/>
                </apex:column>
                <apex:column value="{!wrap.chg.Name}" headerValue="Name"/>
                <apex:column value="{!wrap.chg.Phone__c}" headerValue="Phone"/>
                </apex:pageBlockTable>
                <apex:pageBlockTable value="{!existingRecords}" var="sel" id="table2">
                    <apex:column value="{!sel.Name}" headerValue="Name"/>
                    <apex:column value="{!sel.Phone__c}" headerValue="Phone"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>User-added image
Hi,
I have a scenario where I have few records in a custom object. The custom object is in lookup relation with Account. In the child records I want to update the lookup filed using apex class.

 I have created a custom page where I have a checkbox to select recods. The selected record should be assigned to any parent record by clicking a button. I have attached the image of my custom page for your reference.

Can someone tell me how to achieve this scenario..
Hi,

I would like to retrieve the contacts related to each account using the apex class. I tried but I am getting the list of accounts on page. Can someone tell me how to achieve this?

//Apex Class

public class operations_class
{
    public List<Contact> result{get;set;}
    public List<Account> accList = [Select Id from Account];
    public operations_class()
    {
        result=new List<Contact>();
    }
    public void search()
    {
        result=[Select FirstName, LastName, Email from Contact Where account.Id=:accList];
    }
}


//VF Page

<apex:page controller="operations_class">
    <script>
        window.onloadstart = function()
        {
            hello()
            {

            }
        }
    </script>
    <apex:form>
        <apex:actionFunction name="hello" action="{!search}">
        <apex:pageBlock>
            <apex:pageBlockTable value="{!result}" var="con">
                <apex:column value="{!con.FirstName}"/>
                <apex:column value="{!con.LastName}"/>
                <apex:column value="{!con.Email}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        </apex:actionFunction>
    </apex:form>
</apex:page>
Hi, I've written the below class. While executing the class I am getting the error
"System.NullPointerException: Argument cannot be null." Can someone help me to resolve the issue. Below is my class,

public class UpdateEligibleContactsController {
    public void checkContactsEligible(Set<Id> contactIds) {
        Set<Id> accountIds = new Set<Id>();
        Map<Id,List<Opportunity>> accOpportunityMap = new Map<Id,List<Opportunity>>();
        //get Accounts based on ContactId
        List<Contact> contactList = [Select AccountId,Eligible__c from Contact WHERE id in:contactIds];
        for(Contact con : contactList) {
            accountIds.add(con.AccountId);
        }
        system.debug('accountIdsaccountIds'+accountIds);
        //getting opportunites from Account
        List<Opportunity> oppList = [Select AccountId,Amount from Opportunity WHERE AccountId in:accountIds];
        //Constructing map to avoid query in loop
        for(Opportunity opp : oppList) {
            Id accountId = opp.AccountId;
            List<Opportunity> accOppList = accOpportunityMap.get(accountId);
            if(accOppList == null) {
                accOpportunityMap.put(accountId,new List<Opportunity>{opp});
            } else {
                accOpportunityMap.get(accountId).add(opp);
            }
        }
        List<Contact> updateContactList = new List<Contact>();
        for(Contact con : contactList) {
            Id accountId = con.AccountId;
            List<Opportunity> accountoppList = accOpportunityMap.get(accountId);
            Decimal totalSum = 0;
            for(Opportunity opp:accountoppList){
                Decimal amount = opp.Amount;
                totalSum += amount;
            }
            if(totalSum > 5000) {
                con.Eligible__c = true;
            } else {
                con.Eligible__c = false;
            }
            
            updateContactList.add(con);
        }
        if(updateContactList.size() > 0) {
            update updateContactList;
        }
    }
}
Here is my apex class
public class AccountClass
{
    public void createAccount(String Name)
    {
        AccountClass acc = new AccountClass();
        acc.Name = 'Test';
        insert acc;
    }
}

I am getting the following error while compiling,
DML requires SObject or SObject list type: AccountClass.

Can anyone help me how to resolve this error also tell me how to execute this class using Developer Console
I would like to create an alert selecting picklist value and the alert should display the value of picklist which I have selected. I tried the below code but no luck.
<apex:page standardController="Account" recordSetVar="Accounts" sidebar="False"> <apex:form > <apex:pageBlock > <apex:pageBlockSection title="Details"> <apex:pageBlockTable value="{!Accounts}" var="a"> <apex:column value="{!a.name}"/> <apex:column value="{!a.Type}"/> <apex:column value="{!a.Website}"/> <apex:column headerValue="Details"> <apex:commandButton value="Details" onclick="showAlert('{!a.Name}')"/></apex:column> </apex:pageBlockTable> </apex:pageBlockSection> </apex:pageBlock> </apex:form> <script> function showAlert(accountName){ alert('HELLO ' + accountName); } </script> </apex:page>
Can anyone help me achieve this.
I was unable to complete a trailhead task in Admin Beginner-> Salesforce Mobile App Customization -> Customize Compact Layouts..
I have created a Compact Layout in my Developer Environment but I am still getting the following error..


Challenge not yet complete in maddulasaivineeth@bigworks.com
There was an unexpected error while verifying this challenge. Usually this is due to some pre-existing configuration or code in the challenge Org. We recommend using a new Developer Edition (DE) to check this challenge. If you're using a new DE and seeing this error, please post to the developer forums and reference error id: ZFEQRJTP

So we have a visualforce page which has a standard Apex button which calls the native "Save" event. After the users save the "form" so to speak, they are taken to another page which is an overview of what they just did with the options to edit or submit (which triggers an email to an internal department)

 

 

Since our sales reps aren't the brightest bunch, they would like a "pop up" to remind them to submit after they have saved. So in actullity im just looking to attach a JavaScirpt listener to that button that when its clicked, the save event is still initiated but there is a pop up which just displays a message stating "Please do not forget to submit after saving". Its almost like a confirm popup but i dont know if i can attach an apex command to the confirmation.

 

 

Does anyone have any help or advice on this one? i can post a little bit of the Visualforce page if needed. Any help is appreciated.

 

Thanks,

Dan

I'm using the following markup to show an ordered list in my Visualforce page:

 

<apex:dataList type="1" value="{!EmployeeFAQ.ClsSolution}" var="itemQ" id="theList" styleClass="questionsList"> <apex:outputLink value="#a{!itemQ.SCount}"> <apex:outputText value="{!itemQ.Obj.SolutionName}" /> </apex:outputLink></apex:dataList>

 

However, the result being rendered is an unordered list:

 

 

<ul class="questionsList" id="j_id0:j_id1:theList" type="1"><li id="j_id0:j_id1:theList:0" class="">...</li>......<ul>

  

Any pointers as to what could be wrong here?

 

Thanks in Advance,

-Manu 

 

  • November 02, 2009
  • Like
  • 0