You need to sign in to do that
Don't have an account?
Ben Merton 15
Building my first trigger
I am trying to write an Apex class that clones a record from a related record. My app structure is a little complex:
Work_Order__c (Parent of Material Indent)
Custom Fields: Quantity__c, Delivery Date__c
Custom Lookup: Product__c
Material_Indent__c (Child of Work Order Object)
Custom Fields: Quantity__c
Custom Lookup: GSI_No__c, Work_Order__c
Product__C Object (Parent of BOM Line Item Object)
Custom Fields: Irrelevant
BOM_Items (Child of Parent Product Object)
Custom Fields: Quantity__c
Custom Lookup: GSI_No__c
I am tryig to write a trigger that automates the process of creating an Indent from the BOM Items (which are related to the Product, which is in turn in the lookup in the Work Order).
The trigger will be initiated from the Work Order page with a button ("Create Indent").
This is where I am so far. I am confused about whether I am even heading in the right direction? I have doubts about the following:
1. Is a trigger even a way to go for this?
2. Will Page Reference return the Products__c custom field ID if I am running this via a button on the Work Order (Products__c is a Custom Field on Work_Order__c
3. Is the method for creating a set of the BOM Items which have a Products__c=the Products__c found from the Page Reference on the Work Order correct?
4. Am I correctly traversing the objects using BOMItemSet.add(BOMItems.GSI_No__c.id) to add the ID of the GSI number to the list?
After this, I was going to create another trigger to insert the set that I have created above into Material_Indents, the child record of the Product object. However, I don't want to go on with this unless I am clear with the first step!
Please help!
Work_Order__c (Parent of Material Indent)
Custom Fields: Quantity__c, Delivery Date__c
Custom Lookup: Product__c
Material_Indent__c (Child of Work Order Object)
Custom Fields: Quantity__c
Custom Lookup: GSI_No__c, Work_Order__c
Product__C Object (Parent of BOM Line Item Object)
Custom Fields: Irrelevant
BOM_Items (Child of Parent Product Object)
Custom Fields: Quantity__c
Custom Lookup: GSI_No__c
I am tryig to write a trigger that automates the process of creating an Indent from the BOM Items (which are related to the Product, which is in turn in the lookup in the Work Order).
The trigger will be initiated from the Work Order page with a button ("Create Indent").
This is where I am so far. I am confused about whether I am even heading in the right direction? I have doubts about the following:
1. Is a trigger even a way to go for this?
2. Will Page Reference return the Products__c custom field ID if I am running this via a button on the Work Order (Products__c is a Custom Field on Work_Order__c
3. Is the method for creating a set of the BOM Items which have a Products__c=the Products__c found from the Page Reference on the Work Order correct?
4. Am I correctly traversing the objects using BOMItemSet.add(BOMItems.GSI_No__c.id) to add the ID of the GSI number to the list?
trigger Indent on Work_Order__c (before update) { // Create variable for capturing Product ID Page Reference productid=Product__c.id; // Create a set of all records Set<String> BOMItemSet = new Set<String>(); for (BOM_Items__c BOMItems : Trigger.new) { // Add the fields for Quantity and GSI No. if (BOMItems.Product__c = productid) { BOMItemSet.add(BOMItems.Quantity__c); BOMItemSet.add(BOMItems.GSI_No__c.id); //NB This is a lookup }
After this, I was going to create another trigger to insert the set that I have created above into Material_Indents, the child record of the Product object. However, I don't want to go on with this unless I am clear with the first step!
Please help!
Here is how you can do it with a class and a visualforce page.
Class: CreateIndentController Visualforce page: CreateIndentPage (which uses the above class). Edit the button you created and select the behaviour as "Display in existing window without sidebar or header" and the content source as "URL" and in the text area specify the url as below.
/apex/CreateIndentPage?id={!Work_Order_c.Id}&productId={!Work_Order__c.Product__c}
In the class I have set some validations, when the product is blank on Work Order and when there are no BOM Items for that product, which will display the error message on the page. Then the user need to add the product or BOM items accordingly and then come back to work order and click the button again, which will then create the Material indents.
Hope this is helpful.
All Answers
Change the field names accordingly.
Hope this helps.
Yes you can go with trigger.
I think your should be like this,
and your handler class should be like this,
At first you have to write & save the IntendTriggerHandler class and then you should write the apex trigger.
apart from trigger you may try with process builder also.
Thanks
Prosenjit
Suppose I want to convert your handler class to a regular class called by a button/visualforcepage/javascript, would i need to change anything?
Ben
Further to this, I THINK your code may have misunderstood the structure
I am looking to populate the Indent FROM the BOM Item. I think that your code goes the other way round (ie creating a BOM Item FROM the Indent). If this is correct, please like this post, as I am feeling devoid of likes as a newbie and I need all the electronic support I can get...
Ben
If you want to automatically create the indent records when a work order is saved, then you can go for the trigger. Then you don't need to click a button. When the work order is saved, the trigger will automatically create the indent records. Here is how you can build the trigger. Change or adjust the fields according to your requirement. Hope this helps.
Many thanks. I am definitely not looking for it to run on save. Only when the button is pressed. The reason for this is that they may want to create a Work Order independently of issuing the material (for example if the BOM in the product isn't prepared at that point, the production team will still need to start other activities).
Ben
Here is how you can do it with a class and a visualforce page.
Class: CreateIndentController Visualforce page: CreateIndentPage (which uses the above class). Edit the button you created and select the behaviour as "Display in existing window without sidebar or header" and the content source as "URL" and in the text area specify the url as below.
/apex/CreateIndentPage?id={!Work_Order_c.Id}&productId={!Work_Order__c.Product__c}
In the class I have set some validations, when the product is blank on Work Order and when there are no BOM Items for that product, which will display the error message on the page. Then the user need to add the product or BOM items accordingly and then come back to work order and click the button again, which will then create the Material indents.
Hope this is helpful.
/apex/CreateIndentPage?id={!abc__Work_Order_c.Id}&productId={!abc__Work_Order__c.Product__c}
I ran the debug log on it and it is returning the Work Order ID fine, but the Product ID is getting returned as the Name and not the ID.
I think it is definitely something wrong with this...
workOrderId = ApexPages.currentPage().getParameters().get('id'); //THIS IS RETURNING 1a0M1500000UGHdo
productId = ApexPages.currentPage().getParameters().get('productId'); //THIS IS RETURNING PRO-0001
Could there be some sort of conflict between the strings declared at the top and the parameters used in the Javascript?
Even if I bypass this, the code is not inserting any records for the same reason.
I have written code for creating BOM items from Intend. Yes you can use this apex code to attach with a visualforce. But for calling this from a javascript button you need to change the class a little bit. An apex class should be gobal and methods should be as Webservice.
please follow this link for that : http://prosenjit-sarkar-sfdc.blogspot.in/2015/01/how-to-call-apex-method-through-custom.html (http://prosenjit-sarkar-sfdc.blogspot.in/2015/01/how-to-call-apex-method-through-custom.html)
Thanks
Prosenjit.