You need to sign in to do that
Don't have an account?
mgodsey
Trigger error: System.StringException: Invalid id. (updating custom object field from Opp picklist)
I have two objects: Opportunity and ProposalFlight__c. They are in a master-detail relationship (ProposalFlight__c being the child). I also have a picklist field on both called ApprovalStatus__c. After an update on certain types of Opportunities, I want the ApprovalStatus__c picklist on ProposalFlight__c to change to match that on Opportunity. I wrote the following trigger and it saves, but when it tries to execute if gives me this error message:
Validation Errors While Saving Record(s) |
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger UpdatePFtoPending caused an unexpected exception, contact your administrator: UpdatePFtoPending: execution of AfterUpdate caused by: System.StringException: Invalid id: Approved: External entry point". |
Can anyone please explain to me what this error message means and any tips for how I can fix it? Thank you so much!
trigger UpdatePFtoPending on Opportunity (after update) { //List of approved Master Terms Opportunities List<ID> masterOpps = new List<ID> (); for (Opportunity opp : trigger.new){ if (opp.VersionType__c=='Change Request'){ masterOpps.add(opp.ApprovedPARENTOpportunity__c);} } //List of Related Not Submitted Proposal Flights List<ProposalFlight__c> pflights = [SELECT ID, ApprovalStatus__c, Opportunity__c FROM ProposalFlight__c WHERE Opportunity__c in:masterOpps AND ApprovalStatus__c !='Approved']; Map<id,ProposalFlight__c> mapofPFs = New Map<id, ProposalFlight__c>(); //Build map of Proposal Flights from List of IDs in trigger for(ProposalFlight__c pf : pflights) mapofPFs.put(pf.id,pf); //for each record in this trigger for(Opportunity thiscropp :trigger.new){ //update approval status on Proposal Flight ProposalFlight__c thisPF = mapofPFs.get(thiscropp.ApprovalStatus__c); If(thiscropp.VersionType__c == 'Change Request'){ thisPF.ApprovalStatus__c = thiscropp.ApprovalStatus__c; }} Update mapofPFs.values(); }
ok. this trigger will update the child proposal flight record with the corresponding value in its parent opportunity record's approval status field, when the opp is updated. the trigger will only fire on proposal flight records not in an approved status already, and the opp has to have versiontype = change request.
i'm still not sure where this parent opp thing fits in but i have a feeling this trigger will only need to be slightly tweaked for that. essentially though this is a simple trigger to update a child record when a parent is edited, so adding in logic for a parent opp should be easy. try this trigger to see how it works and let me know how you want to change it.
to test: 1. create a test opp with version type = change request. save. 2. create a proposal flight record on that opp and just leave approvalstatus blank. 3.edit the opp so that approval status = approved. 4. check the proposal flight record and see that the status matches that of the opp
All Answers
turn off validation rules on those objects and then try the test again. i think the trigger might be in conflict with an active validation rule
Hi Ross - thanks for your quick response. I don't think a validation rule is the issue though, because we don't have any yet on ProposalFlight__c (which is what is being updated in the trigger) and the debug log doesn't say anything about a validation rule error:
Any other ideas of what is happening? In what situations does the system.stringexception: invalid id error message usually come up?
what type of field is
ApprovedPARENTOpportunity__c
It's a lookup field to Opportunity.
We have two different types of Opportunities - "Master Terms" and "Change Requests". The Change Request Opportunities are related to the Master Terms Opportunities through the lookup field ApprovedPARENTOpportunity__c.
ok. this trigger will update the child proposal flight record with the corresponding value in its parent opportunity record's approval status field, when the opp is updated. the trigger will only fire on proposal flight records not in an approved status already, and the opp has to have versiontype = change request.
i'm still not sure where this parent opp thing fits in but i have a feeling this trigger will only need to be slightly tweaked for that. essentially though this is a simple trigger to update a child record when a parent is edited, so adding in logic for a parent opp should be easy. try this trigger to see how it works and let me know how you want to change it.
to test: 1. create a test opp with version type = change request. save. 2. create a proposal flight record on that opp and just leave approvalstatus blank. 3.edit the opp so that approval status = approved. 4. check the proposal flight record and see that the status matches that of the opp
Thank you so much for your help on this! I really appreciate you taking the write out this trigger. I didn't realize you could put in those relationship fields in a SOQL query - I know I'll be using this as a reference quite often!
I'm sorry that I can't explain the parent/child opp thing better (the parent being the "Master Terms" and the child being the "Change Request). It's tricky to explain over the internet like this! But basically, the ProposalFlight__c will always be related to the Master Terms opp. However, it needs to be updated differently in the following scenarios:
1) "Master Terms" Proposal Flight - needs to be updated with the "Master Terms" Opportunity's Approval Status
2) "Change Request" Proposal Flight - needs to be updated with "Change Request" Opportunity's Approval Status
Your trigger is great for scenario 1, but I wasn't sure how to fit scenario 2 into it. However, I have a trigger that I developed last night that seems to be working. I combined the two below. They work when testing in the UI, but I'm very new to APEX and I worry that they are poorly compiled and possibly breaking best practices. Any glaring issues you see that I should be aware of? Thanks again!
sure i'll take a look. would you mind marking my last response as resolution to this post? thanks a lot