• sfdc Beginner
  • NEWBIE
  • 220 Points
  • Member since 2014

  • Chatter
    Feed
  • 3
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 18
    Questions
  • 27
    Replies
Hi friends

I need to count the number of contacts for each account. so I created a custom field (Number_of_Contacts_c) on the account object.
For new accounts I have written code and its working fine. Also when I delete a contact in detail page of account  its workign fine.
There will be old accounts with no contacts records in detail page prior to this code.  I am stuck here as how to update this accounts and show the number of contacts records

apex class code:
public class CountContacts_InAccount
{
      public void CountContacts()
      {
          list<Account> noofcontacts = [select Id,Number_of_contacts__c from Account];
          list<Contact> conforcontacts = [select ID,AccountID from Contact];
          
          for (Account acct : noofcontacts ) 
          {
              list<ID> conIds = new list<ID>();

              for (Contact con : conforcontacts )
              {
                    if (con.AccountId == acct.Id)
                    {
                       conIds.add(con.Id);
                       acct.Number_of_contacts__c= conIds.size();
                     }
              }
         }
          Database.Update(noofcontacts);   
     }
}

trigger code:
trigger trg_CountContacts on Contact (after Insert,after Update,after Delete,after Undelete)
{
     CountContacts  obj = new CountContacts();
     
     if ((Trigger.IsInsert) || (Trigger.IsUndelete))
     {
         obj.CountContacts(Trigger.New);
     }
     
     if (Trigger.IsDelete)
     {
         obj.CountContacts(Trigger.Old);
     }
}

Can any one psl elt me know:?

Regards
sonali verma
I'm getting the following error in my batchable class. I want to pull all training records with an expiration date older than today and then ultimately mass update their Training Status to expired. I plan on making it schedulable. 

I'm getting the following error:

BatchUpdateTraining: Class must implement the global interface method: Iterable&lt;Training__c&gt; start(Database.BatchableContext) from Database.Batchable&lt;Training__c&gt;

Apex:
global class BatchUpdateTraining implements Database.Batchable<Training__c>{
    public String Query;

    public Database.QueryLocator start(Database.BatchableContext BC) 
    {   
        Query = 'SELECT id,Training_Status__c,Expiry_Date__c FROM Training__c WHERE Expiry_Date__c > Today';
        return Database.getQueryLocator(Query);
    }

    public void execute(Database.BatchableContext BC, List<Training__c> scope) 
    {   
        for (Training__c ac : scope) 
        {
        
        }
        update scope;
    }

    public void finish(Database.BatchableContext BC) { }    
}


 
hi

I have a custom controller in which i have placed the logic of the save method.
However after clicked on save i want to display the detail page of the newly inserted record to the user

How do i do it?

Any help would be appreciated

Thanks
I have a trigger for Which I need to Write a Test Class.

Trigger:
 
trigger AccountShareWhenOwnerChage on Account (after update) {

        Set<Id> OwnerChangedAccs = new Set<Id>();
        
        for(Account a : trigger.new){
            if(trigger.oldMap.ContainsKey(a.id)  && a.ownerid != trigger.oldMap.get(a.id).ownerid){
              OwnerChangedAccs.add(a.id);
           }
        }
    
        List<AccountShare> lstAccShare = new List<AccountShare>(); 
    
        for(Account_Share__c aShare : [SELECT Id, Account__c, User__c FROM Account_Share__c WHERE Account__c IN :OwnerChangedAccs]){
            AccountShare as = new AccountShare();
            as.UserOrGroupID = aShare.User__c;
            as.AccountId = aShare.Account__c;
            as.AccountAccessLevel = 'Edit';
            lstAccShare.add(as);
        }
        insert lstAccShare;
    }
    
}





Test Class:
@isTest
public class TestAccountOwnerChange{

    public static testMethod void unitTest(){
    
        Account a = new Account();
        a.name = 'Test Account';
        insert a;
        
         
        User u = new User();
        u.FirstName = 'Test';
        u.LastName  = 'User';
        u.Email     = 'testuser@gmail.com';
        u.Username  = 'testuser@gmail.com';
        u.Alias     = 'testy';
        u.ProfileId = '00ei0000001NKcq';
        u.TimeZoneSidKey    = 'America/Denver';
        u.LocaleSidKey      = 'en_US';
        u.EmailEncodingKey  = 'UTF-8';
        u.LanguageLocaleKey = 'en_US';
       
        insert u;
        
        Account ua = [SELECT Id,Name,Owenrid FROM Account WHERE Id = :a.id];
        ua.ownerid = u.id;
        update ua;
        
        
        Account_Share__c as = new Account_Share__c();
        as.Account__c = a.id;
        as.User__c = u.id;
        insert as;
        

        

        List<AccountShare> ashares = [SELECT Id, UserOrGroupId, AccountaccessLevel,

        RowCause FROM AccountShare WHERE AccountId = :as.Account__c AND UserOrGroupId= :u.Id];
        
        
        
        
        
        
        
        
    }

}


I have Written a Test Class for this  but it gives me only 60% code Coverage, Can you please help me in Increasing My Code Coverage
suggesting some changes.



Thanks,
SFDC Beginner



 
I have a trigger for Which I need to Write a Test Class.

Trigger:
 
trigger AccountShareWhenOwnerChage on Account (after update) {

        Set<Id> OwnerChangedAccs = new Set<Id>();
        
        for(Account a : trigger.new){
            if(trigger.oldMap.ContainsKey(a.id)  && a.ownerid != trigger.oldMap.get(a.id).ownerid){
              OwnerChangedAccs.add(a.id);
           }
        }
    
        List<AccountShare> lstAccShare = new List<AccountShare>(); 
    
        for(Account_Share__c aShare : [SELECT Id, Account__c, User__c FROM Account_Share__c WHERE Account__c IN :OwnerChangedAccs]){
            AccountShare as = new AccountShare();
            as.UserOrGroupID = aShare.User__c;
            as.AccountId = aShare.Account__c;
            as.AccountAccessLevel = 'Edit';
            lstAccShare.add(as);
        }
        insert lstAccShare;
    }
    
}
I have Written a Test Class for this  but it gives me only 60% code Coverage, Can you please help me in Increasing My Code Coverage
suggesting some changes.
 
@isTest
public class TestAccountOwnerChange{

    public static testMethod void unitTest(){
    
        Account a = new Account();
        a.name = 'Test Account';
        insert a;
        
         
        User u = new User();
        u.FirstName = 'Test';
        u.LastName  = 'User';
        u.Email     = 'testuser@gmail.com';
        u.Username  = 'testuser@gmail.com';
        u.Alias     = 'testy';
        u.ProfileId = '00ei0000001NKcq';
        u.TimeZoneSidKey    = 'America/Denver';
        u.LocaleSidKey      = 'en_US';
        u.EmailEncodingKey  = 'UTF-8';
        u.LanguageLocaleKey = 'en_US';
       
        insert u;
        
        Account ua = [SELECT Id,Name,Ownerid FROM Account WHERE Id = :a.id];
        ua.ownerid = u.id;
        update ua;
        
        
        Account_Share__c as = new Account_Share__c();
        as.Account__c = a.id;
        as.User__c = u.id;
        insert as;
        

        

        List<AccountShare> ashares = [SELECT Id, UserOrGroupId, AccountaccessLevel,

        RowCause FROM AccountShare WHERE AccountId = :as.Account__c AND UserOrGroupId= :u.Id];
        
        
        
        
        
        
        
     }

}


Thanks,
SFDC Beginner




 
How to Display 1 Million Records in a Page ?
How to Insert data from Organization1 to Organization2 ?
How Organization2 receives response from Organization1 ?
 
How to Send Response from Organization1 to Organization2 ?
How does Organization2 Receive the Request from Organization1 ?
How to Send a Request from Organization1 to Organization2 ?
Can we use Changesets to deploy components from one Production Environment to another Production Environment ?

What is the Use of QueryFactor Class and Where do we use it in Real Time ?
What is the Use of Type Class in Salesforce and Where do we Use it in Realtime?
How to Query the child records of parent object on which trigger is running and fetch the respective child records of the enitity.
How to move Relationships from Full Sandbox to Production ?
How to write a formula to calculate Age in Years Months and Days when DOB is given using formula filed in salesforce?
Why should the method annotated with future should be Static ?
How do client enter into our system after deployment ?
What is dynamic Apex? What are the uses? How is it different from normal Apex ? can anyone please throw some light on this.

Thanks in Advance
Can anyone please tell which amon the dataloaders is the best Informatica Cloud Data Loader, informatica cloud express,Jitterbit Loader.If you could tell the pros & cons it would be helpful.
Can anyone please tell which amon the dataloaders is the best Informatica Cloud Data Loader, informatica cloud express,Jitterbit Loader.If you could tell the pros & cons it would be helpful.
I have a object called Incident__c and a custom field called Notes__c (Long Text Area).

Whenever a New Note is added to a Incident__c record, I want to populate Notes__c field with Note body.

If there are several Notes added to the Incident__c record, Notes__c field should be appended with each Note bodies.

Once a Note body is updates Note__c field should also be updated accordingly.

I'm currently struggling to make progress on this as i'm new to this scenario! How can I do this ?
Is it possible to import queues from one salesforce org to the other? If yes, how can i do that?
Hi Developers,

Can anyone help me on this issue, I have a Child object that has a Lookup to Parent. I wrote the below apex class and the trigger on child, such that the count of Child records should be shown on each Parent record. I have a number field on the Parent which should be update as per the Trigger.

It works fine except in one scenario, it does not consider the existing Child records on the Parent, hence it shows incorrect count on the Parent record. It works perfect if I add the new Child records. I heard that Batch Apex can resolve this issue, I am not sure how Batch Apex is related here to resolve the isssue. Can I get some guidance here to proceed further.


Any help on this is much appreciated.
Thank you.

Apex Class:

public class ChildCountHelper{
    
    //List<Parent__c> parentList = [select id, child_count__c, (select id from child__r) from Parent__c where id in :parentIDSet];
    
    public List<ID> conList= new List<ID>();
    
    public static void handleBeforeInsert(List<Child__c> childList){
        Set<ID> parentIDSet = new Set<ID>();
        
        for(Child__c childRec: childList){
     
            parentIDSet.add(childRec.Parent__c);
            
        }
        
        Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
        for(Child__c childRec: childList){
            if(parentMap.get(childRec.Parent__c).child_count__c == null){
                parentMap.get(childRec.Parent__c).child_count__c = 0;
            }
            parentMap.get(childRec.Parent__c).child_count__c ++;
        }
        update parentMap.values();
    }
    
    public static void handleBeforeUpdate(List<Child__c> newChildList, List<Child__c> oldChildList){
        Set<ID> parentIDSet = new Set<ID>();
        Map<ID, ID> oldChildMap = new Map<ID, ID>();
        
        for(Child__c childRec: newChildList){
            parentIDSet.add(childRec.Parent__c);
        }
        
        for(Child__c childRec: oldChildList){
            parentIDSet.add(childRec.Parent__c);
            oldChildMap.put(childRec.Id, childRec.Parent__c);
        }
        
        Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
        for(Child__c childRec: newChildList)
       {
        /*if(ChildRec.Parent__c!=null){  */
            if(parentMap.get(childRec.Parent__c).child_count__c == null){
                parentMap.get(childRec.Parent__c).child_count__c = 0;
            }
           // }
            if(childRec.Parent__c != oldChildMap.get(childRec.id)){
                if(oldChildMap.get(childRec.id) == null && childRec.Parent__c != null){
                    parentMap.get(childRec.Parent__c).child_count__c ++;
                }else if(oldChildMap.get(childRec.id) != null && childRec.Parent__c == null){
                    parentMap.get(oldChildMap.get(childRec.id)).child_count__c --;
                }else if(oldChildMap.get(childRec.id) != null && childRec.Parent__c != null){
                    parentMap.get(oldChildMap.get(childRec.id)).child_count__c --;
                    parentMap.get(childRec.Parent__c).child_count__c ++;
                }
            }
            
        }
        update parentMap.values();
    }
    
    public static void handleBeforeDelete(List<Child__c> childList){
        Set<ID> parentIDSet = new Set<ID>();
        for(Child__c childRec: childList){
            parentIDSet.add(childRec.Parent__c);
        }
        
        Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
        for(Child__c childRec: childList){
            if(parentMap.get(childRec.Parent__c).child_count__c == null){
                parentMap.get(childRec.Parent__c).child_count__c = 0;
            }
            parentMap.get(childRec.Parent__c).child_count__c --;
        }
        update parentMap.values();
    }
    
    public static void handleBeforeUnDelete(List<Child__c> childList){
        Set<ID> parentIDSet = new Set<ID>();
        for(Child__c childRec: childList){
            parentIDSet.add(childRec.Parent__c);
        }
        
        Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
        for(Child__c childRec: childList){
            if(parentMap.get(childRec.Parent__c).child_count__c == null){
                parentMap.get(childRec.Parent__c).child_count__c = 0;
            }
            parentMap.get(childRec.Parent__c).child_count__c ++;
        }
        update parentMap.values();
    }

}




Trigger:


trigger ChildTrigger on Child__c (before insert, after update, after delete, after undelete) {
    if (Trigger.isInsert) {
        ChildCountHelper.handleBeforeInsert((List<Child__c>)Trigger.NEW);
    }else if (Trigger.isUpdate) {
        ChildCountHelper.handleBeforeUpdate((List<Child__c>)Trigger.NEW, (List<Child__c>)Trigger.OLD);
    }else if (Trigger.isDelete) {
        ChildCountHelper.handleBeforeDelete((List<Child__c>)Trigger.OLD);
    }else if (Trigger.isUndelete) {
        ChildCountHelper.handleBeforeUnDelete((List<Child__c>)Trigger.NEW);
    }
}  
Hi
  I wrote the trigger whenever i will update the phone field on the ACCOUNT object,respective phone field on the CONTACT object updates automatically.i posted the code below but it was not updating the phone field on CONTACT object.
trigger updatephone on Account (before update) {
 Map<id,account>accmap = new Map<id,account>();
    for(Account acc:trigger.new){
       accmap.put(acc.id,acc);
     }   
  List<contact>listcon = [SELECT id, Phone, accountid FROM contact WHERE accountid in :accmap.keyset()];
     for(Contact con :listcon){
          con.Phone = accmap.get(con.accountid).Phone;   
     }    
      update listcon;
    }
How to Query the child records of parent object on which trigger is running and fetch the respective child records of the enitity.
How to move Relationships from Full Sandbox to Production ?
Hello,

This is first time i will create report, i need advise on how should i achieve it.

1) I want to get the account details(Total number of account, also the name and siret of account) 
OF those accounts, who atleast have one opportunity. And the record type of that account should be "SYZ"
 
  • August 28, 2015
  • Like
  • 0
trigger instantTrigger on Opportunity (before insert,after update) {
    List<OpportunityShare> share=new List<OpportunityShare>();
    User u=[SELECT id FROM User WHERE alias='sara'];
    for(Opportunity op:Trigger.New){
        if(op.Type=='New Customer'){
            OpportunityShare so=new OpportunityShare();
            so.OpportunityId=op.id;
            so.OpportunityAccessLevel='Edit';
            so.UserOrGroupId=u.Id;
            so.RowCause='Manual';
            share.add(so);
        }
    }
    insert share;
}

Error:Apex trigger instantTrigger caused an unexpected exception, contact your administrator: instantTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []: Trigger.instantTrigger: line 14, column 1
 
Hi,
Can i, downgrade access using permission set in salesforce ?
 
Hi friends

I need to count the number of contacts for each account. so I created a custom field (Number_of_Contacts_c) on the account object.
For new accounts I have written code and its working fine. Also when I delete a contact in detail page of account  its workign fine.
There will be old accounts with no contacts records in detail page prior to this code.  I am stuck here as how to update this accounts and show the number of contacts records

apex class code:
public class CountContacts_InAccount
{
      public void CountContacts()
      {
          list<Account> noofcontacts = [select Id,Number_of_contacts__c from Account];
          list<Contact> conforcontacts = [select ID,AccountID from Contact];
          
          for (Account acct : noofcontacts ) 
          {
              list<ID> conIds = new list<ID>();

              for (Contact con : conforcontacts )
              {
                    if (con.AccountId == acct.Id)
                    {
                       conIds.add(con.Id);
                       acct.Number_of_contacts__c= conIds.size();
                     }
              }
         }
          Database.Update(noofcontacts);   
     }
}

trigger code:
trigger trg_CountContacts on Contact (after Insert,after Update,after Delete,after Undelete)
{
     CountContacts  obj = new CountContacts();
     
     if ((Trigger.IsInsert) || (Trigger.IsUndelete))
     {
         obj.CountContacts(Trigger.New);
     }
     
     if (Trigger.IsDelete)
     {
         obj.CountContacts(Trigger.Old);
     }
}

Can any one psl elt me know:?

Regards
sonali verma
I'm getting the following error in my batchable class. I want to pull all training records with an expiration date older than today and then ultimately mass update their Training Status to expired. I plan on making it schedulable. 

I'm getting the following error:

BatchUpdateTraining: Class must implement the global interface method: Iterable&lt;Training__c&gt; start(Database.BatchableContext) from Database.Batchable&lt;Training__c&gt;

Apex:
global class BatchUpdateTraining implements Database.Batchable<Training__c>{
    public String Query;

    public Database.QueryLocator start(Database.BatchableContext BC) 
    {   
        Query = 'SELECT id,Training_Status__c,Expiry_Date__c FROM Training__c WHERE Expiry_Date__c > Today';
        return Database.getQueryLocator(Query);
    }

    public void execute(Database.BatchableContext BC, List<Training__c> scope) 
    {   
        for (Training__c ac : scope) 
        {
        
        }
        update scope;
    }

    public void finish(Database.BatchableContext BC) { }    
}


 
Hello all,

Methods defined as TestMethod do not support Web service callouts, test skipped

can anybody tell what that mean.
 
@isTest
public class TestSetIsCopiedToProjectFlagFalse {
 static testmethod void UnitTest()
    {
      
         Account objAcc = new Account(Name = 'karthik test');
          insert objAcc;
     
    Ibanking_Project__c  objIbanking_Project = new Ibanking_Project__c();
       objIbanking_Project.Name='karthiktest trigger';
        objIbanking_Project.Company__c=objAcc.Id;
          Insert objIbanking_Project;

          Delete objIbanking_Project;
    
    }
}

 
Hi all,

Can someone help me to answer the below question.

Which element is included in security model of Force.com sites. Choose two answers
A. Full CRUD Permissions on all custom objets
B. Read/Write Permissions on all standard objects
C. Named visualforce pages
D. Restricted IP ranges

R - Manu
how to write soql query to get the records from account object which are not modified since one year?
  • September 03, 2014
  • Like
  • 1