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
MasieMasie 

Apex trigger to create a new record

Hi, I am a complete novice on trigger creation so please hep. 

I need help with the following trigger- it is supposed to create a new record in my custom objectt Billable Swapped Assets should an asset's Custom Field called Movement Status be changed to Swapped. 

I am getting an unexpected token error on the underlined line for the "For"

trigger create on Asset (after update) {

List <Billable_Swapped_Asset__c> auditTrailList = new List <Billable_Swapped_Asset__c>

// or whatever your custom object name put instead of Vehicle__c

for (Asset ass : Trigger.new) {
 
 
  // here is where you check if opportunity that is being inserted meets the criteria
  if (ass.Movement_Status__c = 'Swapped') { 


                           //instantiate the object to put values for future record 
  Billable_Swapped_Asset__c  b=  new Billable_Swapped_Asset__c ();
        
  // now map opportunity fields to new vehicle object that is being created with this opportunity
 
 
                            ass.Product2= b.Product__c;
                            ass.InstallDate= b.Install_Date__c;
                      ass.UsageEndDate= b.End_Date__c;
 
  //once done, you need to add this new object to the list that would be later inserted.
  //don't worry about the details for now
 
  auditTrailList.add(b);
 
 
  }//end if
 
}//end for ass

//once loop is done, you need to insert new records in SF
// dml operations might cause an error, so you need to catch it with try/catch block.
try {
  insert auditTrailList;
} catch (system.Dmlexception e) {
  system.debug (e);
}

}
Ramu_SFDCRamu_SFDC
The problem is with these lines

ass.Product2= b.Product__c;
                            ass.InstallDate= b.Install_Date__c;
                      ass.UsageEndDate= b.End_Date__c;


Change these to 

                          b.Product__c=ass.Product2;
                          b.Install_Date__c=  ass.InstallDate;
                      b.End_Date__c=ass.UsageEndDate;
MasieMasie
Hi Ramu

I changed them and I am still getting the same error- it refuses to save. 
Ramu_SFDCRamu_SFDC
Hi Masie,

I just noticed a silly mistake. The problem is in the second line List <Billable_Swapped_Asset__c> auditTrailList = new List <Billable_Swapped_Asset__c>

Please change this line to 

List <Billable_Swapped_Asset__c> auditTrailList = new List <Billable_Swapped_Asset__c>();

Mark this as the solution if this resolved your issue.