• Tyler Ditto
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 7
    Replies
I am new apex programming and am struggling a little bit. I have pieced together some code in an attempt to build a rollup from the Assets to Contracts.

The trigger is supposed to be able to get sum all of the assets' summary_asset_cost__c fields that are related to the contract and post it to the base_product_rev__c field on the contract. I am getting an error when trying to call the relationship in the second query. Also please notfiy if this is the best way to accomplish this goal, as stated before I am not real skilled in apex programming.



trigger AssetCostRollup on Asset (after delete, after insert, after update) {

    Set<id> contractIds = new Set<id>();
    List<Contract> contractsToUpdate = new List<Contract>();

    for (Asset item : Trigger.new)
        contractIds.add(item.Asset);

    if (Trigger.isUpdate || Trigger.isDelete) {
        for (Asset item : Trigger.old)
            contractIds.add(item.Contract);
    }

    // get a map of the contracts and the costs of the assets on the contract
    Map<id,Contract> contractMap = new Map<id,Contract>([select id, summary_asset_cost__c from Asset where id IN :contractIds]);

    // query the contracts and the related assets and sum the cost of the assets to the contracts base_product_rev__c field
    for (Contract contract : [select Id, Name, summary_asset_cost__c,(select id from contract_lookup__r) from Asset where Id IN :contractIds]) {
        contractMap.get(contract.Id).asset_summary_cost__c = contract.base_product_rev__c.sum();
        
        contractsToUpdate.add(contractMap.get(contract.Id));
    }

    update contractsToUpdate;

}
}
 
I am new apex programming and am struggling a little bit. I have pieced together some code in an attempt to build a rollup from the Assets to Contracts.

The trigger is supposed to be able to get sum all of the assets' summary_asset_cost__c fields that are related to the contract and post it to the base_product_rev__c field on the contract. I am getting an error when trying to call the relationship in the second query. Also please notfiy if this is the best way to accomplish this goal, as stated before I am not real skilled in apex programming.



trigger AssetCostRollup on Asset (after delete, after insert, after update) {

    Set<id> contractIds = new Set<id>();
    List<Contract> contractsToUpdate = new List<Contract>();

    for (Asset item : Trigger.new)
        contractIds.add(item.Asset);

    if (Trigger.isUpdate || Trigger.isDelete) {
        for (Asset item : Trigger.old)
            contractIds.add(item.Contract);
    }

    // get a map of the contracts and the costs of the assets on the contract
    Map<id,Contract> contractMap = new Map<id,Contract>([select id, summary_asset_cost__c from Asset where id IN :contractIds]);

    // query the contracts and the related assets and sum the cost of the assets to the contracts base_product_rev__c field
    for (Contract contract : [select Id, Name, summary_asset_cost__c,(select id from contract_lookup__r) from Asset where Id IN :contractIds]) {
        contractMap.get(contract.Id).asset_summary_cost__c = contract.base_product_rev__c.sum();
        
        contractsToUpdate.add(contractMap.get(contract.Id));
    }

    update contractsToUpdate;

}
}
 
I am brand new to Salesforce, and still working in our trial but trying to test out a few apps while im in the trial.  I just installed the Generate Orders (w/ Quotes) app.  I am looking to create orders from quotes.  I added the Generate Order button to the quote page layout.  When I tried to use the button I got the below error and the below email. I did not modify the flow template (that I know of).  

Also, let me know if this is miscateforized? 

An unhandled fault has occurred in this flow
An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information.


Encountered unhandled fault when running process Quote_to_Order/30137000000ChYs exception by user/organization: 00537000000J406/00D37000000PmEp
Errorcaused by element : FlowRecordCreate.Create_Order
caused by: UPSERT --- UPSERT FAILED --- ERRORS : (INVALID_FIELD_FOR_INSERT_UPDATE) Order: bad field names on insert/update call: QuoteId, OpportunityId --- for SFDC record with ID : null,


An error occurred at element Create_Order.
UPSERT --- UPSERT FAILED --- ERRORS : (INVALID_FIELD_FOR_INSERT_UPDATE) Order: bad field names on insert/update call: QuoteId, OpportunityId --- for SFDC record with ID : null, .

This report lists the elements that the flow interview executed. The report is a beta feature.
We welcome your feedback on IdeaExchange
Flow DetailsFlow Name: Quote_to_Order
Type: AutoLaunchedFlow
Version: 1
Status: ACTIVE
 
Flow DetailsStart time(GMT): Tue Aug 04 19:57:02 GMT 2015
End time(GMT): Not Yet Finished
Duration: 0 seconds
 
Fast Lookup: Get_Quote_Fields 
Decision: OrderGenerated 
Fast Lookup: Get_Opportunity_Fields 
Fast Lookup: Get_Account_Fields 
Assignment: Assign_Order_Fields 
Fast Create: Create_Order