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
andersalandersal 

Problems with moving certain fields from opportunityLineItem to Asset

Hi!

 

I am trying to move an opportunityLineItem to asset. I am using some code which where posted in this forum and modified some of it. What my problem is that I am unable to get the product name to be transfered to the asset. All other data seems to be transfered ok. If anybody can help me, I would be thankful. The asset name should be the same as the product name. But there seems to be a problem here. The error msg is: 

 

Error:Apex trigger CreateAssetonDoneItem caused an unexpected exception, contact your administrator: CreateAssetonDoneItem: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CreateAssetonDoneItem: line 28, column 11

 

 

Here is the trigger:

 

trigger CreateAssetonDoneItem on OpportunityLineItem (before insert, before update) {
  List<OpportunityLineItem> list_oli = new List<OpportunityLineItem>();
  //Setup the array to hold the ids to Iterate through
   Set<Id> pbeIds = new Set<Id>();
  //Iterate through the Line Items
    for (OpportunityLineItem oli : Trigger.new)
    {   
       // Create individual post
       pbeIds.add(oli.PricebookEntryId);
       pbeIds.add(oli.opportunityId);
    }
     //Setup APEX MAP Arrays to get the required data from the Pricebook and Opportunity
    Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>([select Product2.ProductCode,Product2.Name from PricebookEntry where id in :pbeIds]);
    Map<Id, Opportunity> Opp = new Map<Id, Opportunity>([select Account.Name, Account.ID from Opportunity where id in :pbeIds]);
    

      Asset[] ast = new Asset[]{};
         Asset a = new Asset();
         Integer teller = 0;
      for(OpportunityLineItem oli: trigger.new){
               
        if(oli.Status__c == 'Godkjent hos leverandør' && oli.Converted_to_Asset__c == false)
         {
          a = new Asset();
          a.AccountId = opp.get(oli.OpportunityId).AccountId;
          //a.Product2Id = opp.get(oli.Pricebookentryid).PricebookEntry.Product2Id;
          a.Product2Id = oli.PricebookEntry.Product2.id;
          a.Product2.Name= oli.PricebookEntry.Product2.Name;
          //a.Product2 = oli.PricebookEntry.Product2Id;
          a.Quantity = oli.Quantity;
          a.Opprinnelig_kj_pesum__c = a.Quantity;
          //a.Price =  ol.UnitPrice;
          a.PurchaseDate = oli.Opportunity.CloseDate;
          a.Status = 'Mangler provisjon';
          //a.Description = ol.Description;
          a.Provisjon__c = oli.Provisjon__c;
          System.debug(entries.get(oli.Pricebookentryid).Product2.Name);
          a.Name = entries.get(oli.Pricebookentryid).Product2.Name;
          ast.add(a);
          oli.Converted_to_Asset__c = true;
          //update oli;
          }  
    }
       
 
      insert ast;
     
    
}