function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Divyansh Verma 10Divyansh Verma 10 

Creating contacts that depend on an account field value

We need to create a trigger. So the situation is like therev is a field in Account called Number_of_Contacts__c(number field). When we update the Number_of_Contacts__c feild, then the same number of Contacts will be created associated with that Account.

There can be 2 cases for update trigger:
1>   new value>old value
2>   new value<old value

I have completed the 1st case successfully, however I am Stuck in the 2nd case. Can you guys help me out. I am posting my 1st case of code here:

if(trigger.isUpdate && trigger.isafter)
    {
          list<Contact> conta=new list<Contact>();      
         set<Id> co=new set<Id>();
        for(Account a1:trigger.new)
        {
            co.add(a1.id);
        }
        list<Account> acc=[select id,name,Number_of_Contacts__c from Account where id in: (co)];
      
        for(Account a2:acc)
        {
            if(a2.Number_of_Contacts__c>trigger.oldMap.get(a2.Id).Number_of_Contacts__c)
            {
                a2.Sales_Rep__c='greater is working';
                for(Decimal i=trigger.oldMap.get(a2.Id).Number_of_Contacts__c+1;i<=a2.Number_of_Contacts__c;i++)
                 {
                        Contact c=new Contact();
                        c.AccountId=a2.Id;
                        c.LastName='Default Con'+ i;
                        conta.add(c);
                 }   
                insert conta;
            }
            if(a2.Number_of_Contacts__c<trigger.oldMap.get(a2.Id).Number_of_Contacts__c)
            {
                a2.Sales_Rep__c='smaller is working';
                for(Decimal i=trigger.oldMap.get(a2.Id).Number_of_Contacts__c-1;i>=a2.Number_of_Contacts__c;i--)
                 {
                      
                  // PLEASE HELP ME OUT FOR THIS PART


                 }   
              
            }
        }
    }
 
viswanath reddy 53viswanath reddy 53
Hi divyansh,

It looks like our team of experts can help you resolve this ticket. We have Salesforce global help-desk support and you can log a case and our Customer Success Agents will help you solve this issue. You can also speak to them on live chat. Click on the below link to contact our help-desk. Trust me it is a support service that we are offering for free!

https://jbshelpdesk.secure.force.com  (https://jbshelpdesk.secure.force.com )
Thanks,
Jarvis SFDC team