You need to sign in to do that
Don't have an account?
sampath palli
when i write a after insert trigger it's firing error
Hi
I have writen a trigger for two custom objects like registration and rumexpenses and i given lookup relation between these two objects i have an requirement when the record created in registration object with field rum name then automatically new record is created in rumexpenses object also. to achive this i have writen a after insert trigger but it not working it fires error. please check my code
Apex class:
public class Rumapex {
public String rumname{set;get;}
public String password{set;get;}
public String email{set;get;}
public list<Registartion__c> result{set;get;}
public pagereference register(){
pageReference p=new pageReference('/apex/RegistrationPage_Rum');
return p;
}
public pageReference go(){
pageReference p=new pageReference('/apex/Home');
return p;
}
public PageReference login(){
pageReference p=new pageReference('/apex/loginpage_Rum');
return p;
}
public pageReference forgetpwd(){
pageReference p=new pageReference('/apex/forgetpwd_Rum');
return p;
}
public pageReference search(){
Integer regcount=[select count() from Registartion__c where Rum_Name__c=:rumname];
if(regcount==0){
return create();
}else{
Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.WARNING,'Records are already existed'));
return null;
}
}
public PageReference create(){
Registartion__c r=new Registartion__c();
r.Rum_Name__c=rumname;
r.Password__c=password;
r.E_Mail__c=email;
insert r;
PageReference p=new PageReference('/apex/loginpage_Rum');
ApexPages.Message msg=new ApexPages.message(ApexPages.Severity.CONFIRM,'Registration completed successfully');
ApexPages.addMessage(msg);
return p;
}
public void clear(){
rumname=null;
password=null;
email=null;
}
public pageReference getdetails(){
result=[select Rum_Name__c,Password__c from Registartion__c where Rum_Name__c = :rumname and Password__c=:password];
if(result.size()>0){
return go();
}
else{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter valid credentials'));
return null;
}
}
public void getpwd(){
result=[select Rum_Name__c,Password__c,E_Mail__c from Registartion__c where E_Mail__c=:email];
if(result.size()>0){
rumname=result[0].Rum_Name__c;
password=result[0].Password__c;
email=result[0].E_Mail__c;
}
else{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Enter registred E-mail'));
}
}
}
Trigger:
trigger RumnameTrigger on Registartion__c (after insert) {
list<Rum_Expenses__c>exp=new list<Rum_Expenses__c>();
for(Registartion__c reg:Trigger.new){
Rum_Expenses__c ru=new Rum_Expenses__c();
ru.Id=reg.Id;
ru.Rum_Name__c=reg.Rum_Name__c;
exp.add(ru);
}
insert exp;
}
when records are inserted through vf page it shoes error like this
When records are inserted in sobject
Thanks in Advance
I have writen a trigger for two custom objects like registration and rumexpenses and i given lookup relation between these two objects i have an requirement when the record created in registration object with field rum name then automatically new record is created in rumexpenses object also. to achive this i have writen a after insert trigger but it not working it fires error. please check my code
Apex class:
public class Rumapex {
public String rumname{set;get;}
public String password{set;get;}
public String email{set;get;}
public list<Registartion__c> result{set;get;}
public pagereference register(){
pageReference p=new pageReference('/apex/RegistrationPage_Rum');
return p;
}
public pageReference go(){
pageReference p=new pageReference('/apex/Home');
return p;
}
public PageReference login(){
pageReference p=new pageReference('/apex/loginpage_Rum');
return p;
}
public pageReference forgetpwd(){
pageReference p=new pageReference('/apex/forgetpwd_Rum');
return p;
}
public pageReference search(){
Integer regcount=[select count() from Registartion__c where Rum_Name__c=:rumname];
if(regcount==0){
return create();
}else{
Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.WARNING,'Records are already existed'));
return null;
}
}
public PageReference create(){
Registartion__c r=new Registartion__c();
r.Rum_Name__c=rumname;
r.Password__c=password;
r.E_Mail__c=email;
insert r;
PageReference p=new PageReference('/apex/loginpage_Rum');
ApexPages.Message msg=new ApexPages.message(ApexPages.Severity.CONFIRM,'Registration completed successfully');
ApexPages.addMessage(msg);
return p;
}
public void clear(){
rumname=null;
password=null;
email=null;
}
public pageReference getdetails(){
result=[select Rum_Name__c,Password__c from Registartion__c where Rum_Name__c = :rumname and Password__c=:password];
if(result.size()>0){
return go();
}
else{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter valid credentials'));
return null;
}
}
public void getpwd(){
result=[select Rum_Name__c,Password__c,E_Mail__c from Registartion__c where E_Mail__c=:email];
if(result.size()>0){
rumname=result[0].Rum_Name__c;
password=result[0].Password__c;
email=result[0].E_Mail__c;
}
else{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Enter registred E-mail'));
}
}
}
Trigger:
trigger RumnameTrigger on Registartion__c (after insert) {
list<Rum_Expenses__c>exp=new list<Rum_Expenses__c>();
for(Registartion__c reg:Trigger.new){
Rum_Expenses__c ru=new Rum_Expenses__c();
ru.Id=reg.Id;
ru.Rum_Name__c=reg.Rum_Name__c;
exp.add(ru);
}
insert exp;
}
when records are inserted through vf page it shoes error like this
When records are inserted in sobject
Thanks in Advance
Please check the below modified trigger:
We shouldn't use Id while inserting the records.
Please do let me know if it helps you.
Regards,
Mahesh
Another way to write:
If there is a relationship between Registration and Rum_Expenses then you can write like below:
Please do let me know if it helps you.
Regards,
Mahesh
You are trying to insert Rum_Expenses__c object record while providing registration__c id to Rum_Expenses__c id.
Remove this line from your trigger :
Simply use this code:
Have a look to it and let me know if this works.
Regards
Rahul
Issue is coming is coming because of below line. In this line you are pass the Registartion__c object id in Rum_Expenses__c object which is invalid.
PLease try below code.
Let us know if this will help you
Thanks
Amit Chaudhary
I got a doubt here,
Can you give me a scenario where we will get exp.size() = 0 in this code.
It will never be empty. Hence its not required here to check the size() condition.
Please do let me know if my thinking is wrong.
Regards,
Mahesh
I am agree with you in this case list size will never be zero. But is best pratice that we should always check the list size before doing any DML.
And DML should be in try catch.
Well we are here to spread the Best Pratice so that will help him in other issue as well . Not to resolve only single issue
Please let me know if there is any harm in checking list size before performing DML ?
Please do let me know if my thinking is wrong.
I tried your codes but i am getting same error
Please paste your trigger and also enable the debug log and print the debug log also here.
Regards,
Mahesh
To answer your questions:
(1) Yes it will add extra the processing time, if you are loading 200 records and if we add an extra .size() method will cost between 1 - 2 ms (approx).
Hence we shouldn't add any unnecessary code if we are sure that the situation will never occur.
(2) Adding exception to the DML is good only if we handle the exceptions properly.
Just adding the try {} catch(){} will not help for anything, on top of it, we are killing the real exception. With this we will never get a chance to know the actual error until the user realize that the record not inserted / updated.
Hope I answered your questions.
Please do let me know if any of my explanation is not clear or wrong.
Thank you.
Regards,
Mahesh
I am pasting my trigger and debug log
Trigger:
trigger RumnameTrigger on Registartion__c (after insert) {
list<Rum_Expenses__c>exp=new list<Rum_Expenses__c>();
for(Registartion__c reg:Trigger.new){
Rum_Expenses__c ru=new Rum_Expenses__c();
ru.Rum_Name__c=reg.Rum_Name__c;
exp.add(ru);
}
insert exp;
}
Debuglog:
36.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
01:06:45.3 (3802885)|EXECUTION_STARTED
01:06:45.3 (3852865)|CODE_UNIT_STARTED|[EXTERNAL]|01q28000000JgJR|RumnameTrigger on Registartion trigger event AfterInsert for [a0Y280000010p0S]
01:06:45.3 (3933454)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
01:06:45.3 (4614583)|HEAP_ALLOCATE|[72]|Bytes:3
01:06:45.3 (4680794)|HEAP_ALLOCATE|[77]|Bytes:152
01:06:45.3 (4708032)|HEAP_ALLOCATE|[342]|Bytes:408
01:06:45.3 (4735535)|HEAP_ALLOCATE|[355]|Bytes:408
01:06:45.3 (4761318)|HEAP_ALLOCATE|[467]|Bytes:48
01:06:45.3 (4813404)|HEAP_ALLOCATE|[139]|Bytes:6
01:06:45.3 (5045471)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
01:06:45.3 (5078331)|VARIABLE_SCOPE_BEGIN|[1]|this|RumnameTrigger|true|false
01:06:45.3 (5208961)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x3567c489
01:06:45.3 (5286327)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
01:06:45.3 (5308031)|VARIABLE_SCOPE_BEGIN|[1]|this|RumnameTrigger|true|false
01:06:45.3 (5348133)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x3567c489
01:06:45.3 (5359049)|STATEMENT_EXECUTE|[1]
01:06:45.3 (5362784)|STATEMENT_EXECUTE|[3]
01:06:45.3 (5374603)|HEAP_ALLOCATE|[3]|Bytes:4
01:06:45.3 (5569412)|HEAP_ALLOCATE|[50]|Bytes:5
01:06:45.3 (5608143)|HEAP_ALLOCATE|[56]|Bytes:5
01:06:45.3 (5631843)|HEAP_ALLOCATE|[64]|Bytes:7
01:06:45.3 (5696255)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
01:06:45.3 (5735260)|VARIABLE_ASSIGNMENT|[3]|this.exp|{"s":1,"v":[]}|0x3567c489
01:06:45.3 (6220734)|HEAP_ALLOCATE|[5]|Bytes:5
01:06:45.3 (6278909)|VARIABLE_SCOPE_BEGIN|[5]|reg|Registartion__c|true|false
01:06:45.3 (6633153)|VARIABLE_ASSIGNMENT|[5]|reg|{"LastModifiedDate":"2016-03-18T19:36:45.000Z","IsDeleted":false,"Rum_Name__c":"sam","E_Mail__c":"sa@gmail.com","OwnerId":"00528000001CiYJAA0","CreatedById":"00528000001CiYJAA0","CreatedDate":"2016-03-18T19:36:45.000Z","Id":"a0Y280000010p0SEAQ","LastModifiedById":"00528000001CiYJAA0","Password__c":"56","Name":"R-0013","SystemModstamp":"2016-03-18T19:36:45.000Z"}|0x5c299f6d
01:06:45.3 (6643923)|STATEMENT_EXECUTE|[6]
01:06:45.3 (6647126)|STATEMENT_EXECUTE|[7]
01:06:45.3 (6674555)|HEAP_ALLOCATE|[7]|Bytes:4
01:06:45.3 (6861359)|VARIABLE_SCOPE_BEGIN|[7]|ru|Rum_Expenses__c|true|false
01:06:45.3 (6898700)|VARIABLE_ASSIGNMENT|[7]|ru|{}|0x776efd52
01:06:45.3 (6908056)|STATEMENT_EXECUTE|[9]
01:06:45.3 (7494393)|HEAP_ALLOCATE|[9]|Bytes:19
01:06:45.3 (7632350)|FATAL_ERROR|System.StringException: Invalid id: sam
Trigger.RumnameTrigger: line 9, column 1
01:06:45.3 (7758148)|FATAL_ERROR|System.StringException: Invalid id: sam
Trigger.RumnameTrigger: line 9, column 1
01:06:45.7 (7807441)|CUMULATIVE_LIMIT_USAGE
01:06:45.7 (7807441)|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 0 out of 150
Number of DML rows: 0 out of 10000
Maximum CPU time: 0 out of 10000
Maximum heap size: 0 out of 6000000
Number of callouts: 0 out of 100
Number of Email Invocations: 0 out of 10
Number of future calls: 0 out of 50
Number of queueable jobs added to the queue: 0 out of 50
Number of Mobile Apex push calls: 0 out of 10
01:06:45.7 (7807441)|CUMULATIVE_LIMIT_USAGE_END
01:06:45.3 (9696469)|CODE_UNIT_FINISHED|RumnameTrigger on Registartion trigger event AfterInsert for [a0Y280000010p0S]
01:06:45.3 (10723701)|EXECUTION_FINISHED
What is the data type of
Rum_Expenses__c..Rum_Name__c ??
Registration__c.Rum_Name__c ??
One is String and one is lookup,
Regards,
Mahesh
Data type of Registration__c.Rum_Name__c ---Text
What is the requirement here as the other field is String we can't insert into lookup field.
what is the name field on Rum Expenses
if it is normal text then
ru.Name = reg.Rum_Name__c;
Test it and let me know.
It's executed well I am trying to make insert on the lookup filed Thank's for ur reply
Regards,
Mahesh