Skip to main content
Adriana Smith (TWG Plus) 님이 #Apex에 질문했습니다
Hello, I'm building a trigger that I had at a previous company where an asset is populated on the account, from the listed products on an Opportunity, anytime it is Closed Won.

the trigger is:

trigger CreateAssetonClosedWon on Opportunity (after insert, after update) {

     for(Opportunity o: trigger.new){ 

      if(o.isWon == true && o.HasOpportunityLineItem == true){

         String opptyId = o.Id;

         OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Project_Association__c, Product_Family__c, Product_Category__c, Converted_to_Asset__c  

                                      From OpportunityLineItem 

                                      where OpportunityId = :opptyId  and Converted_to_Asset__c = false];

         Asset[] ast = new Asset[]{};

         Asset a = new Asset();

         for(OpportunityLineItem ol: OLI){

            a = new Asset();

            a.AccountId = o.AccountId;

            a.Product2Id = ol.PricebookEntry.Product2Id;

            a.Quantity = ol.Quantity;

            a.Price =  ol.UnitPrice;

            a.PurchaseDate = o.CloseDate;

The error I'm getting is: Error: Compile Error: expecting right curly bracket, found '<EOF>' at line 17 column 0

I added a curly bracket on line 17, so it pushed the error to line 18.  What am I missing?
답변 24개
  1. 2017년 4월 14일 오후 4:52

    Trigger CreateAssetonClosedWon on Opportunity (after insert, after update){

    Set<string> OppsIds = new set<string>();

    Set<string> accIds = new set<string>();

    List<Asset> assets = new List<Asset>();

    For(opportunity o : trigger.new)

    {

    OppsIds.add(o.id);

    accIds.add(o.accountid);

    }

    Map<id, opportunityLineItem> mapOpp = new Map<id, opportunityLineItem>([select Id, unitprice, quantity,pricebookentry.product2id from opportunityLineItem where opportunityId IN : OppsIds and converted_to_Asset__c = false]);

    Contact con = [Select id from Contact where accountid =: accIds order by CreatedDate limit 1 ];

    For(opportunity opp : trigger.new)

    {

    For (opportunityLineItem Oli : mapOpp.values())

    {

    If(opp.iswon == true && opp.hasopportunitylineitem ==true)

    {

    Asset a = new asset();

    a.name = a.Opportunity_Name__c;

    a.accountid = opp.Accountid;

    a.contactid = con.id;

    a.product2id = Oli.pricebookentry.product2id;

    a.quantity = Oli.quantity;

    a.price = Oli.unitprice;

    a.purchasedate = opp.closedate;

    assets.add(a);

    }

    }

    }

    If (assets.size()>0)

    Insert assets;

    }

    Please try this I have made the changes:))

    let me know what happens?

    Thanks
0/9000