You need to sign in to do that
Don't have an account?
Samyra Chrisp 18
Apex trigger AllDealCounts caused an unexpected exception, contact your administrator: AllDealCounts: execution of AfterInsert caused by: System.StringException: Invalid id: 1 EDI Source - test: External entry point
I am trying to build a trigger to update a field on Account for a custom object that has a lookup relationship to the acccount (Investment__c).
The field on Account that I'm trying to update is DealCount__c Number (3, 0).
The field on Investment__c that I am using to get the count is Deal_Unique_ID__c
The ID for Investment__c is a text field.
Any help or advice is greatly appreciated....my experience writting triggers is almost non-existent...and I'm trying to learn.
Below is the error I'm receiving and the trigger:
//Error:Apex trigger AllDealCounts caused an unexpected exception, contact your administrator: AllDealCounts: execution of AfterUpdate caused by: System.StringException: Invalid id: PharmaMetrics (2017): External entry point
//Trigger
trigger AllDealCounts on Investment__c (after update,after insert)
{
List <ID> DealIDs = new List<Id> ();
List <Account> accounts = new List<Account>();
List <AggregateResult> requirements = New List<AggregateResult>();
for(Investment__c req:trigger.new)
{
DealIDs.add(req.Name);
}
Accounts = [Select ID, DealCount__c from Account where ID in :accounts];
Requirements = [Select Target_Company__C, Count(Deal_Unique_ID__c) from Investment__c where Target_Company__c in :accounts group by Target_Company__c];
For(AggregateResult ar: requirements)
{
for(Account a:accounts)
{
if (ar.get('Accounts') == a.ID)
{a.DealCount__c = Decimal.ValueOf(String.ValueOf(ar.get('expr0'))) ;}
}
}
}
The field on Account that I'm trying to update is DealCount__c Number (3, 0).
The field on Investment__c that I am using to get the count is Deal_Unique_ID__c
The ID for Investment__c is a text field.
Any help or advice is greatly appreciated....my experience writting triggers is almost non-existent...and I'm trying to learn.
Below is the error I'm receiving and the trigger:
//Error:Apex trigger AllDealCounts caused an unexpected exception, contact your administrator: AllDealCounts: execution of AfterUpdate caused by: System.StringException: Invalid id: PharmaMetrics (2017): External entry point
//Trigger
trigger AllDealCounts on Investment__c (after update,after insert)
{
List <ID> DealIDs = new List<Id> ();
List <Account> accounts = new List<Account>();
List <AggregateResult> requirements = New List<AggregateResult>();
for(Investment__c req:trigger.new)
{
DealIDs.add(req.Name);
}
Accounts = [Select ID, DealCount__c from Account where ID in :accounts];
Requirements = [Select Target_Company__C, Count(Deal_Unique_ID__c) from Investment__c where Target_Company__c in :accounts group by Target_Company__c];
For(AggregateResult ar: requirements)
{
for(Account a:accounts)
{
if (ar.get('Accounts') == a.ID)
{a.DealCount__c = Decimal.ValueOf(String.ValueOf(ar.get('expr0'))) ;}
}
}
}
Please change the following line in you code:
From To:
This would compare the value of Target_Compay__c with the account Id and should resovle the issue.
Thanks
Shashikant
When I look at the record - the Invalid ID is the "Deal Name" on the record - Syngy.
I got the trigger to run without causing an error to appear when updating an Investment recordby making a change an additional change to look at the ID rather than Investment__C.Name. (Changes in BOLD below.) But I'm still not getting the account record to update.
Trigger:
trigger AllDealCounts on Investment__c (after update,after insert)
{
List <ID> DealIDs = new List<ID> ();
List <Account> accounts = new List<Account>();
List <AggregateResult> requirements = New List<AggregateResult>();
for(Investment__c req:trigger.new)
{
DealIDs.add(req.ID);
}
Accounts = [Select ID, DealCount__c from Account where ID in :accounts];
Requirements = [Select Target_Company__C, ID, Count(Deal_Unique_ID__c) from Investment__c where Target_Company__c in :accounts group by Target_Company__c, ID];
For(AggregateResult ar: requirements)
{
for(Account a:accounts)
{
if (ar.get('Target_Company__c') == a.ID)
{a.DealCount__c = Decimal.ValueOf(String.ValueOf(ar.get('expr0'))) ;}
}
}
}
if you still face issue could you just check using debug logs on which Line you are getting this exception and please share that information.
I made the change you suggested.
When I run the code in the developer console I'm getting the error code
Line: 1, Column: 0
required (...)+ loop did not match anything at input 'trigger'.
Can you point me to something that can explain to me how to get this built out?
Thanks
Shashikant