Forum Replies Created

  • Awaiting for your comments harsitha..
    Don't forget to like or comment if the solution works.so that other people can directly jump on to the solution if they face the same issue..
    or let me know the challenges you're facing to implement the above logic..
    we are here to help..
    we really appreciate your valuable comments or likes..

  • Taken example of account and contact.

    On Account object create a field to Count the number of child contacts and add it to your layout(in this example the field is Count_of_Contacts__c).Next create a Trigger on contact and simply copy and paste the below code.This will work in all DML operations such as Create ,Edit and Delete of a contact from an account and it is bulkified also 🙂

    trigger UpdateCountonAccount on Contact (after insert,after update,after delete) {
    set<Id> accIds = new Set<Id>();
    if(trigger.isInsert || trigger.isUpdate){
    for(contact con :trigger.new){
    accIds.add(con.accountId);
    }
    }
    if(trigger.isdelete){
    for(contact con :trigger.old){
    accIds.add(con.accountId);
    }
    }
    Map<Id, Integer> ConCountMap = new Map<Id, Integer>();
    for (AggregateResult aggRes : [
    SELECT COUNT(ID) numofCons, accountId accId
    FROM contact where accountId IN: accIds
    GROUP BY accountId
    ]) {
    Id accId = (Id) aggRes.get('accId');
    Integer numofcons = (Integer) aggRes.get('numofCons');
    ConCountMap.put(accId, numofcons);
    }
    if(!ConCountMap.isEmpty()){
    List<account> lstAcconts = new List<account>();
    List<account> lstResultAcconts = new List<account>();
    lstAcconts =[select id,name from account where id IN:ConCountMap.keySet()] ;
    for(account acc :lstAcconts){
    if(ConCountMap.containsKey(acc.id)){
    acc.Count_of_Contacts__c = ConCountMap.get(acc.id);
    lstResultAcconts.add(acc);
    }
    }
    if(!lstResultAcconts.isEmpty()){
    update lstResultAcconts;
    }
    }

    }

    Let me know if you face any issues.

    • This reply was modified 7 years, 6 months ago by  Shaik.
  • Shaik

    Member
    June 9, 2017 at 6:39 pm in reply to: Run Batch Apex class without stopping in Salesforce?

    invoke the same batch in Finish method.

    global void finish(Database.BatchableContext BC){
    Database.executeBatch(new sampleBatch,1000);
    }

  • Shaik

    Member
    June 9, 2017 at 6:23 pm in reply to: How to throw error for Salesforce external Id field null values?

    To get External ID from outside sales force Environment

    1. Either you have to hit their API via soap/Rest API call or
    2. you have to provide them the login creds of your org and Object API name along with External Id API name as defined in sales force.

    for option 1 : you can write your own Integration code to check for the External Id null values where you can throw error saying "Following record Ids have null values" 🙂

  • Write a batch class to delete the records.

    Two ways of Executing the batch class :

    1. Execute it in "Anonymous" block or
    2. Create a VF page with a Delete button which invokes the batch class(with VF page you can catch the errors while performing delete operation and can show it on VF page as error messages)

Popular Salesforce Blogs