1

I have a trigger on opportunity that inserts a new asset. I have a field that is a lookup field for asset on the opportunitylineitem object called asset__c. I simply need that field to be updated with the asset id that was just inserted (i.e. asset__c = id of new asset). Here is a segment of the code where I am creating the asset:

List<opportunitylineitem>opps=[Select id, product2id, product2.name, name, create_asset__c, 
                                   opportunity.Status__c, ProductCode, Line_of_Business__c, 
                                   Opportunity.Name, Opportunity.Description, 
                                   opportunity.account.accountnumber, opportunity.Partner_Account__c, 
                                   Opportunityid, Implementation_Requires_Master_Ticket__c, 
                                   opportunity.accountid 
                               from opportunitylineitem where Opportunityid in: Trigger.new];

For(OpportunityLineItem opp:opps)
         { if(opp.create_asset__c == true && opp.opportunity.status__c == 'Accepted') {
        asset asset = new asset();

        asset.accountid = opp.opportunity.accountid;
        asset.name = opp.product2.name;
        asset.status = 'Contracted';
        asset.product2id = opp.product2id;
        asset.Line_of_Business__c = opp.Line_of_Business__c;
        asset.PartnerAccount__c = opp.opportunity.Partner_Account__c; 

        newasset.add(asset);

My question is how to grab the newly inserted id and pass it back to the opportunity product record?

  • Please add some detail besides just a code dump. This is not a question as posted and will otherwise be closed. – Adrian Larson Aug 10 '15 at 20:52
  • Ok. The trigger above is inserting a new asset and need to be able to pass the new id to the opportunitylineitem of the opportunity. I have a field that is a lookup field for asset on the opportunitylineitem object called asset__c. I simply need that field to be updated with the asset id that was just inserted (i.e. asset__c = id of new asset). Does that clear thing up a bit? – sfdev09 Aug 10 '15 at 21:01
  • Take a look at how to ask a good question. You should modify your questions instead of adding to the comments, it will be easier for people to understand what you are asking and provide you with help. – Adrian Larson Aug 10 '15 at 21:11
1

Typically what I have done in this type of situation is to insert the new objects:

insert newasset;

Then loop through the objects you want to link to the new objects, setting the value of the appropriate field:

for(Integer i = 0; i < opps.size(); i++){
    opps[i].asset__c = newasset[i].Id;
}

Then perform an update:

update opps;

New Answer

map<Id,Asset> olitoasset = new map<Id,Asset>();

For(OpportunityLineItem opp:opps)
     { if(opp.create_asset__c == true && opp.opportunity.status__c == 'Accepted') {
    asset asset = new asset();

    asset.accountid = opp.opportunity.accountid;
    asset.name = opp.product2.name;
    asset.status = 'Contracted';
    asset.product2id = opp.product2id;
    asset.Line_of_Business__c = opp.Line_of_Business__c;
    asset.PartnerAccount__c = opp.opportunity.Partner_Account__c; 

    olitoasset.put(opp.Id,asset);

}}

insert olitoasset.values();

list<OpportunityLineItem> olitoupdate = new list<OpportunityLineItem>();
for(Id i : olitoasset.keySet()){
    olitoupdate.add(new OpportunityLineItem(Id=i, asset__c=olitoasset.get(i).Id));
}
update olitoupdate;
  • Thanks Martin. However, I am getting error, 'Illegal assignment from Asset to Id'. I am able to get it to work with doing a simple select suchas: Asset assetid=[select id from asset where id in: newasset]; opp.asset__c = assetid.id; update opp; Problem with that though is that its a query in the For loop but have read somewhere that this is not as critical when the call method is only an insert. – sfdev09 Aug 11 '15 at 14:20
  • @sfdev09 I updated the code to fix the error. That query in the for loop will probably cause problems for you eventually and I suggest you get rid of it. – martin Aug 11 '15 at 14:29
  • @sfdev09 also what you have in your comment won't work if there is more than one opportunity line item, which certainly will cause problems. – martin Aug 11 '15 at 14:35
  • I tried your change and am now getting: System.ListException: List index out of bounds: 1. I am checking if the newasset list is empty but still get this error. – sfdev09 Aug 11 '15 at 15:07
  • if(!newasset.isEmpty()) { insert newasset; } if(newasset.size() > 0) { for(Integer i = 0; i < opps.size(); i++) { opps[i].asset__c = newasset[i].Id; } } update opps; – sfdev09 Aug 11 '15 at 15:09
0

That did the trick and makes sense due to the first line item already did it's insert and when a second DML call is made then it's updating with same the same asset id. I will need to add some validation to prevent that from happening. Thanks Martin for your insight and knowledge.

  • No problem. Glad to help. – martin Aug 11 '15 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.