You need to sign in to do that
Don't have an account?
Uttpal 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.
{
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.
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
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
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.