You need to sign in to do that
Don't have an account?
ch ranjith
dubplicatewithoutgov: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.dubplicatewithoutgov: line 4, column 1
trigger dubplicatewithoutgov on student__c (before insert)
{
list<string> stlist=new list<string>();
for(student__C st:trigger.old)
{
stlist.add(st.name);
}
for(student__C st:trigger.new)
{
for(integer i=0;i<stlist.size();i++)
{
if(st.name==stlist[i])
{
st.adderror('cant not add dupicate record');
}
}
}
}
{
list<string> stlist=new list<string>();
for(student__C st:trigger.old)
{
stlist.add(st.name);
}
for(student__C st:trigger.new)
{
for(integer i=0;i<stlist.size();i++)
{
if(st.name==stlist[i])
{
st.adderror('cant not add dupicate record');
}
}
}
}
All Answers
for ref: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htmhttps://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htm
{
list<student__C> stlist=[Select studentname from student__C];
for(student__C st:trigger.new)
{
for(integer i=0;i<stlist.size();i++)
{
if(st.name==stlist[i])
{
st.adderror('cant not add dupicate record');
}
}
}
}
This will work.mark it as solved if it is answered
You have written trigger on before insert so you will never get the value of Trigger.old because there are not old record, records are going to insert first time.
You can read about trigger from here https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm
trigger dubplicatewithoutgov on student__c (before insert)
{
list<student__C> stlist=[Select name from student__C];
for(student__C st:trigger.new)
{
for(integer i=0;i<stlist.size();i++)
{
String objectAsString= String.valueOf(stlist[i]);
if(st.name==objectAsString)
{
st.adderror('cant not add dupicate record');
}
}
}
}