• sagar077
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 13
    Questions
  • 14
    Replies
Hi

Create an Apex trigger that will count the number of contacts associated with an account(create a field at account level). Must update the count in insertion and deletion of contact by using Map.

I am done with using map but i am unable to complete one case ie- if account having associated with 2 contacts and I changed the Account Name 'A' to Account 'B' then the count of contact fields not update the count of contact.

Pleases help me in code
Thanks 

trigger AccountCountContactMap on Contact (after insert,after update, after delete)
{
        Map <Id, List<Contact>> mapAcctIdContactList = new Map <Id, List<Contact>>();
        Map <Id, List<Contact>> mapAcctIdDelContactList = new Map <Id, List<Contact>>();
    
        Set<Id> AcctIds = new Set<Id>();
        List<Account> listAcc = new List<Account>();
      
    if(trigger.isinsert)
        {
            for(Contact Con : trigger.new)
                {
                    if(string.isNotBlank(Con.AccountId))
                    {
                        if(!mapAcctIdContactList.containsKey(Con.AccountId))
                        {
                            mapAcctIdContactList.put(Con.AccountId, new List<Contact>());        
                        }
                            mapAcctIdContactList.get(Con.AccountId).add(Con);
                            AcctIds.add(Con.AccountId);
                    }
                }
        }
    
    if(trigger.isupdate)
    {
        for(Contact Con:trigger.new)
            {
                 if(string.isNotBlank(Con.AccountId) && Con.AccountId !=trigger.oldmap.get(Con.Id).AccountId)
                    {
                        if(!mapAcctIdContactList.containsKey(Con.AccountId))
                        {
                            mapAcctIdContactList.put(Con.AccountId, new list <Contact>());
                        }
                            mapAcctIdContactList.get(Con.AccountId).add(Con);
                            AcctIds.add(Con.AccountId);
                   }
                    else if(string.isBlank(Con.AccountId) && string.isNotBlank(trigger.oldmap.get(Con.Id).AccountId))
                        {
                            if(!mapAcctIdDelContactList.containsKey(Con.AccountId))
                            {
                                 mapAcctIdDelContactList.put(Con.AccountId,new list<Contact>());   
                            }
                          mapAcctIdDelContactList.get(Con.AccountId).add(Con);
                          AcctIds.add(trigger.oldMap.get(Con.Id).AccountId);
                        }
            }
    }
 
     if(trigger.isdelete)
         {
            for(Contact Con : trigger.Old)
                {
                    if(string.isNotBlank(Con.AccountId))
                        {
                            if(!mapAcctIdDelContactList.containsKey(Con.AccountId))
                                {
                                    mapAcctIdDelContactList.put(Con.AccountId, new List<Contact>());
                                }
                            mapAcctIdDelContactList.get(Con.AccountId).add(Con);
                            AcctIds.add(Con.AccountId);
                        }
                }
         }
    
    if(AcctIds.size() > 0)
        {
            listAcc =[Select Id,Number_Of_Contact_Count__c from Account where Id in : AcctIds];
            for(Account acct : listAcc)
                {
                    Integer noofConts=0;
                    if(mapAcctIdContactList.containsKey(acct.Id))
                        {
                            noofConts += mapAcctIdContactList.get(acct.Id).size();
                        }
                    if(mapAcctIdDelContactList.containsKey(acct.Id))
                        {
                            noofConts -= mapAcctIdDelContactList.get(acct.Id).size();    
                        }
                    acct.Number_Of_Contact_Count__c = acct.Number_Of_Contact_Count__c == null ? noOfConts : (acct.Number_Of_Contact_Count__c + noOfConts);
                }
            update listAcc;
        }
}

plz help me
Create an Apex trigger that will count the number of contacts associated with an account(create a field at account level). Must update the count in insertion and deletion of contact by using MAP Collection.
Create an Apex Trigger that will count the number of contacts associated with an account(create a field at account level). Must update the count in insertion and deletion of a contact.

CODE-

trigger CountonAccountofcontact on Contact (after insert,after delete)
{
    Set<Id> mysetid = new Set <Id>();
        if(Trigger.isinsert)
        {
            System.debug('Insert new contact for trigger.new '+ Trigger.new);
            for(Contact contac :trigger.new)
                {
                    mysetid.add(contac.Accountid);
                }
            List<Account> Acc = [Select Id,Number_Of_Contact_Count__c from Account where Id in : mysetid];
            List<Contact> Con = [Select Id from Contact where Accountid in : mysetid];
            for(Account A: Acc)
                {
                    A.Number_Of_Contact_Count__c = Con.size(); 
                }
            update Acc;
            System.debug('Number of count is ' + Acc);
        }

     if(Trigger.isdelete)
        {
            System.debug('The Delete Contact Name For Trigger.old'+ Trigger.Old); 
            for(Contact contac : Trigger.Old)
                {
                    mysetid.add(contac.Accountid);
                }          
            List<Account> Acc = [Select id,Number_Of_Contact_Count__c from Account where id in: mysetid];
            List<Contact> Con = [Select id from Contact where Accountid in : mysetid];
           
            for(Account A :Acc)
                {
                    A.Number_Of_Contact_Count__c = Con.size();
                }
                update Acc;
            System.debug('The Update number is '+ Acc);
        }
    }

NOTE- This code is running but I want for After Update event also and Plz Help me in that 
Hello, Please me on this requirement.

Count Account with Different Industry(on account picklist fields is there)(e,g. For Industry Electronics we have 3 Accounts) using Map.

plz help me i am new in apex collection.
Hello, Please me on this requirement. 

Count Account with Different Industry(fields on account)(e,g. For Industry Electronics we have 3 Accounts) using Map.

Please help me I am new in Apex collection.
thankss

 
Hello, Please me on this requirement. Create a set to find a unique industry(field on account) from the account. 
Check this code and tell me where I am wrong.

Set <Account> indust = new Set <Account> ();
Account [] newindus = [SELECT Id,Name FROM Account WHERE Industry = 'Agriculture'];                  
set<Account> aa = new set<Account>();
for (Account aa : indust)
{
indust.add(aa.Industry);
}
System.debug('Name The Unique Industry ' + aa );

I am not getting the error please help me ..thanks
Write an apex trigger for update custom field Details_oF_Product__c (text area datatype) .
after insert, after update , after delete this events want to use.
On the Opportunity object, there is a releted list name as Products (Standard) it will take record data from there and show on custom fields name as Details_oF_Product__c (text area datatype).
Three fields should be show name as Product Code, List Price, Quantity.

Plz help me in this trigger i am new in trigger.
thanks

The result will show on the custom field that is  Details_oF_Product__c (text area datatype)
----------------------------------------
----------------------------------------
Product Code : beds 
List Price :200
Quantity : 1 
----------------------------------------
----------------------------------------User-added imageUser-added image
 
Write an apex trigger for update custom field Details_oF_Product__c (text area datatype) .
after insert, after update , after delete this events want to use.
On the Opportunity object, there is a releted list name as Products (Standard) it will take record data from there and show on custom fields name as Details_oF_Product__c (text area datatype).
Three fields should be show name as Product Code, List Price, Quantity.

Plz help me in this trigger i am new in trigger.
thanks

The result will show on the custom field that is  Details_oF_Product__c (text area datatype)
----------------------------------------
----------------------------------------
Product Code : beds 
List Price :200
Quantity : 1 
----------------------------------------
---------------------------------------- 
Product Code : black
List Price : 400 
Quantity : 2
----------------------------------------
---------------------------------------
Write an apex trigger for update custom field Details_oF_Product__c (text area datatype) it will take value from Line Item Opportunity Related list Quotes. 
Want to perform (insert, update , delete) event.
Please help me I am new in the apex.
thanks..

Result on show on custom field Details_oF_Product__c (text area datatype)
----------------------------------------
----------------------------------------
Sr No : 1
Product Code : beds 
List Price :200
Quantity : 1 
----------------------------------------
---------------------------------------- 
Sr No : 2
Product Code : black
List Price : 400 
Quantity : 2
----------------------------------------
----------------------------------------
Sr No : 3
Product Code : bunk bed 
List Price : 500
Quantity : 1
----------------------------------------
--------------------------------------
Write an apex trigger for update custom field Details_oF_Product__c (text area datatype) it will take value from Line Item Opportunity Related list Quotes. 
Want to perform (insert, update , delete) event.
Please help me I am new in the apex.
thanks..

Result on show on custom field Details_oF_Product__c (text area datatype)
----------------------------------------
----------------------------------------
Sr No : 1
Product Code : beds 
List Price :200
Quantity : 1 
----------------------------------------
---------------------------------------- 
Sr No : 2
Product Code : black
List Price : 400 
Quantity : 2
----------------------------------------
----------------------------------------
Sr No : 3
Product Code : bunk bed 
List Price : 500
Quantity : 1
----------------------------------------
---------------------------------------
If Order Type (picklistlist data type) ='New ' then on Master_Order_ Number__c (number data type)> will populate > "New number of Order Number (Auto Number data type)"
Create a button on Quote 'Populate Root Id', must work for Classic and Lightning both
Create a field on QuoteLine "Root Id" Lookup (QuoteLine)
On click of this Button we need to populate the Parent bundle Id (Parent Quote Line Item) on every child Lines
Input to your Class will be QuoteId from Quote Detail page.
NOTE: You need to identify fields at QuoteLine level where you can have immediate Parent QuoteLine Id and Bundle Product
 
For Example :
Quote Q-00001 :
QuoteLines:
QL-00001 (Product A)
QL-00002 (Product B)
QL-00003 (Product C)
QL-00004 (Product D)
QL-00005 (Product E)
QL-00006 (Product F)
QL-00007 (Product G)
QL-00008 (Product H)
QL-00009 (Product I)
QL-00010 (Product H)
QL-00011 (Product J)
QL-00012 (Product K)
QL-00012 (Product L)
QL-00013 (Product M)
QL-00014 (Product N)
QL-00015 (Product O)
QL-00016 (Product P)
QL-00016 (Product Q)
QL-00001 (Product C)
Output: Highlighted in Green
Quote Q-00001 :
QuoteLines:
QL-00001 (Product A) [ROOT_ID : Null]
QL-00002 (Product B) [ROOT_ID : QL-00001]
QL-00003 (Product C) [ROOT_ID : QL-00001]
QL-00004 (Product D) [ROOT_ID : QL-00001]
QL-00005 (Product E) [ROOT_ID : QL-00001]
QL-00006 (Product F) [ROOT_ID : QL-00001]
QL-00007 (Product G) [ROOT_ID : QL-00001]
QL-00008 (Product H) [ROOT_ID : QL-00001]
QL-00009 (Product I) [ROOT_ID : QL-00001]
QL-00010 (Product H) [ROOT_ID : QL-00001]
QL-00011 (Product J) [ROOT_ID : QL-00001]
QL-00012 (Product K) [ROOT_ID : QL-00001]
QL-00012 (Product L) [ROOT_ID : QL-00001]
QL-00013 (Product M) [ROOT_ID : NULL]
QL-00014 (Product N) [ROOT_ID : QL-00013]
QL-00015 (Product O) [ROOT_ID : QL-00013]
QL-00016 (Product P) [ROOT_ID : QL-00013]
QL-00016 (Product Q) [ROOT_ID : QL-00013]
QL-00001 (Product C) [ROOT_ID : QL-NULL]

Please help me i am new apex code
Thank You
Create a button on Quote 'Populate Root Id', must work for Classic and Lightning both
Create a field on QuoteLine "Root Id" Lookup (QuoteLine)
On click of this Button we need to populate the Parent bundle Id (Parent Quote Line Item) on every child Lines
Input to your Class will be QuoteId from Quote Detail page.
NOTE: You need to identify fields at QuoteLine level where you can have immediate Parent QuoteLine Id and Bundle Product
 
For Example :
Quote Q-00001 :
QuoteLines:
  • QL-00001 (Product A)
  • QL-00002 (Product B)
  • QL-00003 (Product C)
  • QL-00004 (Product D)
  • QL-00005 (Product E)
  • QL-00006 (Product F)
  • QL-00007 (Product G)
  • QL-00008 (Product H)
  • QL-00009 (Product I)
  • QL-00010 (Product H)
  • QL-00011 (Product J)
  • QL-00012 (Product K)
  • QL-00012 (Product L)
  • QL-00013 (Product M)
  • QL-00014 (Product N)
  • QL-00015 (Product O)
  • QL-00016 (Product P)
  • QL-00016 (Product Q)
  • QL-00001 (Product C)
Output: Highlighted in Green
Quote Q-00001 :
QuoteLines:
QL-00001 (Product A) [ROOT_ID : Null]
QL-00002 (Product B) [ROOT_ID : QL-00001]
QL-00003 (Product C) [ROOT_ID : QL-00001]
QL-00004 (Product D) [ROOT_ID : QL-00001]
QL-00005 (Product E) [ROOT_ID : QL-00001]
QL-00006 (Product F) [ROOT_ID : QL-00001]
QL-00007 (Product G) [ROOT_ID : QL-00001]
QL-00008 (Product H) [ROOT_ID : QL-00001]
QL-00009 (Product I) [ROOT_ID : QL-00001]
QL-00010 (Product H) [ROOT_ID : QL-00001]
QL-00011 (Product J) [ROOT_ID : QL-00001]
QL-00012 (Product K) [ROOT_ID : QL-00001]
QL-00012 (Product L) [ROOT_ID : QL-00001]
QL-00013 (Product M) [ROOT_ID : NULL]
QL-00014 (Product N) [ROOT_ID : QL-00013]
QL-00015 (Product O) [ROOT_ID : QL-00013]
QL-00016 (Product P) [ROOT_ID : QL-00013]
QL-00016 (Product Q) [ROOT_ID : QL-00013]
QL-00001 (Product C) [ROOT_ID : QL-NULL]

Please help me i am new apex code
Thank You
Create an Apex trigger that will count the number of contacts associated with an account(create a field at account level). Must update the count in insertion and deletion of contact by using MAP Collection.
Hello, Please me on this requirement. Create a set to find a unique industry(field on account) from the account. 
Check this code and tell me where I am wrong.

Set <Account> indust = new Set <Account> ();
Account [] newindus = [SELECT Id,Name FROM Account WHERE Industry = 'Agriculture'];                  
set<Account> aa = new set<Account>();
for (Account aa : indust)
{
indust.add(aa.Industry);
}
System.debug('Name The Unique Industry ' + aa );

I am not getting the error please help me ..thanks
Write an apex trigger for update custom field Details_oF_Product__c (text area datatype) .
after insert, after update , after delete this events want to use.
On the Opportunity object, there is a releted list name as Products (Standard) it will take record data from there and show on custom fields name as Details_oF_Product__c (text area datatype).
Three fields should be show name as Product Code, List Price, Quantity.

Plz help me in this trigger i am new in trigger.
thanks

The result will show on the custom field that is  Details_oF_Product__c (text area datatype)
----------------------------------------
----------------------------------------
Product Code : beds 
List Price :200
Quantity : 1 
----------------------------------------
----------------------------------------User-added imageUser-added image
 
Write an apex trigger for update custom field Details_oF_Product__c (text area datatype) it will take value from Line Item Opportunity Related list Quotes. 
Want to perform (insert, update , delete) event.
Please help me I am new in the apex.
thanks..

Result on show on custom field Details_oF_Product__c (text area datatype)
----------------------------------------
----------------------------------------
Sr No : 1
Product Code : beds 
List Price :200
Quantity : 1 
----------------------------------------
---------------------------------------- 
Sr No : 2
Product Code : black
List Price : 400 
Quantity : 2
----------------------------------------
----------------------------------------
Sr No : 3
Product Code : bunk bed 
List Price : 500
Quantity : 1
----------------------------------------
--------------------------------------
After copying from Trailhead ; I'm recieving an error for line 9. expecting right curly bracket , found "insert". Please advise.

// Create a list of contacts
02List<Contact> conList = new List<Contact> {
03    new Contact(FirstName='Joe',LastName='Smith',Department='Finance'),
04        new Contact(FirstName='Kathy',LastName='Smith',Department='Technology'),
05        new Contact(FirstName='Caroline',LastName='Roth',Department='Finance'),
06        new Contact(FirstName='Kim',LastName='Shain',Department='Education')};
07             
08// Bulk insert all contacts with one DML call
09insert conList;
10 
11// List to hold the new contacts to update
12List<Contact> listToUpdate = new List<Contact>();
13 
14// Iterate through the list and add a title only
15//   if the department is Finance
16for(Contact con : conList) {
17    if (con.Department == 'Finance') {
18        con.Title = 'Financial analyst';
19        // Add updated contact sObject to the list.
20        listToUpdate.add(con);
21    }
22}
23 
24// Bulk update all contacts with one DML call
25update listToUpdate;
trigger should work for after insert , after update, after delete, after undelete 
 Thanks  in advance
Hi! 
I've created a trigger under contacts to populate a custom field on the Account object that says how many contacts are under that one account. It's been uploaded through sandbox, but it doesn't seem to be working. Here's the code I'm using: 

Trigger NumberContacts on Contact(after update,after insert){
    Set<ID> accids=New Set<ID>();
    For(contact c:trigger.new){
        Accids.add(c.AccountId);
    }

    Map<ID,Account> Acc= new Map<ID,Account>([Select ID, No_of_Contacts_SFDC__c from account where id IN:accids ]);
    Map<ID,Account> updateMap = new Map<ID,Account>();
    for(Account ac :Acc.values())
    {
        Integer count = 0;
        for(Contact c:Trigger.New){
            if(ac.Id == c.AccountId)
                count = count + 1;
            
        }
        ac.No_of_Contacts_SFDC__c= count;
        updateMap.put(ac.Id,ac);
    }

    if(!updateMap.isEmpty())
        update updateMap.values();
}

Is there something wrong with the code? The field updates to 1 the first time I add a contact, and then nothing updates after that. Not sure what's going on. Thanks! 
Hi All,

Does anyone have any idea to how to count the number of occurrences of all the elements present in a list ? For Example, I have a list of names.
List is having 10 elements with values {A,B,C,D,A,B,A,A,A,A}. Now here A came 6 times, B came 2 times , C came 1 time and D also 1 time. But I don't know how to do it in apex.
Please help how can I get the count of repeated values in apex.
trigger CountFemales1 on Contact (after insert,after update,after delete)
{
set<id>  conId = new set<id>();

 if(trigger.isInsert||trigger.isUpdate)
 {
  for(contact c: trigger.new)
  {
   if(c.accountid!=null)
   {
   conId.add(c.accountid);
   }
  }
 }
 
 
 if(trigger.isDelete)
 {
 for(contact c:trigger.old)
 {
  if(c.accountid!=null)
  {
  conId.add(c.accountid);
  }
 }
 
 
list<account> FemaleList = [select id  ,(select Accountid, Sex__c from contacts where Sex__c='Female') from account where id In :conId];
 map<id,integer> countMap = new map<id,integer>();
 for(account a :FemaleList)
 {
 countMap.put(a.id, a.contacts.size());
 }
 
 
 list<account> countlist = [select Count_of_Females__c from account where id in :conId];
 for(account a : countlist)
 {
 //Integer countoffemale = countMap.get(a.id);
 //a.Count_of_Females__c= countoffemale;
  a.Count_of_Females__c= countMap.get(a.id);
 }
 update countList;

}  
    
}


I want that it should display the count of no. of contacts where sex = female 
it m able to  save the trigger but  there is no result  displayed ( its not displaying the count of females in account , even though i have contacts where sex= female in the picklist)

any help?
Hi All,

I want to display number of contacts associated with an account using triggers.

for this I had created a lookup field like noofcontacts__c in account  object. and Wrote code as

trigger numberofcontacts on contact(after insert, after update, after delete) {
    Map<Id, List<Contact>> AcctContactList = new Map<Id, List<Contact>>();
    Set<Id> AcctIds = new Set<Id>();   
    List<schema.Account> AcctList = new List<schema.Account>();
    List<schema.Contact> ConList = new List<schema.Contact>();
   
    if(trigger.isInsert || trigger.isUPdate) {
        for(Contact Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId)){
                AcctIds.add(Con.AccountId); 
            }  
        } 
    }
   
    if(trigger.isDelete) {
        for(Contact Con : trigger.Old) {
            AcctIds.add(Con.AccountId);    
        } 
    }          
   
    if(AcctIds.size() > 0){
        ConList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN : AcctIds];
       
        for(Contact Con : ConList) {
            if(!AcctContactList.containsKey(Con.AccountId)){
                AcctContactList.put(Con.AccountId, new List<Contact>());
            }
            AcctContactList.get(Con.AccountId).add(Con);     
        }                          
      
           
        AcctList = [SELECT noofContacts__c FROM Account WHERE Id IN : AcctIds];
        for(Account Acc : AcctList) {
            List<schema.Contact> ContList = new List<schema.Contact>();
            ContList = AcctContactList.get(Acc.Id);
            Acc.Number_of_Contacts__c = ContList.size();
        }   
       
      
        update AcctList;   
    }

}
 I am   getting an error as "Variable doesnot exist:id".

Kindly support and suggest.

Thanks

To take the example from the docs:

AggregateResult[] groupedResults = [SELECT CampaignId, AVG(Amount) FROM Opportunity 
GROUP BY CampaignId];

Now try

Map<ID, AggregateResult> resultsMap = [SELECT CampaignId, AVG(Amount) 
 FROM Opportunity 
 GROUP BY CampaignId]; 

This won't work even though CampaignId is of type ID. To make it work, alias CampaignId with Id:

Map<ID, AggregateResult> resultsMap = [SELECT CampaignId Id, AVG(Amount) 
 FROM Opportunity 
 GROUP BY CampaignId]; 

Interestingly, the case here does matter. Aliasing with id or ID won't work.

 

This worked on March 28th 2012 (seriously, I swear it did! :-), but does not any more.

 

Please support my idea at https://sites.secure.force.com/success/ideaView?id=08730000000hmhOAAQ to reinstate this behavior.