You need to sign in to do that
Don't have an account?
Trigger Update (std) Parent from (custom) Child
We have created a custom many-to-many connection record between the standard Opportunity object the User object called 'business_consultant__c'. There is a checkbox on the connection record that shows records if it is the 'lead' consultant or not.
I am trying to write a trigger on create / save of 'business_consultant__c' that will check to see if the record is a lead consultant, and if so, update another lookup on the opportunity to the users called "Lead_Bus_Consultant__c".
The requirement here is to have the lead business consultant listed on the actual opportunity record.
I am trying to piece this together...
//Get Ids of lead = true business_consultant__c records that were updated for bulk (just grab the first 1 if there are multiple) Set<Id> opportunityIds - new Set<Id>(); for (Business_Consultant__c busCon : trigger.new){ opportunityIds.add(busCon.Opportunity__c); } //Check to see if it is the lead if(busCon.Lead_Consultant__c == TRUE){ //Get Ids of related Opportunities List<opportunity> relatedOpportunities = [SELECT Opportunity__c FROM Business_Consultant__c WHERE Id in: opportunityIds]; //Set the 'Lead_Bus_Consultant__c' on the Opportunity to the consultant__c field on the connection record. for(Opportunity opp : relatedOpportunities){ opp.Lead_Bus_Consultant__c = busCon.Consultant__c; }else{ //NOTHING }
Is there an easier way of accomplishing this? The field on the Opportunity does not have to be a lookup, it could be a text field with just a name, although user would provide additional functionality.
Any help is greatly appreciated!!
Hi
I think the code is wrong. TRY this
set<id> opportunitiesid = new set<id>();
for(Business_Consultant__c buscon: Trigger.New) {
opportunitiesid.add(buscon.Opportunity__c)
}
List<Opportunity> updatelst = new List<Opportunity>();
List<Opportunity> lstopp =[select id, Lead_bus_consultant__c from Opportunity where id IN:opportunitiesid];
for(Business_Consultant__c buscon:Trigger.New) {
if(buscon.Lead_Consultant__c!=null) {
for(Opportunity opp:lstopp) {
if(opp.id == buscon.opportunity__c) {
opp.Lead_bus_consultant__c = buscon.consultant__r.name;
updatelst.add(opp);
}
}
}
}
update updatelst;
Note:I used similar field names. Please use proper field names.
Hello,
This should ideally be done by trigger - so that part is fine.
However, in the trigger, you'll have to cover up all scenarios like
1. On insert of a new Business Consultant record on an Opportunity, if the Lead_Consultant field is checked, you update the Opportunity - this is done.
2. On update of a Business Consultant record, if I uncheck the Lead Consultant field, I again need to update the Opportunity - Not done.
3. On delete of a BC record, if I delete the record where Lead Consultant was checked, I have to update the Opportunity - not done.
Also, one question that arises here is - can more than one Business Consultant records for a given Opportunity have Lead Consultant checked? If yes, write your logic that way for update and delete cases.
Thanks!
I've gotten most of that covered with the following Trigger (needs to be cleaned up still)
However, I am running into a null point exception on line 4 for the isDeletes.
I thought that making it before delete would help... is the issue that if the record is deleted there is no reference to the opportunity__c field anymore?
EDIT:
I suppose the other option is to prevent users with a vRule from deleting connections where lead_bus_consultant__c == True... but I would rather do it in the trigger :)
I modified to code a bit to fix it... didn't realize that you cant use trigger.new for deletes
Code below seems to be working correctly.
Hi,
I am working on the test class now but still running into some problems that I can't seem to figure out...
I have this as a test class but it is failing the first system.assertEquals.
When I try this manually everything is working correctly, but it is not working in the testClass.
Any ideas would be greatly appreciated!
-Justin
Has anybody had this before, where the trigger works when tested manually, but in a test class it doesnt seem to work?
Meaning - you getting some error or the code coverage doesn't give expected results?