You need to sign in to do that
Don't have an account?
Jan Vandevelde
Update an opportunity field when a product that meets specific criteria is added or removed
Hi all,
to start off, I'm not a coder but just a Salesforce Admin. And I think I'll need to do what I want using a trigger. So if anybody could help me achieve this I would greatly appreciate it.
On and opportunity I've got a custom checkboxfield called Guarantee__c (true or false), I would like that this checkbox is checked whenever I add a product to the opportunity that has in a custom productfield Product_Code__c the word "GUAR". offcourse the checkbox should also be unchecked whenever I remove all products containing the word GUAR.
Just to be clear:
custom field on opportunity: Guarantee__c (checkbox)
custom field on product: Product_Code__c
some products have in the product code the word GUAR exemple: GUAR-5, GUAR-3, GUAR-7
Whenever adding one or more products to the opportunity which have the word GUAR in their Product_Code__c I want the checkbox on the opportunity Guarantee__c set to TRUE and if the opportunity is not containing any of those products it should be false.
Can anybody help me with how I can get this done?
Thanks in advance
to start off, I'm not a coder but just a Salesforce Admin. And I think I'll need to do what I want using a trigger. So if anybody could help me achieve this I would greatly appreciate it.
On and opportunity I've got a custom checkboxfield called Guarantee__c (true or false), I would like that this checkbox is checked whenever I add a product to the opportunity that has in a custom productfield Product_Code__c the word "GUAR". offcourse the checkbox should also be unchecked whenever I remove all products containing the word GUAR.
Just to be clear:
custom field on opportunity: Guarantee__c (checkbox)
custom field on product: Product_Code__c
some products have in the product code the word GUAR exemple: GUAR-5, GUAR-3, GUAR-7
Whenever adding one or more products to the opportunity which have the word GUAR in their Product_Code__c I want the checkbox on the opportunity Guarantee__c set to TRUE and if the opportunity is not containing any of those products it should be false.
Can anybody help me with how I can get this done?
Thanks in advance
Both can be done, sounds like two triggers. I've got the first trigger partially written and will share here. Not sure if I will have time to finish, but maybe some other nice person might.
trigger UpdateOpptyProd on OpportunityLineItem (after insert, after update, after delete) {
// Get all the other products on the oppty
List<OpportunityLineItem> lineItems;
if (trigger.IsDelete) {
lineItems = Trigger.Old;
} else if (trigger.IsInsert || trigger.IsUpdate) {
lineItems = Trigger.New;
}
Map<String, OpportunityLineItem> opptyLookup = new Map<String, OpportunityLineItem>();
for (OpportunityLineItem li : lineItems) {
opptyLookup.put(li.OpportunityId, li);
}
List<Opportunity> oppty = [Select Opportunity.Id From Opportunity Where Id in
(Select OpportunityId From OpportunityLineItem where OpportunityId in :opptyLookup.keySet() And PriceBookEntry.Product2.Product_Code__c Like 'GUAR%')];
if (oppty.size() == 0) {
return;
} else {
for (Opportunity o : oppty) {
// Note, just setting a text value not a checkbox.
o.Guarantee__c = '1';
}
update oppty;
}
}