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
juppingerjuppinger 

Check opportunitys products (lineitems) after update an opportunity

Hi,

 

I want to create a trigger that fires after an opportunity will be updated.

First the trigger has to check, if specified fields in the opportunity has changed. If so, the trigger has to log this changes into a new simple custom object (text only).

I wrote my code and the first part works very well:

 

trigger HistoryOpportunityV001_test on Opportunity (after update) { // no bulk processing; will only run from the UI if (Trigger.new.size() == 1) { String strChanges = ''; //// Checking fields if the value has changed // 1. Field: Opportunity.Name if (Trigger.old[0].Name != Trigger.new[0].Name) { cnt++; strChanges += cnt + '. <Name>: "' + Trigger.old[0].Name + '" -> "' + Trigger.new[0].Name + '"\n'; } // 2. Field: Opportunity.Order_ID__c if (Trigger.old[0].Order_ID__c != Trigger.new[0].Order_ID__c) { cnt++; strChanges += cnt + '. <Order_ID__c>: "' + Trigger.old[0].Order_ID__c + '" -> "' + Trigger.new[0].Order_ID__c + '"\n'; } if (cnt > 0) { OpportunityLog__c oppLog = new OpportunityLog__c( OpportunityID__c = Trigger.old[0].Id, OpportunityName__c = 'oppName', OpportunityChangesUser__c = UserInfo.getUserId(), Changes__c = strChanges ); insert oppLog; } } }

 

 

 

Now I want to implement, that the trigger will check if the value of some specific fields of the updated opportunitys line items (products in the opportuity) has changed. If so, I want to log this lineitems field changes, too.

I know that I have to loop through all line items and have to check the field values.

- BUT HOW CAN I DO THIS?

 

Could anyone help me, please? It would be great if you post me your code or ideally complete the existing code ;)

 

Next... I want to log if the user adds a new product/lineitem or deletes a lineitem.

 

Any ideas?

 

Thanks a lot,

jup

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Venkat PolisettVenkat Polisett

juppinger wrote:

Hi,

 

I want to create a trigger that fires after an opportunity will be updated.

First the trigger has to check, if specified fields in the opportunity has changed. If so, the trigger has to log this changes into a new simple custom object (text only).

I wrote my code and the first part works very well:

 

trigger HistoryOpportunityV001_test on Opportunity (after update) { // no bulk processing; will only run from the UI if (Trigger.new.size() == 1) { String strChanges = ''; //// Checking fields if the value has changed // 1. Field: Opportunity.Name if (Trigger.old[0].Name != Trigger.new[0].Name) { cnt++; strChanges += cnt + '. <Name>: "' + Trigger.old[0].Name + '" -> "' + Trigger.new[0].Name + '"\n'; } // 2. Field: Opportunity.Order_ID__c if (Trigger.old[0].Order_ID__c != Trigger.new[0].Order_ID__c) { cnt++; strChanges += cnt + '. <Order_ID__c>: "' + Trigger.old[0].Order_ID__c + '" -> "' + Trigger.new[0].Order_ID__c + '"\n'; } if (cnt > 0) { OpportunityLog__c oppLog = new OpportunityLog__c( OpportunityID__c = Trigger.old[0].Id, OpportunityName__c = 'oppName', OpportunityChangesUser__c = UserInfo.getUserId(), Changes__c = strChanges ); insert oppLog; } } }

 

 

 

Now I want to implement, that the trigger will check if the value of some specific fields of the updated opportunitys line items (products in the opportuity) has changed. If so, I want to log this lineitems field changes, too.

I know that I have to loop through all line items and have to check the field values.

- BUT HOW CAN I DO THIS?

 

Could anyone help me, please? It would be great if you post me your code or ideally complete the existing code ;)

 

Next... I want to log if the user adds a new product/lineitem or deletes a lineitem.

 

Any ideas?

 

Thanks a lot,

jup

 

 

 


 

For tracking field value changes, why don't you use Opportunity field histrory tracking. Whatever you are doing in your trigger above, can be done without writing a single line of code using history tracking.

 

As for adding/deleting line items, you may have to implement a trigger on the line items object.

 

 

All Answers

Venkat PolisettVenkat Polisett

juppinger wrote:

Hi,

 

I want to create a trigger that fires after an opportunity will be updated.

First the trigger has to check, if specified fields in the opportunity has changed. If so, the trigger has to log this changes into a new simple custom object (text only).

I wrote my code and the first part works very well:

 

trigger HistoryOpportunityV001_test on Opportunity (after update) { // no bulk processing; will only run from the UI if (Trigger.new.size() == 1) { String strChanges = ''; //// Checking fields if the value has changed // 1. Field: Opportunity.Name if (Trigger.old[0].Name != Trigger.new[0].Name) { cnt++; strChanges += cnt + '. <Name>: "' + Trigger.old[0].Name + '" -> "' + Trigger.new[0].Name + '"\n'; } // 2. Field: Opportunity.Order_ID__c if (Trigger.old[0].Order_ID__c != Trigger.new[0].Order_ID__c) { cnt++; strChanges += cnt + '. <Order_ID__c>: "' + Trigger.old[0].Order_ID__c + '" -> "' + Trigger.new[0].Order_ID__c + '"\n'; } if (cnt > 0) { OpportunityLog__c oppLog = new OpportunityLog__c( OpportunityID__c = Trigger.old[0].Id, OpportunityName__c = 'oppName', OpportunityChangesUser__c = UserInfo.getUserId(), Changes__c = strChanges ); insert oppLog; } } }

 

 

 

Now I want to implement, that the trigger will check if the value of some specific fields of the updated opportunitys line items (products in the opportuity) has changed. If so, I want to log this lineitems field changes, too.

I know that I have to loop through all line items and have to check the field values.

- BUT HOW CAN I DO THIS?

 

Could anyone help me, please? It would be great if you post me your code or ideally complete the existing code ;)

 

Next... I want to log if the user adds a new product/lineitem or deletes a lineitem.

 

Any ideas?

 

Thanks a lot,

jup

 

 

 


 

For tracking field value changes, why don't you use Opportunity field histrory tracking. Whatever you are doing in your trigger above, can be done without writing a single line of code using history tracking.

 

As for adding/deleting line items, you may have to implement a trigger on the line items object.

 

 

This was selected as the best answer
pj_27pj_27

Sub : OpportunityLineItem field values tracking on update and delete

 

Hi ,

 

I have a similar kind of requirement where I need to write a trigger to keep track of the fields values of OpportunityLineItem that are updated/deleted. But I should be able to get these fields dynamically so that we do not need to update/modify this trigger each time for different fields.

 

Could you please suggest me a way to deal with this ?

 

Thanks in advance.

 

Regards,

pj_27