You need to sign in to do that
Don't have an account?
Meghana Sharma
Need Help with Apex Class
Hello Developers
need small Assistance with the apex class
My requirement is that when i create a lead with country as 'india', there is a custom field named 'India_Auto_Number__c' where it should update as 1, and again when i create a lead with country name 'india' it should increment to 2...like this it should keep on incrementing till 5th lead, when i try to insert 6th lead, it should again update as 1
how do i achive this functionality
This is what i have come up with so far
need small Assistance with the apex class
My requirement is that when i create a lead with country as 'india', there is a custom field named 'India_Auto_Number__c' where it should update as 1, and again when i create a lead with country name 'india' it should increment to 2...like this it should keep on incrementing till 5th lead, when i try to insert 6th lead, it should again update as 1
how do i achive this functionality
public class IncrementonLead { public void onInsert( list<Lead> newList){ List<Lead> lstld = [SELECT Id,India_Auto_Number__c FROM Lead]; Integer intCounter = lstld.size() != 0 ? lstld[0].India_Auto_Number__c : 0; for(Lead objLead: newList) { intCounter ++; objLead.India_Auto_Number__c = intCounter; } } }
This is what i have come up with so far
Trigger:
Test Class:
All Answers
trigger LeadIncrementV2 on Lead (before insert,Before Update) {
Lead lds = new Lead();
Lead ld = [SELECT India_Auto_Number__c from Lead LIMIT 1];
integer LatestNumber = Integer.valueOf(lds.India_Auto_Number__c);
for(Lead llds : Trigger.New){
if(Trigger.OldMap.get(llds.id).Country == 'India'){ // This is where i am getting the error (System.NullPointerException: Attempt to de-reference a null object) //
llds.India_Auto_Number__c = LatestNumber + 1;
//Numero_Account__c is a text number of Account
}
}
lds.India_Auto_Number__c= LatestNumber + 1;
update lds;
}
Try this,
I hope this helps you.
Thank you very much for the code its working fine, When i create a 6th Lead with Country as 'India' the custom number field 'India_Auto_Number__c' should be updated again as 1,
how to achive this functionality??
can you create any other trigger or workflow rule which updates this custom field?
in the Workflow rule, i am taking field update, where i need to define a formula, cant we modify in the existing trigger itself, because it may become an issue while creating a test class
can you suggest any formula where if India_Auto_Number__c = 6 it should automatically update to 1, if its 7 it should update as 2....etc etc
When India_Auto_Number__c = 6 then which field you want to update as 1?
Never Mind, i figured it out, it dint want the mod from the begining, can you please help me with the test class
This is my perfectly working Trigger code
trigger LeadIncrementV3 on Lead (before insert,Before Update) {
if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
List<Lead> leadList = [Select Id,Lead_No_Region_IN__c From Lead Where Region__c = 'India'];
For(Lead l : trigger.New) {
if(l.Region__c == 'India') {
if(leadList.size() > 0){
l.Lead_No_Region_IN__c = leadList.size()+0;
} else {
l.Lead_No_Region_IN__c = 1;
}
}
}
List<Lead> leadListt = [Select Id,Lead_No_Region_USA__c From Lead Where Region__c = 'US'];
For(Lead m : trigger.New) {
if(m.Region__c == 'US') {
if(leadListt.size() > 0){
m.Lead_No_Region_USA__c = leadListt.size()+0;
} else {
m.Lead_No_Region_USA__c = 1;
}
}
}
List<Lead> leadListm = [Select Id,Lead_No_Region_EU__c From Lead Where Region__c = 'EU'];
For(Lead n : trigger.New) {
if(n.Region__c == 'EU') {
if(leadListm.size() > 0){
n.Lead_No_Region_EU__c = leadListm.size()+0;
} else {
n.Lead_No_Region_EU__c = 1;
}
}
}
List<Lead> leadListo = [Select Id,Lead_No_Region_EU__c From Lead Where Region__c = 'SEA'];
For(Lead o : trigger.New) {
if(o.Region__c == 'SEA') {
if(leadListo.size() > 0){
o.Lead_No_Region_SEA__c = leadListo.size()+0;
} else {
o.Lead_No_Region_SEA__c = 1;
}
}
}
List<Lead> leadListp = [Select Id,Lead_No_Region_ME__c From Lead Where Region__c = 'ME'];
For(Lead p : trigger.New) {
if(p.Region__c == 'ME') {
if(leadListp.size() > 0){
p.Lead_No_Region_ME__c = leadListp.size()+0;
} else {
p.Lead_No_Region_ME__c = 1;
}
}
}
}
}
Can you please help me with the Test class??
Trigger:
Test Class:
I hope this helps you.
i am getting an error , (Field is not writeable: Lead.Region__c), Region__c is an formula field, how do i cover formula field in test class? and i am getting 51% code coverage, how do i increase the test coverage??
i solved the Field is not Writeable issue, when i try to run the test, i am getting the error in the line 3 of the trigger,
can you please share your formula for Region__c?
CASE(Country, "United States", "US",
"Canada", "US",
"USA", "US",
"US", "US",
"Brazil", "EU",
"Guyana", "SA",
"Paraguay", "SA",
"Peru", "SA",
"Uruguay", "SA",
"Venezuela", "SA",
"Germany", "EU",
"Deutschland", "EU",
"United Kingdom", "EU",
"France", "EU",
"Italy", "EU",
"Spain", "EU",
"Ukraine", "EU",
"Poland", "EU",
"Romania", "EU",
"Netherlands", "EU",
"Belgium", "EU",
"Greece", "EU",
"Czech Republic", "EU",
"Portugal", "EU",
"Sweden", "EU",
"Hungary", "EU",
"Belarus", "EU",
"Serbia", "EU",
"Austria", "EU",
"Switzerland", "EU",
"Bulgaria", "EU",
"Denmark", "EU",
"Finland", "EU",
"Slovakia", "EU",
"Norway", "EU",
"Ireland", "EU",
"Russia", "EU",
"Albania", "EU",
"Georgia", "EU",
"Latvia", "EU",
"Lithuania", "EU",
"Malta", "EU",
"Croatia", "EU",
"Polska", "EU",
"San Marino", "EU",
"Bosnia and Herzegovina", "EU",
"Sweden", "EU",
"Bahrain", "ME",
"Cyprus", "ME",
"Egypt", "ME",
"Iran", "ME",
"Iraq", "ME",
"Israel", "ME",
"Jordan", "ME",
"Kuwait", "ME",
"Lebanon", "ME",
"Oman", "ME",
"Qatar", "ME",
"Saudi Arabia", "ME",
"Turkey", "ME",
"United Arab Emirates", "ME",
"UAE", "ME",
"Yemen", "ME",
"India", "India",
"Indonesia", "SEA",
"Malaysia", "SEA",
"Philippines", "SEA",
"Singapore", "SEA",
"Thailand", "SEA",
"Viet Nam", "SEA",
"Taiwan", "SEA",
"Hong Kong", "SEA",
"Cambodia", "SEA",
"China", "SEA",
"Japan", "SEA",
"Australia", "SEA",
"ROW")
Try this,
still i am getting the same error as Too manu SOQL queries, in the line 3 of the trigger
How many records of lead at the time you update or insert?
When test class run then you get this error?
the error is coming from the below line
List<Lead> leadList = [Select Id,Lead_No_Region_IN__c From Lead Where Region__c = 'India'];
when i try to queery - Select Id,Lead_No_Region_IN__c From Lead Where Region__c = 'India'
it in the Querry editor it shows as 72 Records
still i am getting this error in the test results
Try this for trigger,
and for test class,
It finally worked, thank you very much for taking your time for Corrections and suggestions
Trigger:
Test Class:
If your issue is solved then this question make solved. so it helps others.
Thanks.
i have marked the best answer, and once again thank you very much for the help