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
Adaniels117Adaniels117 

Help with trigger code

Hey guys, long time listener...first time caller.

 

Just beginning at Apex, need some help writing this trigger. I want a custom date field to update on the Account level whenever a Task within the Account is updated to Status=Sold.

 

The error i'm getting is "Compile Error: expecting a semi-colon, found 'Account' at line 8 column 6"

 

trigger HasSoldTask on Task (after insert) {
for (Task t: Trigger.new){
    if (t.accountID != NULL){
      acctIDs.add(t.accountID);
//Create a map to match the task related to ID's with their corresponding account ID's
      Map<ID, Account> acctMap = new Map<ID, Account> ([Select ID, Has_Sold_Opp__c from Account where ID in :acctIDs])
//Create the account object
      Account acctRec = acctMap.get(t.accountID);

//If the account ID isn't null, the subject line starts with "sw", and the task has been marked as completed
//Check to see if the Last_SW_Activity__c field is current compared with the latest completed activity    
    If ((t.Status == 'Sold') && (acctMap.get(t.accountID).Has_Sold_Opp__c < t.LastModifiedDate || acctMap.get(t.accountID).Has_Sold_Opp__c ==null)){
//Update the Last_SW_Activity__c field on the account object with the task's end date  
          acctrec.Has_Sold_Opp__c  = t.LastModifiedDate;
   
      }
  }
  update acctRec;
  }

 If I add a semi colon before " Account acctRec = acctMap.get(t.accountID);" Then I get "Compile Error: Variable does not exist: acctIDs at line 6 column 110"

 

Any ideas? Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
SurpriseSurprise

Below given code should work.

 

You have to change datatype of  Hasoptfield because the value you are assigning to this field has datatype of datetime,therefore u have to ensure that HasOptField has datatype of  Datetime

 

 

trigger Work on Task(after insert)
{
List<Id> accIds = new List<Id>();
   for(Task t : Trigger.new)
    {
        If (t.status=='sold')
        {
            accids.add(t.accountid);
        }
    }
   Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Has_Sold_Opp__c FROM Account WHERE Id IN :accIds]);
    
    Map<Id, Account> updateMap = new Map<Id, Account>();
    
    for (Task t: Trigger.new)
    {
        If (t.Accountid <> null && (t.Status == 'sold') && (accMap.get(t.AccountId).Has_Sold_Opp__c < t.LastModifiedDate || accMap.get(t.AccountId).Has_Sold_Opp__c ==null))
        {
            Account acc = accMap.get(t.AccountId);
            acc.Has_Sold_Opp__c  = t.LastModifiedDate;
            updateMap.put(t.AccountId, acc);
        }
    }
    update updateMap.values();
    }

All Answers

empucempuc

You've missed a semicollon in the end of this line:

 

Map<ID, Account> acctMap = new Map<ID, Account> ([Select ID, Has_Sold_Opp__c from Account where ID in :acctIDs]) ;

 

 

Adaniels117Adaniels117

Thanks empuc, I tried this out and ran into the another error. Now i'm getting "Compile Error: Variable does not exist: acctIDs at line 6 column 110"

I read in a different thread that it might have something to do with the semi colon placement. Any ideas?

 

trigger HasSoldTask on Task (after insert) {
for (Task t: Trigger.new){
    if (t.accountID != NULL){
      acctIDs.add(t.accountID);
//Create a map to match the task related to ID's with their corresponding account ID's
      Map<ID, Account> acctMap = new Map<ID, Account> ([Select ID, Has_Sold_Opp__c from Account where ID in :acctIDs]);
//Create the account object
      Account acctRec = acctMap.get(t.accountID);

//If the account ID isn't null, the subject line starts with "sw", and the task has been marked as completed
//Check to see if the Last_SW_Activity__c field is current compared with the latest completed activity    
    If ((t.Status == 'Sold') && (acctMap.get(t.accountID).Has_Sold_Opp__c < t.LastModifiedDate || acctMap.get(t.accountID).Has_Sold_Opp__c ==null)){
//Update the Last_SW_Activity__c field on the account object with the task's end date  
          acctrec.Has_Sold_Opp__c  = t.LastModifiedDate;
   
      }
  }}
  update acctRec;
  }

 

Naidu PothiniNaidu Pothini
trigger HasSoldTask on Task (after insert)
{
    List<Id> accIds = new List<Id>();
	
    for(Trigger t : Trigger.new)
    {
	accIds.add(t.AccountId);
    }
	
    Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Has_Sold_Opp__c FROM Account WHERE Id IN :acctIds]);
	
    for (Task t: Trigger.new)
    {
	if (t.accountID != NULL)
	{
  	    If ((t.Status == 'Sold') && (acctMap.get(t.accountID).Has_Sold_Opp__c < t.LastModifiedDate || acctMap.get(t.accountID).Has_Sold_Opp__c ==null))
	    {
		Account acc = accMap.get(t.AccountId);
			
		acc.Has_Sold_Opp__c  = t.LastModifiedDate;
				
		accMap.put(t.AccountId, acc);
	    }
	}
    }
    update accMap.values();
}

 try this.

Adaniels117Adaniels117

Hey Naidu,

 

I tried this code out out, and now i'm getting this error " Compile Error: unexpected token: 'Trigger' at line 5 column 8"

Do you know what might be causing this?

SurpriseSurprise

By mistake naidu has put keyword trigger ,replace it with Task.

SurpriseSurprise
for (Task t: Trigger.new){ if (t.accountID != NULL){ acctIDs.add(t.accountID);


In the above line whr are u adding all inserted accountids to the list,why don't you have



if t.status=='completed')
{
acctids.add(t.accountids);
}

This will only add record id's in which status is completed.As of now you are adding all id's whether are status is completed or somewthing else




Naidu PothiniNaidu Pothini
trigger HasSoldTask on Task (after insert)
{
    List<Id> accIds = new List<Id>();
	
    for(Trigger t : Trigger.new)
    {
		If (t.Account <> null)
		{
			accIds.add(t.AccountId);
		}
    }
	
    Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Has_Sold_Opp__c FROM Account WHERE Id IN :acctIds]);
	
	Map<Id, Account> updateMap = new Map<Id, Account>();
	
    for (Task t: Trigger.new)
    {
		If (t.Account <> null && (t.Status == 'Sold') && (acctMap.get(t.AccountId).Has_Sold_Opp__c < t.LastModifiedDate || acctMap.get(t.AccountId).Has_Sold_Opp__c ==null))
		{
			Account acc = accMap.get(t.AccountId);
			acc.Has_Sold_Opp__c  = t.LastModifiedDate;
			updateMap.put(t.AccountId, acc);
		}
    }
    update updateMap.values();
}

 

 Try this... 

Adaniels117Adaniels117

Tried two variations. The one from above, with  for(Task t : Trigger.new) and one with the variation suggested above by Surprise (below)

 

 

trigger HasSoldTask on Task (after insert)
{
    List<Id> accIds = new List<Id>();
    
    for(Task t : Trigger.new)
    {
        If (t.status=='Sold')
{
acctids.add(t.accountids);
}
    }
    
    Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Has_Sold_Opp__c FROM Account WHERE Id IN :acctIds]);
    
    Map<Id, Account> updateMap = new Map<Id, Account>();
    
    for (Task t: Trigger.new)
    {
        If (t.Account <> null && (t.Status == 'Sold') && (acctMap.get(t.AccountId).Has_Sold_Opp__c < t.LastModifiedDate || acctMap.get(t.AccountId).Has_Sold_Opp__c ==null))
        {
            Account acc = accMap.get(t.AccountId);
            acc.Has_Sold_Opp__c  = t.LastModifiedDate;
            updateMap.put(t.AccountId, acc);
        }
    }
    update updateMap.values();
}

 Now i'm getting "Compile Error: Variable does not exist: acctIds at line 13 column 106"

 

SurpriseSurprise
List<Id> accIds = new List<Id>();
    
Check your varible name above.You have mentioned accids as list and then elsewhere you have mentioned wrong variable name checlk below.Correct this everywhere in the code
acctids.add(t.accountids);
SurpriseSurprise

Below given code should work.

 

You have to change datatype of  Hasoptfield because the value you are assigning to this field has datatype of datetime,therefore u have to ensure that HasOptField has datatype of  Datetime

 

 

trigger Work on Task(after insert)
{
List<Id> accIds = new List<Id>();
   for(Task t : Trigger.new)
    {
        If (t.status=='sold')
        {
            accids.add(t.accountid);
        }
    }
   Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Has_Sold_Opp__c FROM Account WHERE Id IN :accIds]);
    
    Map<Id, Account> updateMap = new Map<Id, Account>();
    
    for (Task t: Trigger.new)
    {
        If (t.Accountid <> null && (t.Status == 'sold') && (accMap.get(t.AccountId).Has_Sold_Opp__c < t.LastModifiedDate || accMap.get(t.AccountId).Has_Sold_Opp__c ==null))
        {
            Account acc = accMap.get(t.AccountId);
            acc.Has_Sold_Opp__c  = t.LastModifiedDate;
            updateMap.put(t.AccountId, acc);
        }
    }
    update updateMap.values();
    }

This was selected as the best answer
Adaniels117Adaniels117

This works! It works!

 

Thank you all so much for all the help!