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
Vigneshwaran LoganathanVigneshwaran Loganathan 

Need help for following Trigger

I have given with a situation, In Opportunity whenever the Status Goes 'Closed won' need to write Trigger like, Opportunity line item fields ( Name, Productname, price, Quantity, Account ) should be copied to ASSET ( Which is in product Object ) as new RECORD. Asset having three fiels Asset name ( Product name ), Account and Contact. Here How to get CONTACT ID from Opportunity.

I gave Trigger below, its working and I can save records in opportunity but datas not copied to ASSET ( Which is in Product Object )

trigger Demo on Opportunity (after insert, after update)
        List<opportunitylineitem> opp1 = new List <opportunitylineitem>();
        Map<string,string> product = new map<string,string>();
        List<product2> product1 = new List<product2>();
        List<product2> updateproduct1 = new List<product2>();
         //List<Product2> products = new List<Product2>();
        List<Asset> ast = new List<Asset>();
                Opportunity oldOpp = trigger.oldMap.get(opp.Id);        
                if(opp.StageName == 'Closed Won' && opp.StageName != oldOpp.StageName)
                    opp1 = [select id,name,ProductCode,Quantity from opportunitylineitem where Opportunityid =];
                    //product2 products = [select id, Name from Product2 where];
                    for(opportunitylineitem op : opp1)
                        List<string> productcode = new List<string>();
                   string value =' ';
                        product1 = [select id,name from Product2 where productcode in : productcode];
                        Asset a= new Asset();
                        for(Product2 prodnew:product1)
                        string ss1 = prodnew.productcode;
                                value = product.get(ss1);
                               // system.debug(logginglevel.ERROR,'%%%% Value: ' + value);
                        a.Name = value;
                        a.Quantity = op.Quantity;
                       // system.debug(logginglevel.ERROR,'%%%% Asset Name/Quantity: ' + a.Name + ' ' + a.Quantity);
        insert ast;
because for creating Asset you need to madatory fields Name and Account or contact, and I see neither you have passed account or contact.Pass atleast one. you can easily pass Account