You need to sign in to do that
Don't have an account?
Ravi K 47
trigger to update new and old records on product
Hello All,
I have a requirement to udpate Product2(IsActive) field.
The requirement is udpate IsActive=true for all newly updating records and remaing all old records(oldrecords- newly updating records) should update with IsActive=false value.
Below are the old records
Below are the newly updating records
and final output looks like
Could you please help me to provide the trigger for this requirement .
I have a requirement to udpate Product2(IsActive) field.
The requirement is udpate IsActive=true for all newly updating records and remaing all old records(oldrecords- newly updating records) should update with IsActive=false value.
Below are the old records
id name isactive 1 ravi true 2 kumar true 3 kusuma true
Below are the newly updating records
id name isactive 1 ravi true
and final output looks like
id name isactive 1 ravi true 2 kumar false 3 kusuma false
Could you please help me to provide the trigger for this requirement .
try this code
I have created and tested in my org its working well!
try this code
If information in informative please make my answer as best answer.
Thanks,
varun
<pre>
trigger isActiveFalse on Product2 (after insert, after update)
{
List<Product2 > lstpro =
[ SELECT Id, IsActive
FROM Product2
WHERE IsActive = true AND Id NOT IN :Trigger.new
];
for ( Product2 p : lstpro )
{
p.IsActive = false;
}
// DML on an empty collection does not count against DML limits
update lstpro;
}
</pre>
But if im trying to update the Isactive=false record and these record should be active as well.
I have already tried with the above one and its working fine.
Could you please help me on this ?
Ravi, I want to help, but I don't understand your new question. If "it's working fine", then what do you need help with? Could you provide more information, please?
While updating the Inactive records ,we need to update to Active and all other old records should be InActive mode.
For Example:
We have an Inactive record in the above given scenorio (2 kumar False)
Now i am updating 2nd record then this should be updated to Active as below.
<pre>
trigger ProductActivation on Product2 (before insert, after insert, before update, after update)
{
if ( Trigger.isBefore )
{
for ( Product2 product : Trigger.new )
{
product.IsActive = true;
}
}
if ( Trigger.isAfter )
{
List<Product2 > productsToDeactivate =
[ SELECT Id, IsActive
FROM Product2
WHERE IsActive = true AND Id NOT IN :Trigger.new
];
for ( Product2 product : productsToDeactivate )
{
product.IsActive = false;
}
// DML on an empty collection does not count against DML limits
update productsToDeactivate;
}
}
</pre>
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger isActiveFalse caused an unexpected exception, contact your administrator: isActiveFalse: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 01t7F0000016JQAQA2; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, isActiveFalse: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 01t7F0000016JQ6QAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, isActiveFalse: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 01t7F0000016JQAQA2; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 01t7F0000016JQA) is currently in trigger isActiveFalse, therefore it cannot recursively update itself: [] Trigger.isActiveFalse: line 21, column 1: [] Trigger.isActiveFalse: line 21, column 1: []: Trigger.isActiveFalse: line 21, column
Hello ,
Please find the above code snippet.
Many thanks for your help in advance!
Below is the udpated code ,please ignore the above
Below is the latest error message
Many developers face this issue because of a recursive trigger. For example, in an 'after update' trigger, a developer could be performing an update operation and this would lead to recursive call, and the error:
"maximum trigger depth exceeded"
Solution:In order to avoid the situation of recursive call, make sure your trigger is getting executed only one time. To do so, you can create a class with a static boolean variable with default value true.
In the trigger, before executing your code keep a check that the variable is true or not.
Once you check, make the variable false.
apex class
I hope this is helpful for you make my answer as best answer