• Divyansh Verma 10
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 6
    Questions
  • 1
    Replies
Write a trigger to make an autonumber field on Account with the format "ACC-001".This field will increment sequentially as (ACC-002, ACC-003, .....) everytime when a new account is inserted.

Thanks.
Hi,
I am getting an error in below code. Could you please help me out.

if(trigger.isUnDelete && trigger.isafter)
    {
        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)  ALL ROWS];
        undelete acc;
    }


Thanks.
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


                 }   
              
            }
        }
    }
 
Guys, Could you please suggest me any blogs or sites or links where I can learn Lightning UI from scratch other than trailhead. It would be great help.

Thank You.
Requirement: Account1 has a attachment and I need to tranfer the particular attachment to the Account2 and delete the the Account1. So how can we do this by using trigger?
Hi All,

I am new in apex coding, I am trying to write trigger and this is my code.

My requirement is whenever I am creating new visitor record in the system( when I select multiselect picklist value which is 'Feve' and 'Dry Cough') new patient record should create under patient record.

My code is working fine, but patient record creates with ID, not name.

When I select only 1 value from the mutli-select picklist, then patient record creates with name.
trigger VisitorChangedIntoPatient on Visitor__c (after insert) {

    List<Patient__c> pat = new List<Patient__c>();
    
    for(Visitor__c visitor : Trigger.New)
    {
         Patient__c p = new Patient__c();
        
        if(trigger.isInsert && Visitor.Covid_19_Symtomp__c=='Fever' && Visitor.Covid_19_Symtomp__c=='DryCough')
        {
            p.id=visitor.id;
            p.Name=visitor.Name+'SQC';
            p.IsInfected__c=TRUE;
        }
        pat.add(p);
       
    }
    
    insert pat;
    
    
}
List<Student__c>obj1 = [Select Name,Student_Name__c From Student__c  Where  Student_Name__c Like '_r%' And
                               isDeleted = true All Rows];
        Database.emptyRecycleBin(obj1);