You need to sign in to do that
Don't have an account?
Patrick Foy
First trigger need help getting parent record id for custom object
I'm just learning APEX and I would appreciate it if someone would look this over. It is supposed to create a new work order line(child) once a button is clicked and updates a check box to = true on the parent. The work order line needs to have Product Code( formula uses product id based on current user, i hope), Status,Parent W/O ID, Start Time (Date and Time). Once I get this figured out I think I can easily program the trigger for the "check out" button.
For( OptimaPro__Work_Order_Line__c newWorkOrderLine Trigger.new){
if (myVariable_current.Check_In__c = true) {
RecordTypeId = ‘01246000000Q2t9A’;
OptimaPro__MOBSVC_Start_Date_and_Time__c = datetime.now();
OptimaPro__MOBSVC_work_Order__c = // how do I get current parent record?//
OptimaPro__MOBSVC_Line_Status__c = ‘Open’; //pick list//
//next section picks product code.id based on current user//
IF($User.FirstName = "Cory"){ OptimaPro__MOBSVC_Product__c = '01t46000000nQVKAA2',
IF($User.FirstName = "Jim",){' OptimaPro__MOBSVC_Product__c = 01t46000000nRJFAA2';}
IF($User.FirstName = "Zach") {OptimaPro__MOBSVC_Product__c = '01t46000000nPA2AAM';}
Else [OptimaPro__MOBSVC_Product__c = '01t46000000nPO0AAM';}
}
}
For( OptimaPro__Work_Order_Line__c newWorkOrderLine Trigger.new){
if (myVariable_current.Check_In__c = true) {
RecordTypeId = ‘01246000000Q2t9A’;
OptimaPro__MOBSVC_Start_Date_and_Time__c = datetime.now();
OptimaPro__MOBSVC_work_Order__c = // how do I get current parent record?//
OptimaPro__MOBSVC_Line_Status__c = ‘Open’; //pick list//
//next section picks product code.id based on current user//
IF($User.FirstName = "Cory"){ OptimaPro__MOBSVC_Product__c = '01t46000000nQVKAA2',
IF($User.FirstName = "Jim",){' OptimaPro__MOBSVC_Product__c = 01t46000000nRJFAA2';}
IF($User.FirstName = "Zach") {OptimaPro__MOBSVC_Product__c = '01t46000000nPA2AAM';}
Else [OptimaPro__MOBSVC_Product__c = '01t46000000nPO0AAM';}
}
}
Avishek Nanda thank you for atleast trying to help me.
All Answers
First, you must understand how a trigger is invoked. A trigger gets called automatically when there is a DML operation on a record.
You can't call a trigger using a custom button.
What you can do in your case is first create an apex class where you put all your custom logic. Then create a custom button of type :'Execute Javascript on click'. And from that javascript code, call your apex class.
This way it was a little more versitile for any changes in Salesforce and it also offers a fail safe that if the user accidentaly clicks it they can uncheck the box in action layout and essentially it should do nothing.
Your code is not following the best practices for writing apex. I would suggest you to get an idea of how to write apex using best practices.
Avishek Nanda thank you for atleast trying to help me.
Hi Patrick,
My apologies if it came out in the wrong sense.
Let's start over.
Few things I need to know from you first:
-How are you updating the checkbox field value using a button(are you using Javascript to set the checkbox to true?)?
-Where is that button, on the parent record layout or on the child record layout?