Skip to main content
Hi,

I am new to apex code I would  appreciate any assistance on the following question:

I am trying  to write apex  code that auto sums up the total assets per product . I have a custom field in product object called "Initial stock", and in the asset object, I  have a custom picklist field  called "Status" with values like "check-in" and  "Checkout", and also a  product field  which is a lookup field to the standard product object.

My business idea is  thus:  Every new or existing asset should have  "Check-in" as the asset status and  a trigger should auto sum up all the checked-in assets under each product and put the number in the "Initial stock" field in the product table.

And when the asset status changes to "Checkout", a custom field called "Consumed stock" should be auto updated with the number of assets checked out per product.

Any help will be highly appreciated!
9 answers
  1. Aug 11, 2017, 8:25 PM
    I've reproduced the code with (hopefully) all the issues fixed.  I also changed the placement of the test for productIds being empty.  You don't want to do the query with an empty set - it will throw an error.

    <pre>

    public trigger UpdateProductStockCounts on Asset ( after update )

    {

        // map which stock counts go into which product fields

        Map<String,String> stockFields = new Map<String,String>

        {

            'Available' => 'Available_Stock__c',

            'Not Available' => 'Consumed_Stock__c'

        };

     

        // figure out which products need to be updated

        Set<Id> productIds = new Set<Id>();

        for ( Asset asset : Trigger.new )

        {

           Asset oldAsset = Trigger.oldMap.get( asset.Id );

            if ( asset.Status__c != oldAsset.Status__c )

            {

                productIds.add( asset.Product2Id );

            }

        }

     

        if ( !productIds.isEmpty() )

        {

            // use aggregate query to subtotal asset counts by product and status

            Map<Id,Product2> productsToUpdate = new Map<Id,Product2>();

            for ( AggregateResult result :

                [   SELECT  COUNT(Status__c) stock, Status__c, Product2Id

                    FROM    Asset

                    WHERE   Product2Id IN :productIds

                    GROUP BY ROLLUP(Product2Id, Status__c)

                ]

                )

            {

                Id productId = (Id) result.get( "Product2Id" );

                if ( !productsToUpdate.containsKey( productId ) )

                {

                    productsToUpdate.put( productId, new Product2( Id = productId ) );

                }

                Product2 product = productsToUpdate.get( productId );

                String status = (String) result.get( "Status__c" );

                Decimal stock = (Decimal) result.get( "stock" );

                String field = stockFields.get( status );

                if ( field != null )

                {

                    product.put( field, stock );

                }

            }

            update productsToUpdate;

        }

    }

    </pre>
0/9000