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
sankumsankum 

how solvethis issue using trigger

hi to all ,

     I have one object name 'product' and it has fields like avalable products, howmany products selected

 my requirement is total products have 1000  if i selected 22 products , then available products is (1000 minus(-) 22)

how to write the code for this!!!

Please help...

  

souvik9086souvik9086

Try like this

 

trigger PopulateAvailableproduct on Product__c(before insert,before update){
for(Product__c p: Trigger.new){
if(p.TotalProduct__c != NULL && p.howmanyproductsselected__c != NULL){
p.AvailableProduct__c = p.TotalProduct__c - p.howmanyproductsselected__c;
}
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

Michael_TorchedloMichael_Torchedlo

What you described sounds like a simple math rule that is active at all times, which should not require writing any code.  It could be accomplished with a Workflow Rule + Field Update, or even a Formula Field.

 

Workflow Rule - every time a 'product' is edited

Do Field update: Available Products = 1000 - Selected products.

 

Or just make Available Products a formula field.  Formula value = 1000 - Selected products (at all times).