Skip to main content

Hello, in my org I have an Apx trigger implemented in an old past and I'm not a developer.

The trrigger is very simple and creates Assets from opportnuty Line Items when the Oppty is Closed-Won.

Now, I want to make a little change because the trigger applies as Asset Name the Product Code and I want the Product Name instead.

So, I made the change as in the highlighted line but the Asset name result is opportunity name + product name.

apex trigger for asset creation not working properly

 

I tried to change to 

 

a.name=oli.product2.Name and doesn't work (I have an error)

 

What should I do?
7 answers
  1. Apr 1, 2021, 1:41 PM

    I'd try this:

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

    system.debug('tr:'+trigger.isUpdate);

    if(trigger.isUpdate==true && opportunityHelper.runAfterOnce()==true){

    opportunityHelper.manageMra(trigger.newMap, trigger.oldMap);

    }

    //mappa delle opportunità che sto inserendo/aggiornando

    map<id, opportunity> triggerOpps = new map<id, opportunity>();

    for(opportunity o :trigger.new){

    if(o.StageName=='Contract signed'){

    triggerOpps.put(o.id, o);

    }

    }

    system.debug('trigger opp '+trigger.new);

    if(triggerOpps.size()>0){

    //mappa dei prodotti opportunità

    map<id, opportunitylineitem> oppLines = new map<id, opportunitylineitem>();

    for(opportunitylineitem ol :[select id, name, productCode, description, unitprice, totalprice, product2id, quantity, discount,

    subtotal, business_type__c, companies_range__c, credits__c, data_copia_asset__c, maintenance_rate__c,

    opp_ty_type__c, users__c, Opportunity.accountid, Opportunity.CurrencyIsoCode, Product2.Name

    from opportunitylineitem where opportunityId in :triggerOpps.keySet() and data_copia_asset__c=null]){

    oppLines.put(ol.id, ol);

    }

    //asset da scrivere

    list<asset> listAsset = new list<asset>();

    //opp line item da aggiornare

    list<opportunitylineitem> lineItems = new list<opportunitylineitem>();

    for(opportunitylineitem oli :oppLines.values()){

    asset a = new asset();

    a.name=oli.Product2.Name;

    a.Product2Id=oli.Product2Id;

    a.AccountId=oli.Opportunity.accountid;

    a.Opportunity__c=oli.OpportunityId;

    //DA CALCOLARE MAINTENANCE RATE:

    //Calcolare e metterlo nel campo Asset Maintenance amount (Prezzo di Vendita * Maintenance Rate)

    if(oli.totalPrice!=null && oli.maintenance_rate__c!=null){

    a.Maintenance_amount__c=(oli.totalPrice*oli.maintenance_rate__c)/100;

    }

    a.Maintenance_Rate__c=oli.Maintenance_Rate__c;

    a.Users__c=oli.Users__c;

    a.Orig_Users_Number__c=oli.users__c;

    a.Managed_Employees__c=oli.Companies_Range__c;

    a.Orig_Empl_Number__c=oli.Companies_Range__c;

    a.CurrencyIsoCode=oli.Opportunity.CurrencyIsoCode;

    a.License_Amount__c=oli.TotalPrice;

    a.Discount_rate__c=oli.discount;

    a.Id_Univoco__c=oli.id+'-'+oli.OpportunityId;

    listAsset.add(a);

    oli.Data_Copia_Asset__c=system.now();

    oli.SkipValidation__c=true;

    lineItems.add(oli);

    }

    upsert listAsset id_univoco__c;

    update lineItems;

    }

    }

     

     
0/9000