You need to sign in to do that
Don't have an account?
Neelofer
List<Account> accs=new List<Account>();
account a1=new account(name='gmail',phone='123');
account a2=new account(name='fb',phone='456');
account a3=new account(name='wtsup',phone='789');
accs.add(a1);
accs.add(a2);
accs.add(a3);
List<Contact> cons=new List<Contact>();
contact c1=new contact(LastName='gmail',phone='9030');
contact c2=new contact(LastName='fb',phone='7586');
contact c3=new contact(LastName='wtsup',phone='6935');
cons.add(c1);
cons.add(c2);
cons.add(c3);
insert accs;
for(account a:accs){
for(contact c:cons){
if(a.Name==c.LastName){
c.AccountId=a.Id;
}
}
}
insert cons;
I am getting error Apex cpu Limit Exceeded.....How to resolve this issue plz suggest me....
how to avoid apex cpu limit
List<Account> accs=new List<Account>();
account a1=new account(name='gmail',phone='123');
account a2=new account(name='fb',phone='456');
account a3=new account(name='wtsup',phone='789');
accs.add(a1);
accs.add(a2);
accs.add(a3);
List<Contact> cons=new List<Contact>();
contact c1=new contact(LastName='gmail',phone='9030');
contact c2=new contact(LastName='fb',phone='7586');
contact c3=new contact(LastName='wtsup',phone='6935');
cons.add(c1);
cons.add(c2);
cons.add(c3);
insert accs;
for(account a:accs){
for(contact c:cons){
if(a.Name==c.LastName){
c.AccountId=a.Id;
}
}
}
insert cons;
I am getting error Apex cpu Limit Exceeded.....How to resolve this issue plz suggest me....
Sorry for this issue you are encountering.
Since your code execution takes a lot of time than allowed limit, try refining your code by removing unnecessary for loop, for loop inside for loop. Utilize sets, maps advantages and refine your code. Also, you can move the piece of code that does not need to be executed in the same context to the future method.
The Maximum CPU time on the Salesforce servers - 10,000 milliseconds (Synchronous limit) 60,000 milliseconds(Asynchronous limit)
By doing everything mentioned above you can overcome this issue.
Hope this helps.
Kindly mark this as solved if the reply was helpful.
Thanks,
Nagendra