You need to sign in to do that
Don't have an account?
Adaniels117
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!
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
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]) ;
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?
try this.
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?
By mistake naidu has put keyword trigger ,replace it with Task.
In the above line whr are u adding all inserted accountids to the list,why don't you have
Try this...
Tried two variations. The one from above, with for(Task t : Trigger.new) and one with the variation suggested above by Surprise (below)
Now i'm getting "Compile Error: Variable does not exist: acctIds at line 13 column 106"
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 works! It works!
Thank you all so much for all the help!