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
Travis16Travis16 

Unexpected Token : 'If"

I am not sure what is wrong with the Syntax but I believe I am missing something around the SOQL as I wan't getting this until I changed that. 

trigger OrderUpdateDrafter on Order__c (before insert) {
    String drafterEmail;
    for (Order__c o : Trigger.new){
        //Get all the Drafting records in a list where drafting is on the order
        List<Drafting> myDrafting = new List<Drafting> {
            myDrafting = [SELECT Id,
                          (SELECT Drafter_Email__c,Drafter__c, Order__c
                          FROM Drafting__r 
                          LIMIT 1)
                          FROM Order__c WHERE Id IN : Order__c]
                }
        //check if list is empty if not we will update the order with the Drafters email
        if(myDrafting() > 0 && o.Drafting_Email__c = null) {
            o.Drafter_Email__c == myDrafting.Drafter_Email__c;
        }
    }
}
Best Answer chosen by Travis16
SalesFORCE_enFORCErSalesFORCE_enFORCEr
It can but you have to create the process builder on child object. Fixed your code
trigger OrderUpdateDrafter on Order__c (before update) 
{
    for(Order__c o: Trigger.new){
        //check if Drafting is on the order
        if (o.Drafting_Kingspan__c = true){
            //get 1 drafting record as the assigned drafter should be the same across all drafting records for the order
                Drafting__c myDrafting = [SELECT Id,Drafter_Email__c,Drafter__c, Order__c
                             FROM Drafting__c
                              //make sure that my drafting records are from my trigger.new reocrd
                             WHERE Order__c = o.Id LIMIT 1];
            //check list to make sure values are there and if the order needs the drafter email updated 
            if(myDrafting!=null && o.Drafting_Email__c = null){
                //update the drafters email address if needed
                o.Drafter_Email__c == myDrafting.Drafter_Email__c;
            }
        }
    }
}

All Answers

Travis16Travis16
Amit thanks for the quick reply now it is telling me that it expecting a reight curly bracket, found ';'
Travis16Travis16
Would it be better if I moved the SOQL outside the For loop?
Amit Chaudhary 8Amit Chaudhary 8
Update your code like below
trigger OrderUpdateDrafter on Order__c (before insert) 
{
    String drafterEmail;
    for (Order__c o : Trigger.new)
	{
        List<Drafting> myDrafting = new List<Drafting> ();
		
        myDrafting = [SELECT Id,
                          (SELECT Drafter_Email__c,Drafter__c, Order__c
                          FROM Drafting__r 
                          LIMIT 1)
                          FROM Order__c WHERE Id IN : Order__c] ;
						  
        if(myDrafting() > 0 && o.Drafting_Email__c = null) 
		{
            o.Drafter_Email__c == myDrafting.Drafter_Email__c;
        }
    }
}

 
Travis16Travis16
Amit I made the following changes and now I am getting an 'Invalid bind expression type of Drafting__c for Id field of SObject Order__c' 

trigger OrderUpdateDrafter on Order__c (before insert) 
{
    String drafterEmail;
    for (Order__c o : Trigger.new)
    {
        List<Drafting__c> myDrafting = new List<Drafting__c> ();
        
        myDrafting = [SELECT Id,
                          (SELECT Drafter_Email__c,Drafter__c, Order__c
                          FROM Drafting__r 
                          LIMIT 1)
                          FROM Order__c WHERE Id IN : myDrafting] ;
                          
        if(myDrafting() > 0 && o.Drafting_Email__c = null) 
        {
            o.Drafter_Email__c == myDrafting.Drafter_Email__c;
        }
    }
}
SalesFORCE_enFORCErSalesFORCE_enFORCEr
I guess Drafting is the Child object and Order is the Master. Then, how are you creating records in Drafting object? I am asking because you are trying to update a field on insert of Master from the related Child record. The Child record will not be created in before insert of Master.
I think you should write the trigger on Drafting object and then populate the email on the parent object.
Travis16Travis16
You are correct Drafting is a child object of Order and Order is the master. This needs to be changed from before insert to before update. I believe that I am over complicating this as I am new to APEX and SOQL. I think the best way to approach this would be something like below but I can't figure out how to run my WHERE in my SOQL. Again I am not sure if below make sense to you but it does to me but I beleive that I have the wrong syntax. 

trigger OrderUpdateDrafter on Order__c (before update) 
{
    for(Order__c o: Trigger.new){
        //check if Drafting is on the order
        if (o.Drafting_Kingspan__c = true){
            //get 1 drafting record as the assigned drafter should be the same across all drafting records for the order
            List<Drafting__c> myDrafting = new List<Drafting> ();
                myDrafting = [SELECT Id,Drafter_Email__c,Drafter__c, Order__c
                             FROM Drafting__r
                              //make sure that my drafting records are from my trigger.new reocrd
                             WHERE Order__c = o.Id];
            //check list to make sure values are there and if the order needs the drafter email updated 
            if(myDrafting() > 0 && o.Drafting_Email__c = null){
                //update the drafters email address if needed
                o.Drafter_Email__c == myDrafting.Drafter_Email__c;
            }
        }
    }
}
SalesFORCE_enFORCErSalesFORCE_enFORCEr
I can update the code but why don't you use process builder to populate the email on Order. It will simple and you don't need to write a test class.
Travis16Travis16
Can Process builder pull information from child records where there could be more than one record? I didn't think it could that is why I went down the route of APEX. 
SalesFORCE_enFORCErSalesFORCE_enFORCEr
You can choose the Related Records option while adding Field Update in process builder and then add filter to choose your desired record. 
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Also, I am confused as why don;t you create a builder/trigger or something on the Drafting object and then populate the email on Order.
Travis16Travis16
I am new to APEX and not sure if that is possible, and I am not confident that these records are updated enough to carry the information over to the order. 0
SalesFORCE_enFORCErSalesFORCE_enFORCEr
As you are writing trigger on Order, what if someone changes the Drafter_Email__c on Drafting record or creates a new Drafting record then the related field on Order will not be updated as there is no trigger on Drafting to do that.
Sorry, if I am confusing you.
Travis16Travis16
Thansk for all the help, I didn't think that the parent record could be updated through process builder. I have used process builder to do this and just testing it. 
SalesFORCE_enFORCErSalesFORCE_enFORCEr
It can but you have to create the process builder on child object. Fixed your code
trigger OrderUpdateDrafter on Order__c (before update) 
{
    for(Order__c o: Trigger.new){
        //check if Drafting is on the order
        if (o.Drafting_Kingspan__c = true){
            //get 1 drafting record as the assigned drafter should be the same across all drafting records for the order
                Drafting__c myDrafting = [SELECT Id,Drafter_Email__c,Drafter__c, Order__c
                             FROM Drafting__c
                              //make sure that my drafting records are from my trigger.new reocrd
                             WHERE Order__c = o.Id LIMIT 1];
            //check list to make sure values are there and if the order needs the drafter email updated 
            if(myDrafting!=null && o.Drafting_Email__c = null){
                //update the drafters email address if needed
                o.Drafter_Email__c == myDrafting.Drafter_Email__c;
            }
        }
    }
}
This was selected as the best answer