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
saariko.ax241saariko.ax241 

Simple trigger - change the IsUnreadByOwner

I am trying to update the field: IsUnreadByOwner - when a lead status changes to: Unqualified.

 

this is my trigger:

trigger UnqualifiedLead on Lead (before update) {
	for(Lead lead: Trigger.new)
	{
		if (lead.Status == 'Unqualified')
		{
			lead.IsUnreadByOwner = false;	
			update lead;
		}
	}
}

 

And my trigger:

@isTest
private class UnqualifiedLeadTest {

    static testMethod void myUnitTest() {
        // Setup the lead record
        Lead lead = new Lead();
        lead.LastName = 'last';
        lead.FirstName = 'First';
        lead.Company = 'Company';
        lead.Status = 'Unqualified';
        insert lead;
        lead.IsUnreadByOwner = True;
        update lead;
    }
}

 

i have a feeling, that the "update lead;" line is wrong, but not sure how to check if the change works.

 

Appreciate the help.

 

This is the error log I get

 

*** Deployment Log ***
Result: FAILED
Date: July 17, 2012 5:49:07 PM IDT

# Deployed From:
   Project name: Email2Lead
   Username: saar....@.com
   Endpoint: www.salesforce.com

# Deployed To:
   Username: saar.....@.com
   Endpoint: www.salesforce.com

# Deploy Results:
   File Name:    package.xml
   Full Name:  package.xml
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

   File Name:    triggers/UnqualifiedLead.trigger
   Full Name:  UnqualifiedLead
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

# Test Results:

Run Failures:
  UnqualifiedLeadTest.myUnitTest System.DmlException: Update failed. First exception on row 0 with id 00QD000000VgQJHMA3; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UnqualifiedLead: execution of BeforeUpdate

caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old

Trigger.UnqualifiedLead: line 7, column 1: []

 

JeffStevensJeffStevens

Ya - I'm pretty sure that - in your test class - after your insert lead; - you will need to query to get the lead record.  Something like this...

 

insert lead;

Lead newLead = [SELECT Id, IsUnReadByOwner FROM LEAD where Id = :Lead.Id LIMIT 1]();

newLead.isUnReadByOwner = true;

Update newLead;

 

I might be off on my syntax - but I think that's close. 

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

You cannot have DML Statement in your trigger. Remove the update Statement it should work fine

Jerun JoseJerun Jose

Tuning Sam's reply a bit.

 

You cannot use trigger variables for DML operations. In your code

 

trigger UnqualifiedLead on Lead (before update) {
	for(Lead lead: Trigger.new)
	{
		if (lead.Status == 'Unqualified')
		{
			lead.IsUnreadByOwner = false;	
			update lead;
		}
	}
}

 the variable lead is taken from trigger.new. You cannot use it to perform DML.

 

As Sam mentioned, you can remove the update statement, and it should be fine, as you are modifying the content in triggee.new

JeffStevensJeffStevens

Ah - yes - great catch guys.  Thanks for the clarification/corrections. 

 

 saariko - if this answered your question - you should mark it as solved. 

saariko.ax241saariko.ax241

Thanks all,

 

So I did remove the update from the class (I understand now alittle better the process)

- lead is created

- trigger is "updating" the lead just before the update

- no need to "update the lead" - since this is done once the trigger ends.

 

- I added the query string (as suggested by @JeffStevens) and the validtion is passing.