Skip to content. | Skip to navigation

Personal tools
Log in
Sections
You are here: Home Database Salesforce Apex & Visualforce APEX Snippet: Products-2-Assets

APEX Snippet: Products-2-Assets

This apex class takes opportunity owners (either accounts or contacts) and opportunity line items, and generates Assets for the contact/account based on the opportunity. Also required are a Ship_To_Account__c and Ship_To_Contact__c custom lookup field on the opp, and a Asset_Deployed__c boolean field on the OpportunityLineItem. This is usually triggered on Opportunity insert/update. Can be found in production in the League of Women Voters Education Fund instance.

 

/****************************************/

/* OppProductToAssetTransfer.cls        */

/* By: Kevin Bromer                        */

/* NPower Seattle, 2009 (c)                */

/* Created On: 1/28/2009                */

/* Last MOdified: 1/29/2009                */

/*                                        */

/* Transfers products from opportunity    */

/* line items and pricebooks to assets  */

/* on the appropriate contact/account   */

/* record.                                */

/****************************************/

public class OppProductToAssetTransfer {

    public integer CreateAndTransfer(map<id, Opportunity> oppMap)

    {       

        list<Opportunity> oppsWithProductIDs = new list<Opportunity>();

        list<Asset> assetsToWrite = new list<Asset>();

        list<OpportunityLineItem> productsToUpdate = new list<OpportunityLineItem>();

        system.debug('------------------------CREATE&TRANSFER RUNNING----------------');

               

        //Opps with associated product IDs       

        oppsWithProductIDs = GetProductListFromOpportunity(oppMap);

                                           

        for (Opportunity o : oppsWithProductIDs)

        {

            for (OpportunityLineItem ol : o.OpportunityLineItems)

            {           

            if ((ol.AssetDeployed__c == false) && (ol.PricebookEntry.Product2.Family != 'Non-Asset') && (o.StageName == 'Closed Won'))

                {               

                Asset a = new Asset();

               

                if(o.Ship_To_Account__c != null)

                {

                a.AccountID = o.Ship_To_Account__c;   

                }

                else if (o.Ship_To_Contact__c != null)

                {

                a.ContactID = o.Ship_To_Contact__c;

                }               

                else if(o.AccountID != Main.DefaultAcctId)//If the AccountID is NOT 'Individual'

                {

                a.AccountID = o.AccountID;

                }

                else //apply the assets to the original primary contact

                {

                a.ContactID = o.CR_Contact_ID__c;

                }//close else

               

                a.Name = ol.PricebookEntry.Product2.Name;                               

                a.Product2Id = ol.PricebookEntry.Product2.id;

                a.PurchaseDate = o.CloseDate;

                a.Price = ol.TotalPrice;

                a.Quantity = ol.Quantity;

                assetsToWrite.add(a);

                //We need to update the AssetDeployed field on the product so

                //it doesn't create the asset more than once   

                ol.AssetDeployed__c = true;

                productsToUpdate.add(ol);               

               

               

                }//close if assetdeployed   

            }//close for           

        }//close for

           

   

   

    insert assetsToWrite;

    update productsToUpdate;           

    return assetsToWrite.size();

    }

   

    private list<Opportunity> GetProductListFromOpportunity(map<id, Opportunity> thisOppMap)

    {

    list<Opportunity> oppWithProductID = new list<Opportunity>();   

           

    oppWithProductID = [select op.Ship_To_Contact__c, op.Ship_To_Account__c, op.id, op.StageName, op.CR_Contact_ID__c, op.AccountId, op.CloseDate, (select o.PricebookEntry.Product2.Family, o.PricebookEntry.Product2.Id, o.PricebookEntry.Product2.name, o.AssetDeployed__c, o.TotalPrice, o.Quantity

        from OpportunityLineItems o) from Opportunity op

        where op.ID in :thisOppMap.keySet() AND op.HasOpportunityLineItem = True];   

       

    return oppWithProductID;

    }//Close GetProductListFromOpportunity

    static testmethod void OppProductTransferTest()

    {

    testDataGeneration();

    list<Opportunity> thisOppList = new list<Opportunity>();

    id individualid = [select id from Account where Name = 'Individual'].id;

    integer x;

   

    thisOppList = [select id from Opportunity where Name like 'Test Opp'];

   

    for (Opportunity o : thisOppList)

        {

        system.assert(o.StageName == 'Closed Won');

       

        //Test the 4 types

        if (o.Ship_To_Account__c != null)

            {

            x = [select count() from Asset where AccountID = :o.Ship_To_Account__c];

            system.assert(x > 0);

            }

        if (o.Ship_To_Contact__c != null)

            {

            x = [select count() from Asset where ContactID = :o.Ship_To_Contact__c];

            system.assert(x > 0);           

            }

        if (((o.Ship_To_Contact__c == null) && (o.Ship_To_Account__c == null)) && (o.AccountID == individualid))

            {

            x = [select count() from Asset where ContactID = :o.CR_Contact_ID__c];

            system.assert(x > 0);

            }

        if (((o.Ship_To_Contact__c == null) && (o.Ship_To_Account__c == null)) && (o.AccountID != individualid))

             {

             x = [select count() from Asset where AccountID = :o.AccountID];

            system.assert(x > 0);

                

             }

       

       

       

        }

   

   

    }

   

    private static void testDataGeneration()

    {

    integer counter = 0;   

    list<Contact> cl = new list<Contact>();

    list<Account> al = new list<Account>();

    list<Opportunity> ol = new list<Opportunity>();

    list<OpportunityLineItem> olil = new list<OpportunityLineItem>();

    id SaleTypeID = [select id from RecordType where Name = 'Sale'].id;

    id PriceBookID = [select id from PricebookEntry where Product2.Family != 'Non-Asset' limit 1].id;

    id ProductID = [select id from Product2 where Family != 'Non-Asset' limit 1].id;

   

       

    Contact tcr = new Contact(LastName = 'TestName1');

    Contact tcst = new Contact(LastName = 'TestName2');

    Account tar = new Account(Name = 'TestAccount1');

    Account tast = new Account(Name = 'TestAccount2');

   

    cl.add(tcr);

    cl.add(tcst);

    al.add(tar);

    al.add(tast);

   

    insert cl;

    insert al;

   

        for (Account a : al)

        {

        counter++;

        Opportunity o = new Opportunity();

        o.AccountID = a.id;

        o.RecordTypeId = SaleTypeID;

        o.Name = 'Test Opp ' + counter;

        o.StageName = 'Prospecting';

        o.CloseDate = date.ValueOf('1980-1-1');

        if (counter == 1)

            o.Ship_To_Account__c = al[1].id;

        ol.add(o);   

        }

       

        for (Contact c : cl)

        {

        counter++;

        Opportunity o = new Opportunity();

        o.CR_Contact_ID__c = c.id;

        o.RecordTypeId = SaleTypeID;

        o.Name = 'Test Opp ' + counter;

        o.CloseDate = date.ValueOf('1980-1-1');

        o.StageName = 'Prospecting';

        if (counter == 3)

            o.Ship_To_Contact__c = cl[1].id;

        ol.add(o);

        }

       

        insert ol;

       

        for (Opportunity o : ol)

        {

        OpportunityLineItem oli = new OpportunityLineItem();

        oli.PricebookEntryID = PriceBookID;

        oli.OpportunityID = o.id;

        oli.Quantity = 1;

        oli.TotalPrice = 100;

        olil.add(oli);

        o.StageName = 'Closed Won';

        }

        insert olil;

               

               

    }

}//close class

Document Actions

Comments (0)

« February 2012 »
February
MoTuWeThFrSaSu
12345
6789101112
13141516171819
20212223242526
272829