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
Steve101Steve101 

Trigger to update Task custom field

I have a Workflow rule that creates a Task each time a specific type of Lead is created.  I have a custom field in this Task that needs to be populated upon creation but I cannot accomplish this with the Workflow so I am creating a trigger. 

 

I get the following error when I save the trigger

 

Error: Compile Error: unexpected token: '}' at line 5 column 12

 

Here is the trigger

 

trigger PubProductUpdate on Task (before insert){

    for (Task PU : Trigger.new){
        if (PU.RecordTypeId = '012U0000000Z6qBIAS' && PU.Subject = 'Pitch Networks' && PU.Pub_Products__c = null)
            }
    {
    
    PU.Pub_Products__c = 'Pub Opening Pitch';
    }
}

 

I am new to Apex Triggers and not sure where my select criteria goes wrong. 

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
Andrew WilkinsonAndrew Wilkinson

= is used for assignment. == is used for comparison.

 

 if(PU.RecordTypeId = '012U0000000Z6qBIAS' && PU.Subject = 'Pitch Networks' && PU.Pub_Products__c = null)

 

needs to be...

 if(PU.RecordTypeId == '012U0000000Z6qBIAS' && PU.Subject == 'Pitch Networks' && PU.Pub_Products__c == null)

All Answers

DaveT.ax1572DaveT.ax1572

the "}" on line 5 is closing out your if statement.  try this

 

trigger PubProductUpdate on Task (before insert){

    for (Task PU : Trigger.new){
        if(PU.RecordTypeId = '012U0000000Z6qBIAS' && PU.Subject = 'Pitch Networks' && PU.Pub_Products__c = null)
            
    {
    
    PU.Pub_Products__c = 'Pub Opening Pitch';
    }
}
}

Steve101Steve101

Dave T,

 

Thanks for the quick response.  I made the change and I now get the following error.

 

Error: Compile Error: AND operator can only be applied to Boolean expressions at line 4 column 54

 

Steve

Andrew WilkinsonAndrew Wilkinson

= is used for assignment. == is used for comparison.

 

 if(PU.RecordTypeId = '012U0000000Z6qBIAS' && PU.Subject = 'Pitch Networks' && PU.Pub_Products__c = null)

 

needs to be...

 if(PU.RecordTypeId == '012U0000000Z6qBIAS' && PU.Subject == 'Pitch Networks' && PU.Pub_Products__c == null)

This was selected as the best answer
Steve101Steve101

Andrew,

 

That did it for me.  Thanks for your help.

 

Steve