You need to sign in to do that
Don't have an account?
Denise Crosby
need help with trigger to verify only one primary contractor for an opportunity
Hello. I have a custom object contractor__c child object under opportunity. If a contractor__c is created, I need to make sure one and only one contractor__c is marked as primary for the opportunity. I tried an Apex trigger like this but it's not working. If I set the first contractor as primary, it will also set the second contractor as primary. And if I turn the primary flag off for the first contractor, it does not turn on the primary flag for the second contractor. Any help is appreciated.
trigger VerifyOnlyOnePrimaryContractor on Contractor__c (before insert, before update) { for(Contractor__c c: trigger.new){ string O = c.Opportunity__c; string thisContractor = c.ID; //if this contractor is marked primary, make all the others for the same opportunity not primary if(c.Primary__c){ List<contractor__c> cList = [Select id from Contractor__c where Opportunity__c = :O and id <> :thisContractor]; for(contractor__c con : cList){ con.Primary__c = FALSE; } update cList; } else{ //if no contractors are marked as primary, set one of them as primary string primary = 'no'; List<contractor__c> cList1 = [Select id,primary__c from Contractor__c where Opportunity__c = :O]; if(cList1.size() == 0){ c.Primary__c = TRUE; } else { for(contractor__c con1 : cList1){ if(con1.Primary__c){ primary = 'yes'; } } if(primary == 'no'){ c.Primary__c = TRUE; } } } } }
Please try below.
Please let me know, if you still face any issues.
Best Regards,
Dayakar.D
All Answers
This is really good and much better. Thank you! The only issue I'm facing now is it won't let me make any updates to the primary checkbox once there is a single contractor in place. I can't deselect the checkbox on one contractor and save. And I also can't select the checkbox on a different contractor to save. I'm not sure how to handle that. Any ideas?
Thanks again.
Please try below.
Please let me know, if you still face any issues.
Best Regards,
Dayakar.D