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
Vikas MenonVikas Menon 

Triggers or Workflows

Hey Guys,

I am currently working on a module named Logistics where they manage distance education movement of Books across India. Hence this is to be tracked on SFDC
I have an object called Stock which is maintained at different places across india
So my query is :-

I have a Dispatch Order custom object ,based on which the stock at the respective centers is to be decremented 
based on the dispatch Order.

Eg Stock at -->A--->400
   Stock at---->B--->300

Dispatch order made 50 books from A to B
hence updated object value for 
A-->350
B--->350.

Please help me with the best way to go about this.
Cheers !!!
Anirudh SinghAnirudh Singh
Hi Vikas,

From the Scenario you have mentioned above. I think you have to increment or decrement on the Object 'Stock' but on different records such as 'Stock A', 'Stock B', etc., and it is all dependent on the Dispatch Order record of another object 'Dispatch Order'.

Assuming you have the two fields in Dispatch Order object one to capture which Stock record to increment and other to cature which Stock record to decrement.

But since, Dispatch Order is the Child of Stock. You can not update Stock records using a Workflow on Dispatch Order. The best way to go is with Triggers.

Please let me know if this helps.
If yes, please mark the question as Solved.


Thanks and Regards,
Anirudh Singh
Vikas MenonVikas Menon
Hi Anirudh,
I just figured that triggers is the only way. Damn !....Anyways im using the before update and after update trigger to work with this..Hope it works out well.
Thanks for your response.
Matthew VelezMatthew Velez
Hi Vikas,

Triggers would be a good way to approach this problem, but coding out the entire process isn't the only option. Alternatively, you could trigger a flow[visual workflow] that can execute your business logic.

This is a simply way to traverse parent/child relationships, both ways, and make your updates. Even if your Stock objects are not related to the 'Dispatch Order', you could still look up your desired records in the flow & apply the appriopriate value changes.

Let me know if you would like further clarification on this solution.

Cordially,

Matthew Velez
Vikas MenonVikas Menon
Hey Matthew,
Thanks for the help. Also ive just written a trigger on the dispatch order above but it throws a null pointer exception on the "after update" event.
Any thoughts ?. 
Code :-
trigger deduct on Dispatch_Order__c (before update,after update)
{
DO_Line_Item__c ds=new DO_Line_Item__c();
list<Stock__c> lst=new list<Stock__c>();
lst=[select Quantity__c from Stock__c where name in('Stock1','Stock2')];
for(Dispatch_Order__c dx:trigger.new)
{
if(dx.Status_Of_DO__c=='Confirmed')
{
for(Stock__c stk:lst)
{
stk.Quantity__c=stk.Quantity__c-ds.Quantity__c;
update lst; 
}
}
}

}
Matthew VelezMatthew Velez
Vikas, Your error is on the 'after update', not the 'before update'? If the trigger goes off 'before update', it would throw an error when trying to reference the 'Trigger.new" For Loop you have. If that isn't the problem, the issue is probably that some of your Dispatch_Order__c objects do not have related Stock__c objects to reference. Try implementing a Try...Catch block for the NullPointerException & isolate the issue. You can then come up with an alternate solution in the Catch portion of the block.
Vikas MenonVikas Menon
Thanks a ton mate....
Also while developing i had this query.
Consider that we have a city name in one of the Fields and related to that City name there is quantity of goods associated.
but i need to access the quantity of goods using a soql statement
hence for eg :-
String city='City1'
select Quantity__c from Stock__c where name in('City1');
in the above code i am manually entering the City since i know the value. What if i have to pass the city value in the soql ??.

Hope you got my point. Cheers..awaiting your reply
 
Matthew VelezMatthew Velez
Hi Vikas,

Where are you passing the city variable from and what is the connection to the Quanitity? Are they on the same object or separate? If it is from the record in the trigger, you can do a Select Statement into a string field, like you did, and then pass that into the SOQL query to get your desired stock using the :variable syntax to use a variable from your Apex code in the subsequent SOQL query.

SELECT Quantitity__c
FROM Stock__c
WHERE City = :SelectedCity

While this is still along the Apex path of solving your problem, I really think you can accomplish your goal for efficiently using a Visual Workflow, passing your desired variables through the URL.
Vikas MenonVikas Menon
hey matthew,
I am going through this wierd situation where the database is appened with a particular record but it aint showing in the custom object .

For eg :-
In the developer console while i check a list and enter the list it is seen in my custom object but when it comes to my trigger. It doesnt work.

pls help!
Matthew VelezMatthew Velez
Hi Vikas,

Does your trigger commit the changes back to the database at execution?
Vikas MenonVikas Menon
Hi Matthew,

No bro...it aint happening...Like the list isnt getting inserted when the trigger executes.
 
Matthew VelezMatthew Velez
Hi Vikas,

Please post your code if you would like further assistance. I cannot help with the level of detail explained as of now.
Vikas MenonVikas Menon
Hi Matthew, 

Im Sharing the code below. It has all the necessary comments attached. Thanks for the Help :-)














trigger deduct on Dispatch_Order__c (before update,after update)
                                                {
                                                      list<Dispatch_Order__c> dorder=new list<Dispatch_Order__c>();
                                                      dorder=[select From_Center__c,To_Center__c,Stock_Keeping_Unit__c,Quantity__c from Dispatch_Order__c];
                                                     
                                                      Dispatch_Order__c a;
                                                  for(Dispatch_Order__c dol:dorder)
                                                  {
                                                          a=dol;
                                                  }
                                                  list<Stock__c> lst1=new list<Stock__c>();  //for particular stock derived from the Dispatch order (From Center Value)
                                                  lst1=[select Center__c,Stock_Keeping_Unit__c,Quantity__c,Outgoing_Quantity__c from Stock__c where Center__c=:a.From_Center__c and Stock_Keeping_Unit__c=:a.Stock_Keeping_Unit__c];
                                                  list<Stock__c> lst2=new list<Stock__c>();//for particular stock derived from the Dispatch order (To Center Value)
                                                  lst2=[select Center__c,Stock_Keeping_Unit__c,Quantity__c,Outgoing_Quantity__c from Stock__c where Center__c=:a.To_Center__c and Stock_Keeping_Unit__c=:a.Stock_Keeping_Unit__c];
                                                  
                                                
                                                 
                                                  for(Dispatch_Order__c doc:trigger.new)
                                                  {
                                                  
                                                      if(lst2.isEmpty())                                   //If the list2 is empty then that particular SKU type is not present at the "To Center" fetched from the Dispatch Order object
                                                      {
                                                      
                                                           Stock__c inlst1=new Stock__c();                 //Creating a new Object
                                                           for(Stock__c stock:lst1)
                                                           {
                                                           
                                                               inlst1.Stock_Keeping_Unit__c=stock.Stock_Keeping_Unit__c;              //Initializing the SKU and Center Type along with name.
                                                               inlst1.Center__c=a.To_Center__c;
                                                               inlst1.name='newrecord';
                                                               lst2.add(inlst1); 
                                                           
                                                           }
                                                           
                                                               insert lst2;                           // inserting list
                                                           
                                                           
                                                     } 
                                                     
                                                        
                                                        
                                                          
                                                          
                                                      
                                                     
                                                  
                                                         Dispatch_Order__c dispatch=Trigger.oldMap.get(doc.Id);
                                                         Boolean OldStageValueIsTransit=dispatch.Status_Of_DO__c.equals('Transit');
                                                         Boolean OldStageValueIsDelivered=dispatch.Status_Of_DO__c.equals('Delivered');
                                                         
                                                      
                                                        if(checkRecursive.runOnce())
                                                        {
                                                     
                                                  
                                             
                                                  
                                                        if(doc.Status_Of_DO__c=='Transit' && !OldStageValueIsTransit)
                                                        {
                                                               
                                                             for(Stock__c stk1:lst1)
                                                             {
                                                                 if(stk1.Quantity__c>a.Quantity__c)
                                                                 {
                                                                         stk1.Quantity__c=stk1.Quantity__c-a.Quantity__c;
                                                                         stk1.Outgoing_Quantity__c=a.Quantity__c;
                                                             
                                                                         update lst1;
                                                                 
                                                                
                                                                 }
                                                                 else
                                                                 {
                                                                         doc.addError('Insufficient Quantity at the Stock');
                                                                 }
                                                                 
                                                                 
                                                                 } 
                                                                    
                                                         
                                                         
                                                         
                                                                 }
                                                         
                                                         
                                                                 }
                                                                 
                                                         if(doc.Status_Of_DO__c=='Delivered' && OldStageValueIsTransit && !OldStageValueIsDelivered)
                                                         {
                                                        
                                                         
                                                             for(Stock__c stk2:lst1)
                                                             {
                                                                 for(Stock__c stk3:lst2)
                                                                 {
                                                                     stk3.Quantity__c=stk3.Quantity__c+stk2.Outgoing_Quantity__c;
                                                                     stk2.Outgoing_Quantity__c=0;
                                                                 
                                                                 
                                                                 }
                                                                 
                                                                 }
                                                            
                                                                     update lst1;
                                                                     update lst2;
                                                                 }
                                                            
                                                                 }
                                                             
                                                             }