function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Patrick FoyPatrick 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';}

             }
}


               
 
Best Answer chosen by Patrick Foy
Patrick FoyPatrick Foy
Thanks for validating once again why I don't like this Q&A section. 90% of the time it is 0 help all you get is people telling you oh your doing it wrong but offer 0 information on how to do it right. This is really discouraging and a big issue with most of Salesforce. The people who know are pretentious and don't want to help the people who are learning. 

Avishek Nanda thank you for atleast trying to help me. 

All Answers

Patrick FoyPatrick Foy
Please let me know if I interpreted your reply message correctly
 
CreateLaborWOLine on OptimaPro__Work_Order __c (after insert){

  for (OptimaPro__Work_Order __c newWorkOrder: Trigger.new) {

      if (myCheckBox.Check_In__c = true) {

		OptimaPro__Work_Order_Line__c   newWorkOrderLine = new OptimaPro__Work_Order _Line__c (

		RecordTypeId = '01246000000Q2t9A',

		OptimaPro__MOBSVC_work_Order__c= OptimaPro__Work_Order_C.id,

		OptimaPro__MOBSVC_Start_Date_and_Time__c = datetime.now(),

		OptimaPro__MOBSVC_Line_Status__c = ‘Open’, 

		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';});

			list.add(OptimaPro__Work_Order_Line__c);
		}
	}

	insert list.add;
}


 
Patrick FoyPatrick Foy
triedd to clean up errors
 
CreateLaborWOLine on OptimaPro__Work_Order __c (after insert){

  for (OptimaPro__Work_Order __c newWorkOrder: Trigger.new) {

      if (myCheckBox.Check_In__c = true) {

		OptimaPro__Work_Order_Line__c   newWorkOrderLine = new OptimaPro__Work_Order_Line__c (

		RecordTypeId = '01246000000Q2t9A',

		OptimaPro__MOBSVC_work_Order__c= OptimaPro__Work_Order_C.id,

		OptimaPro__MOBSVC_Start_Date_and_Time__c = datetime.now(),

		OptimaPro__MOBSVC_Line_Status__c = 'Open', 

		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';}

		);

		list.add(OptimaPro__Work_Order_Line__c);
		}
	}

	insert list.add;
}

 
Patrick FoyPatrick Foy
I think this is the coding I will need for check out.
 
CloseLaborWOLine on OptimaPro__Work_Order __c (after update){

  for (OptimaPro__Work_Order __c UpdateWorkOrder: Trigger.new) {

	List OptimaPro__Work_Order_Line__c  children = [ SELECT Id, ParentIdField, OptimaPro__MOBSVC_End_Date_and_Time__c, OptimaPro__MOBSVC_Line_Status__c from 
	OptimaPro__Work_Order_Line__c  where ParentIdField = :parent.Id];

	List OptimaPro__Work_Order_Line__c childrenToUpdate = new List OptimaPro__Work_Order_Line__c();

  for(OptimaPro__Work_Order_Line__c thischild: children )

{
 	if( thischild.OptimaPro__MOBSVC_End_Date_and_Time__c = null)
   	  {
   	  thischild.OptimaPro__MOBSVC_End_Date_and_Time__c = datetime.now();
  
  	  childrenToUpdate.add(thischild)
   	  }
 	if( thischild.OptimaPro__MOBSVC_Line_Status__c = null)
   	  {
   	  thischild.OptimaPro__MOBSVC_Line_Status__c = 'Completed';

   	  childrenToUpdate.add(thischild)
	  }
}

	if( !childrenToUpdate.isEmpty)
	{
   	update childrenToUpdate;
	}


}

 
Narender Singh(Nads)Narender Singh(Nads)
Hi Patrick,
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.
Patrick FoyPatrick Foy
Yeah, I have two action buttons that are just solely marking Check Boxs, not on the layout, complete. Then the trigger should start when those CB = True.

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. 
Patrick FoyPatrick Foy
Just realized I for got to put my if statement in the closing one validating "Check Out" CB = True.
Narender Singh(Nads)Narender Singh(Nads)
Hi patrick,
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.
Patrick FoyPatrick Foy
Thanks for validating once again why I don't like this Q&A section. 90% of the time it is 0 help all you get is people telling you oh your doing it wrong but offer 0 information on how to do it right. This is really discouraging and a big issue with most of Salesforce. The people who know are pretentious and don't want to help the people who are learning. 

Avishek Nanda thank you for atleast trying to help me. 
This was selected as the best answer
Narender Singh(Nads)Narender Singh(Nads)

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?