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
Uttpal chandraUttpal chandra 

While learning trigger came up this problem in programming.

trigger trg4 on Account (after insert) 
{
    Map<Id,Decimal> map1 = new Map<Id,Decimal>();
    for(Account a : trigger.new)
    {
        map1.put(a.id, a.No_of_Contacts__c);
    }
    
    
   List<Contact> con = new List<Contact>();
    if(trigger.isinsert && trigger.isafter)
    {
        if(map1.size() > 0 && map1 !=null)
        {
            for(Id a: map1.keySet())
            {
                for(integer i=1;i<=map1.get(a) ; i++)
                {
                    Contact c = new Contact();
                    c.LastName='Demo Contact';
                    c.AccountId=a;
                    c.Phone='454646';  
                    con.add(c);
                }
            }
        }
        INSERT con;
    }
}


Errors:
Variable does not exist: LastName
Variable does not exist: AccountId
Variable does not exist: Phone
DML requires SObject or SObject list type: List


Kindly help me to solve this problem in trigger.
Best Answer chosen by Uttpal chandra
Om PrakashOm Prakash
Hello,
It seems your org has one apex class name Contact.
Find that custom class created in your org and rename that, then save your trigger.

Addtionally, avoid using loop within loop in apex code.

Feel free to ask if any queries.
 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

Check If you any other apex class with the name Account in your org, If so delete that class.

Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 
Om PrakashOm Prakash
Hello,
It seems your org has one apex class name Contact.
Find that custom class created in your org and rename that, then save your trigger.

Addtionally, avoid using loop within loop in apex code.

Feel free to ask if any queries.
 
This was selected as the best answer
Uttpal chandraUttpal chandra
Thanks OM Praksh yes there was a class name Contact which was previously created in the same org.