You need to sign in to do that
Don't have an account?
SFDC n12
Trigger help needed
Hi,
I am having a trigger on a custom object which creates a new lead record maps the values of certian fields from custom to standard object,
In my custom object i have a field called as "Status" which is set to "qualified" and changes to "Unqualified" based on workflow written backend
Now my trigger is (after insert and after update) , when the lead status is qualified it creates only 1 lead record since there is no workflow or update
but if the lead status is unqualified it creates 2 lead records , since there is an update at the backend,
How do i handle it in my trigger
This is my trigger
Trigger Leadprocess on Lead_Object__c(after insert , after update)
{
List<Lead> sub=new List<Lead>();
for(Lead_Object__c v : Trigger.new)
{
Lead s = new Lead();
// s.RecordType.name =v.Record_Type__c;
s.FirstName=v.First_Name__c;
s.LastName =v.Last_Name__c;
s.Account_Type__c = v.Account_Type__c;
s.Units_In_Inventory__c = v.Units_In_Inventory__c;
s.Company = v.company__c;
s.PostalCode = v.Billing_Zip__c;
s.AnnualRevenue =v.Annual__c;
s.Salutation = v.Salutation__c;
s.LeadSource =v.Record_Type__c;
s.Status =v.Leadstatus__c;
s.Additional_Information__c =('The entered value of ZID is'+(v.ZID__c));
sub.add(s);
insert sub;
}
}
Thanks in Advance
I am having a trigger on a custom object which creates a new lead record maps the values of certian fields from custom to standard object,
In my custom object i have a field called as "Status" which is set to "qualified" and changes to "Unqualified" based on workflow written backend
Now my trigger is (after insert and after update) , when the lead status is qualified it creates only 1 lead record since there is no workflow or update
but if the lead status is unqualified it creates 2 lead records , since there is an update at the backend,
How do i handle it in my trigger
This is my trigger
Trigger Leadprocess on Lead_Object__c(after insert , after update)
{
List<Lead> sub=new List<Lead>();
for(Lead_Object__c v : Trigger.new)
{
Lead s = new Lead();
// s.RecordType.name =v.Record_Type__c;
s.FirstName=v.First_Name__c;
s.LastName =v.Last_Name__c;
s.Account_Type__c = v.Account_Type__c;
s.Units_In_Inventory__c = v.Units_In_Inventory__c;
s.Company = v.company__c;
s.PostalCode = v.Billing_Zip__c;
s.AnnualRevenue =v.Annual__c;
s.Salutation = v.Salutation__c;
s.LeadSource =v.Record_Type__c;
s.Status =v.Leadstatus__c;
s.Additional_Information__c =('The entered value of ZID is'+(v.ZID__c));
sub.add(s);
insert sub;
}
}
Thanks in Advance
Create new class as following and change your code to
If this answers your question, mark this as best answer.
Thanks,
N.J
All Answers
1. Never write a DML or a query statement inside a for loop.
2. To tackle your issue , create a custom field checkbox on the custom obj, set it to true when the workflow is executed.
In the trigger check for this custom field if it is true then dont execute the code.
Create new class as following and change your code to
If this answers your question, mark this as best answer.
Thanks,
N.J
Also before the insert statement put an if (sub.size()>0)
Hope this helps.
Also have a look at
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm
Thanks
OR
(
(How_many_years_have_you_been_in_business__c < 3),
(Is_your_dealership_dedicated_to_RETAIL_a__c = "No"),
(Is_the_dealership_lot_paved__c = "No"),
(Does_the_dealership_operate_from_a_perma__c = "No"),
(What_are_your_average_monthly_used_vehic__c < 50),
(How_many_front_line_ready_units_do_you_c__c <50),
(What_percent_of_your_inventory_has_less__c <90),
(What_percent_of_your_inventory_is_newer__c <90),
(What_percent_of_inventory_has_a_selling__c <90),
(Does_your_dealership_use_DealerTrack_and__c ="No"),
(Can_you_print_contracts_electronically__c ="No"),
(Does_your_dealership_have_a_dedicated_F__c ="No")
)
kindly help me on hw to do it
(v.Is_your_dealership_dedicated_to_RETAIL_a__c = "No")||
(v.Is_the_dealership_lot_paved__c = "No")||
(v.Does_the_dealership_operate_from_a_perma__c = "No")||
(v.What_are_your_average_monthly_used_vehic__c < 50)||
(v.How_many_front_line_ready_units_do_you_c__c <50)||
(v.What_percent_of_your_inventory_has_less__c <90)||
(v.What_percent_of_your_inventory_is_newer__c <90)||
(v.What_percent_of_inventory_has_a_selling__c <90)||
(v.Does_your_dealership_use_DealerTrack_and__c ="No")||
(v.Can_you_print_contracts_electronically__c ="No")||
(v.Does_your_dealership_have_a_dedicated_F__c ="No")
)
)
Try this.
I am getting the following error in my code
Error: Compile Error: OR operator can only be applied to Boolean expressions at line 7 column 2
trigger Leadprocess on Lead_Object__c(after insert , after update)
{
List<Lead> sub=new List<Lead>();
for(Lead_Object__c v : Trigger.new)
{
if((!(v.How_many_years_have_you_been_in_business__c < 3)||
(v.Is_your_dealership_dedicated_to_RETAIL_a__c = 'No')||
(v.Is_the_dealership_lot_paved__c = 'No')||
(v.Does_the_dealership_operate_from_a_perma__c = 'No')||
(v.What_are_your_average_monthly_used_vehic__c < 50)||
(v.How_many_front_line_ready_units_do_you_c__c <50)||
(v.What_percent_of_your_inventory_has_less__c <90)||
(v.What_percent_of_your_inventory_is_newer__c <90)||
(v.What_percent_of_inventory_has_a_selling__c <90)||
(v.Does_your_dealership_use_DealerTrack_and__c ='No')||
(v.Can_you_print_contracts_electronically__c ='No')||
(v.Does_your_dealership_have_a_dedicated_F__c ='No') ||
(v.Which_F_I_Products_does_your_dealership__c ='Gap')||
(v.Which_F_I_Products_does_your_dealership__c ='None')
)
)
{
//triggerRecurssionHandler.isFirstRun = false;
Lead s = new Lead();
// s.RecordType.name =v.Record_Type__c;
s.FirstName=v.First_Name__c;
s.LastName =v.Last_Name__c;
s.Account_Type__c = v.Account_Type__c;
s.Units_In_Inventory__c = v.Units_In_Inventory__c;
s.Company = v.company__c;
s.PostalCode = v.Billing_Zip__c;
s.AnnualRevenue =v.Annual__c;
s.Salutation = v.Salutation__c;
s.LeadSource =v.Record_Type__c;
s.Status =v.Leadstatus__c;
s.Additional_Information__c =('The entered value of ZID is'+(v.ZID__c));
sub.add(s);
}
}
insert sub;
}
Add a new checkbox workflowexecuted__c
Keep this checkbox value default to false and set it true in the workflow
The if in the trigger should be like this :
if(!(All the or conditions here)||v.workflowexecuted__c =true)
The '!' is not correctly positioned in the previous code