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
EtauEtau 

Trigger

I need to write what I believe is a fairly simple trigger, but have no idea where to learn how to do this.  I need to update an inventory amount field on a custom object when a true/false check box is checked on an opportunity line item.  Any direction would be appreciated.

imutsavimutsav
Write a trigger on Opportunity Line Object (after insert, after update). I don't know the relationship between your Opportunity Line Object and your Custom Object so I am giving you just an idea.

trigger trgupdateInventory on OpportunityLineItem (after insert, after update) {
for(OpportunityLineItem oLI : Trigger.new) {
if(oLI.YOUR_CUSTOM_CHECKBOX__c==TRUE) {
List<CustomOBject> custObject = [Select id, inventory__c from CustomObject where relatedField__c =:oLI.Id];
update your inventory field

Let me know if you have any question.

Thanks
Utsav

[Do mark this answer as solution if it works for you and give a kudos.]
Vinit_KumarVinit_Kumar

Couple of things here,

 

1.) I would not recommend to use SOQL query inside for loop as you will the governor limits.

 

2. ) You should not perfom DML inside for loop.

 

If you can let me know how the objects are related,I can help you out.

EtauEtau
Hi Vinit - Thanks for your input. I am trying to get a handle on
inventory. When our French office sends out a DVD, they check a box
that it has been sent. There is a lookup on a custom object that houses
all of our movie titles with an inventory quantity field. When a DVD is
sent out and the box is checked on the opportunity line item, I'd like
the inventory to decrease by one. When it is returned and another box
is checked, I'd like the inventory to increase by one. I am
consistently frustrated by opportunity line items due to the
restrictions on lookup relationships. Any help would be appreciated.



Custom Object:

French_Title_Lookup__r

Field (Numeric): French_Title_Lookup__r.Inventory__c



Opportunity Product - lookup relationship on French_Title_Lookup__r



Opp Product Fields (Check Boxes - True False):



Sent_DVD_s__c



DVD_Returned__c



So - if DVD(s)_Sent__c = true, French_Title_Lookup__r.Inventory__c - 1

If DVD_Returned__c = true, French_Title_Lookup__r.Inventory__c + 1








--------------------------------------------------------------------------
Ellen Taussig | Database Manager | P:314.984.6122 | F: | Email: mailto:etaussig@swankmp.com
Swank Motion Pictures, Inc. | 10795 Watson Road | St. Louis, MO 63127 | United States of America | http://www.swank.com
 
This email and any attachments are the property of Swank Motion Pictures, Inc. and are intended solely for the use of the email recipient or entity to whom the email is addressed. Emails are not to be distributed to other parties without the express written permission of the original sender. If you are not the intended recipient of this email, please delete this message. Any other use, retention, or dissemination is strictly prohibited.
Please consider the environment before printing this email
--------------------------------------------------------------------------
Vinit_KumarVinit_Kumar

Etau,

 

 

Try below this should work:-

 

trigger trgupdateInventory on OpportunityLineItem (before insert, before update){

List<French_Title_Lookup__c> updatedList= new List<French_Title_Lookup__c>();
list<Id> oliIds =  new List<Id>();

for(OpportunityLineItem oLI : Trigger.new) {
if(oLI.Sent_DVD_s__c==true || oLI.DVD_Returned__c){
	oliIds.add(oLI.id);
}
}


List<French_Title_Lookup__c> frList = [Select inventory__c from French_Title_Lookup__c where relatedField__c in:oliIds]; // replace relatedField__c with  relationship field name on French_Title_Lookup__c

for(OpportunityLineItem oLI : Trigger.new){
for(French_Title_Lookup__c fr:frList){
if(oLI.Sent_DVD_s__c==true){
fr.inventory__c=fr.inventory__c + 1;
}
else if(oLI.DVD_Returned__c==true){
fr.inventory__c=fr.inventory__c - 1;
}
updatedList.add(fr);
} 
}
update updatedList;
}

 

EtauEtau
Thanks Vinit - This is an error message I am getting:




Error: Compile Error: expecting right curly bracket, found 'EOF' at line
0 column -1


--------------------------------------------------------------------------
Ellen Taussig | Database Manager | P:314.984.6122 | F: | Email: mailto:etaussig@swankmp.com
Swank Motion Pictures, Inc. | 10795 Watson Road | St. Louis, MO 63127 | United States of America | http://www.swank.com
 
This email and any attachments are the property of Swank Motion Pictures, Inc. and are intended solely for the use of the email recipient or entity to whom the email is addressed. Emails are not to be distributed to other parties without the express written permission of the original sender. If you are not the intended recipient of this email, please delete this message. Any other use, retention, or dissemination is strictly prohibited.
Please consider the environment before printing this email
--------------------------------------------------------------------------
Vinit_KumarVinit_Kumar

You are missing the braces somewhere,Can you check you have all the braces opened and closed correctly.

 

Copy the code again and then re-paste it

EtauEtau
Could it be this?



trigger trgupdateInventory on OpportunityLineItem (before insert, before
update){ List updatedList= new
List(); list oliIds = new List();
for(OpportunityLineItem oLI : Trigger.new) { if(oLI.Sent_DVD_s__c==true
|| oLI.DVD_Returned__c){ oliIds.add(oLI.id); } }
List frList = [Select inventory__c from
French_Titles_and_Studios__c where relatedField__c in:oliIds]; //
replace relatedField__c with relationship field name on
French_Titles_and_Studios__c for(OpportunityLineItem oLI : Trigger.new){
for(French_Titles_and_Studios__c fr:frList){
if(oLI.Sent_DVD_s__c==true){ fr.inventory__c=fr.inventory__c + 1; } else
if(oLI.DVD_Returned__c==true){ fr.inventory__c=fr.inventory__c - 1; }
updatedList.add(fr); } } update updatedList; }




--------------------------------------------------------------------------
Ellen Taussig | Database Manager | P:314.984.6122 | F: | Email: mailto:etaussig@swankmp.com
Swank Motion Pictures, Inc. | 10795 Watson Road | St. Louis, MO 63127 | United States of America | http://www.swank.com
 
This email and any attachments are the property of Swank Motion Pictures, Inc. and are intended solely for the use of the email recipient or entity to whom the email is addressed. Emails are not to be distributed to other parties without the express written permission of the original sender. If you are not the intended recipient of this email, please delete this message. Any other use, retention, or dissemination is strictly prohibited.
Please consider the environment before printing this email
--------------------------------------------------------------------------