function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC n12SFDC 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
Best Answer chosen by SFDC n12
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hi Bharat,

Create new class as following
public Class triggerRecurssionHandler{
    private static boolean isFirstRun= true;    
}
and change your code to
Trigger Leadprocess on Lead_Object__c(after insert , after update)
{
     List<Lead> sub=new List<Lead>();
     for(Lead_Object__c v : Trigger.new)
     {
        if(triggerRecurssionHandler.isFirstRun){
			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;
}

If this answers your question, mark this as best answer.

Thanks,
N.J

All Answers

Vi$hVi$h
Hi,

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.
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hi Bharat,

Create new class as following
public Class triggerRecurssionHandler{
    private static boolean isFirstRun= true;    
}
and change your code to
Trigger Leadprocess on Lead_Object__c(after insert , after update)
{
     List<Lead> sub=new List<Lead>();
     for(Lead_Object__c v : Trigger.new)
     {
        if(triggerRecurssionHandler.isFirstRun){
			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;
}

If this answers your question, mark this as best answer.

Thanks,
N.J

This was selected as the best answer
SFDC n12SFDC n12
tha above code is creating one record if the status is unqualified , but the new status is not getting updated in lead object record it still shows qualified only
Vi$hVi$h
One way to solve it would be to replace triggerRecurssionHandler.isFirstRun with whatever criteria you have for your workflow and put a NOT condition to it.
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
SFDC n12SFDC n12
how do i insert my workflw criteria to it, below is my criteria,

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
Vi$hVi$h
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")
)
)

Try this.
SFDC n12SFDC n12
Hi,

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;
}
Vi$hVi$h
Try with '==' instead of '=' in the if condition
SFDC n12SFDC n12
still 2 records are created , should i remove the insert and keep only after update?
Vi$hVi$h
Keep the code as it is 
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