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
Gurjit Singh 22Gurjit Singh 22 

Trigger is running even if I create a new lead

As per the below code, this meant to update the first and last name only if I update a new lead but it is updating the first and last name of new lead as well Can someone please suggest. Thank you!

trigger HelloWorld on Lead (before update) {
for(Lead l : Trigger.new){
l.FirstName = 'Hello';
l.LastName = 'World';

}
 
Aman Pathak 14Aman Pathak 14
trigger leadtest on Lead (before update) {
    list<lead> le = new list<lead>();
    if(trigger.isbefore && trigger.isupdate)
    { for(lead  l : trigger.new)
  {
      l.FirstName ='Hello';
      l.LastName ='world';
      le.add(l);
  }
  
}
}

try this it should work , I think it is just because of trigger context variable. Apart from that everything looks fine.  
link:: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
Abdul KhatriAbdul Khatri
Can you please list down all the lead triggers by navigating as mentioned in the below screen shot. Your code looks good from the perpective what you are looking for. I meant only trigger when there is an update on an existing lead and will not trigger if you just entering a new lead. No need to put extra checks for that. You only define extract checks when there are multiple trigger context occurring. From my understanding there is another trigger that is executing on the Insert context and updating the lead causing this trigger to fire.

Please also check if there is no workflow, process builder etc. running at the time of lead creation and calling updates on the lead. Thanks.

User-added image


 
Gurjit Singh 22Gurjit Singh 22

HI all, 

Thank you so much for responding, I have created a new org and after that, the first thing I did was created this trigger so there are no workflows and nothing from process builder or visual workflow and moreover this works fine in salesforce classic but in lightening it changes the name even for the new record 

Gurjit Singh 22Gurjit Singh 22
@Aman thank you for responding but your trigger is still not working in lightening 
Abdul KhatriAbdul Khatri
hmm very strange. How about checking the Lead Assignment Rule.

Can you confirm while creating a new lead the following optional is unchecked as shown in the screen shot below. It is 

User-added image

it is blurry, but it is this
User-added image

Can you also share your Lead Settings?
Gurjit Singh 22Gurjit Singh 22

Hi,
Thank you so for responding.

The problem is with the lightning only and I am not able to find any of such option in lightening, could you please suggest any other way 

Lead

Abdul KhatriAbdul Khatri
Is it possible to send Debug Logs while you create new lead?
Gurjit Singh 22Gurjit Singh 22

Yes sure Adbdul, 

Please find below the logs and let me know if this can do.

42.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
19:42:20.0 (415627)|USER_INFO|[EXTERNAL]|0057F000003arvb|gs650d@gmail.com|Pacific Standard Time|GMT-07:00
19:42:20.0 (456132)|EXECUTION_STARTED
19:42:20.0 (458684)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Lead
19:42:20.0 (8784633)|WF_RULE_EVAL_BEGIN|Assignment
19:42:20.0 (8813154)|WF_SPOOL_ACTION_BEGIN|Assignment
19:42:20.0 (8828481)|WF_ACTION|.
19:42:20.0 (8835476)|WF_RULE_EVAL_END
19:42:20.0 (8941324)|WF_ACTIONS_END| None
19:42:20.0 (8952835)|CODE_UNIT_FINISHED|Workflow:Lead
19:42:20.0 (8960517)|EXECUTION_FINISHED
19:42:20.72 (72631441)|USER_INFO|[EXTERNAL]|0057F000003arvb|gs650d@gmail.com|Pacific Standard Time|GMT-07:00
19:42:20.72 (72653754)|EXECUTION_STARTED
19:42:20.72 (72658467)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
19:42:20.72 (73405756)|CODE_UNIT_STARTED|[EXTERNAL]|01q7F000001J6HQ|leadtest on Lead trigger event BeforeUpdate
19:42:20.72 (73467027)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
19:42:20.72 (73507287)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
19:42:20.72 (73930634)|HEAP_ALLOCATE|[72]|Bytes:3
19:42:20.72 (74002406)|HEAP_ALLOCATE|[77]|Bytes:152
19:42:20.72 (74018757)|HEAP_ALLOCATE|[342]|Bytes:408
19:42:20.72 (74030991)|HEAP_ALLOCATE|[355]|Bytes:408
19:42:20.72 (74042603)|HEAP_ALLOCATE|[467]|Bytes:48
19:42:20.72 (74073502)|HEAP_ALLOCATE|[139]|Bytes:6
19:42:20.72 (74190116)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
19:42:20.72 (74223129)|VARIABLE_SCOPE_BEGIN|[1]|this|leadtest|true|false
19:42:20.72 (74488596)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x1da6cb42
19:42:20.72 (74559299)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
19:42:20.72 (74579163)|VARIABLE_SCOPE_BEGIN|[1]|this|leadtest|true|false
19:42:20.72 (74597263)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x1da6cb42
19:42:20.72 (74606169)|STATEMENT_EXECUTE|[1]
19:42:20.72 (74609012)|STATEMENT_EXECUTE|[2]
19:42:20.72 (74653927)|HEAP_ALLOCATE|[2]|Bytes:4
19:42:20.72 (74761641)|HEAP_ALLOCATE|[50]|Bytes:5
19:42:20.72 (74788071)|HEAP_ALLOCATE|[56]|Bytes:5
19:42:20.72 (74798050)|HEAP_ALLOCATE|[64]|Bytes:7
19:42:20.72 (74832054)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
19:42:20.72 (74847402)|VARIABLE_ASSIGNMENT|[2]|this.le|[]|0x1da6cb42
19:42:20.72 (74868919)|STATEMENT_EXECUTE|[4]
19:42:20.72 (75057520)|HEAP_ALLOCATE|[4]|Bytes:5
19:42:20.72 (75119704)|HEAP_ALLOCATE|[4]|Bytes:4
19:42:20.72 (75133766)|VARIABLE_SCOPE_BEGIN|[4]|l|Lead|true|false
19:42:20.72 (75682323)|VARIABLE_ASSIGNMENT|[4]|l|{"Status":"Open - Not Contacted","LastModifiedDate":"2018-05-23T02:42:20.000Z","Company":"FIS","IsDeleted":false,"DoNotCall":false,"IsConverted":false,"FirstName":"Abdul","IsUnreadByOwner":false,"HasOptedOutOfEmail":false,"HasOptedOutOfFax":false,"SystemModstamp":"2018-05-23T02:42:20.000Z","CleanStatus":"Pending","CreatedById":"0057F000003arvbQAA","CreatedByID":"0057F000003arvbQAA","OwnerId":"0057F000003arvbQAA","CreatedDate":"2018-05-23T02:42:20.000Z","Id":"00Q7F00000A8qwTUAR","LastName":"Khatri","LastModifiedByID":"0057F000003arvbQAA","LastModifiedById":"0057F000003arvbQAA","LastTransferDate":"2018-05-23T02:42:20.000Z"}|0x508dfec7
19:42:20.72 (75703046)|STATEMENT_EXECUTE|[5]
19:42:20.72 (75705670)|STATEMENT_EXECUTE|[6]
19:42:20.72 (75710045)|HEAP_ALLOCATE|[6]|Bytes:5
19:42:20.72 (75948542)|VARIABLE_ASSIGNMENT|[6]|this.FirstName|"Hello"|0x508dfec7
19:42:20.72 (75957571)|STATEMENT_EXECUTE|[8]
19:42:20.72 (75961552)|HEAP_ALLOCATE|[8]|Bytes:5
19:42:20.72 (76194306)|VARIABLE_ASSIGNMENT|[8]|this.LastName|"world"|0x508dfec7
19:42:20.72 (76203920)|STATEMENT_EXECUTE|[10]
19:42:20.72 (76255318)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
19:42:20.72 (76277369)|HEAP_ALLOCATE|[4]|Bytes:5
19:42:20.72 (76293361)|VARIABLE_ASSIGNMENT|[4]|l|null|
19:42:20.76 (76303674)|CUMULATIVE_LIMIT_USAGE
19:42:20.76 (76303674)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

19:42:20.76 (76303674)|CUMULATIVE_LIMIT_USAGE_END

19:42:20.72 (77879907)|CODE_UNIT_FINISHED|leadtest on Lead trigger event BeforeUpdate
19:42:20.72 (88407067)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Lead
19:42:20.72 (91661572)|WF_RULE_EVAL_BEGIN|Assignment
19:42:20.72 (91675421)|WF_SPOOL_ACTION_BEGIN|Assignment
19:42:20.72 (91686551)|WF_ACTION|.
19:42:20.72 (91692903)|WF_RULE_EVAL_END
19:42:20.72 (91776152)|WF_ACTIONS_END| None
19:42:20.72 (91787234)|CODE_UNIT_FINISHED|Workflow:Lead
19:42:20.72 (149258381)|CODE_UNIT_FINISHED|TRIGGERS
19:42:20.72 (149278147)|EXECUTION_FINISHED

Abdul KhatriAbdul Khatri
Are you sure about no workflow as I do see instances of some workflow kicking in?
Gurjit Singh 22Gurjit Singh 22

Please find below the workflow screenshot.

User-added image
 

Abdul KhatriAbdul Khatri
I mimic the same on my side and experiencing the same issue. Seems like a bug on the Salesforce side. I would recommend the following
  1. Open a ticket with Salesforce Support for the same
  2. Report a bug to the Salesforce