+ Start a Discussion
lucky25lucky25 

trigger

create  account no field in account object then write
 if u choose industry= apparel  account no=1,next u choose industry= apparel  accountno=2 like wise increase corect it please



trigger num on Account (before insert,before update ,after insert)
 {
 string Apparel,Education,Banking;
 
Account[] acct=Trigger.new;
for(Account a:acct)
{
if(a.Industry==Apparel)
{
a.account_no__c=1;
a.account_no__c++;

}
else if(a.Industry==Education)
{
a.account_no__c=1;
a.account_no__c++;
upsert a;
}
else if(a.Industry==Banking)
{
a.account_no__c=1;
a.account_no__c++;
upsert a;
}
}
}

JitendraJitendra

Hi,

You could use the Custom setting to save the current record count for industry and in Trigger, get the count from Custom setting to initialize the variable and at the end again save the count in custom setting.

jd123jd123

trigger num on Account (before insert)
 {
 string Apparel,Education,Banking;
 
Account[] acct=Trigger.new;
for(Account a:acct)
{
if(a.Industry=='Apparel')
{

Integer i=[SELECT COUNT() FROM Account WHERE a.Industry=='Apparel'];


a.account_no__c=i++;

}
else if(a.Industry=='Education')
{
Integer i=[SELECT COUNT() FROM Account WHERE a.Industry=='Education'];

a.account_no__c=i++;

}
else if(a.Industry=='Banking')
{

Integer i=[SELECT COUNT() FROM Account WHERE a.Industry=='Banking'];

a.account_no__c=i++;


}

}
}

 

if it resloved your question please mark it as resolved.

SamuelDeRyckeSamuelDeRycke

Mind explaining to us why you want to run your trigger on before insert, before update and  after insert ?  (I would like you to think about it)

 

 

 

JitendraJitendra

In the example provided, SOQL should not be written inside the For loop.

 

Instead of SOQL, Custom setting will work like charm !!!

jd123jd123

Hi Jitendra 

 

  can you expalin what is custom settings and how can we do or provide some links.