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
sathisathi 

pls give me solution for trigger

i have two objects inventory and order ....when a record is created in order object for an item then automatically the field quantity avialable in inventory is to be updated with( quantity available  minus  order quantity)

 and i have a hint also (select Inventory record where id=Inventory look up field’s API name)............please help m,e for this trigeer

Best Answer chosen by Admin (Salesforce Developers) 
Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi sathi;

 

please change the lookupfield's API name as pe your lookup field name of order on line 6 and 15.

 

Line 6

orderid.add(order.lookupfield's API Name)

 

Line 15  

 if(inv.id == ord.lookupfield's API Name) 

 

replace with your lookup field api name

 

Please mark as solution if it solved your issue and it will help others who needs help for similar issue.

All Answers

vivek12345vivek12345

Hello,

 

Is there any link between Item and Inventory object and Item and order object.

 

If there is like lookup or masterdetail, you can run a trigger on order object which is link with Item record, from where you can get the item Id put that in a list (Set<String> IdList =  new Set<String>();), then in your trigger query on inventory object where Item Id in:IdList.

 

Now you will get the list of Inventory record which are linked with that Item record, then you can update the value on the Inventory object.

 

Hope this will help, please do let me know this helps.

 

Thanks

Vivek

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Sathi,

 

You can use following Apex Trigger Code.

 

trigger updateQuantity on Order__c(after insert){

       set<id> orderid = new set<id>();

       List<order__c> orderlist = new List<order__c>();

 

    for(Order__c order: Trigger.new){

             orderid.add(order.lookupfield's API name);

             orderlist.add(order);

    }

 

    if(orderid.size()>0) {

             List<Inventroy> inventorylist = [select quantity_available__c from Inventory__c where id IN : orderid];

             

              for(Inventory__c inv: Inventorylist){

                        for(Order__c ord: orderlist){

                                 if(inv.id == ord.lookupfield's API Name)

                                 inv.quantity_available__c = inv.quantity_available__c-ord.order_Quantity__c;

                        }

              }

             

              try{

                    update inventorylist;

             }catch(DMLException ex){System.debug('Exception is '+ex);}

    }

}

 

 

If this is you solution please mark as a solution. So others can take help from here for similar problem.

sathisathi

Thank you for ur response,

but im getting the error 

 

line breaks are not allowed in String literals at line 6 column-1


 

Vinit_KumarVinit_Kumar

Sathi,

 

Did uyou change this line

 

orderid.add(order.lookupfield's API name); // with the API name of relationship field

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi sathi;

 

please change the lookupfield's API name as pe your lookup field name of order on line 6 and 15.

 

Line 6

orderid.add(order.lookupfield's API Name)

 

Line 15  

 if(inv.id == ord.lookupfield's API Name) 

 

replace with your lookup field api name

 

Please mark as solution if it solved your issue and it will help others who needs help for similar issue.

This was selected as the best answer
sathisathi

Hi ,

I got it ,thank you so much.........

sathisathi

hi ,

I have doubt ...in this trigger can't we use single variable instead of list(inventory list) at line 11.

thank you.

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Sathi,

 

When you insert multiple order for a single inventory then it does not work thats why i am using list instead of single variable.

This is bulkified trigger ant it work for both when insert one  order or multiple order at a time.

 

You can use single variable but then it does not work when you insert multiple order fro Data loader at a time for single inventory or multiple inventory.

 

I think now you are able to understand why i am using inventory list.

sathisathi
thank you i got the point.......