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
Samyra Chrisp 18Samyra 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'))) ;}
}
}
}
Shashikant SharmaShashikant Sharma
Hi Samyra,

Please change the following line in you code:

From
if (ar.get('Accounts') == a.ID)
To:
if (ar.get('Target_Compay__c') == a.ID)

This would compare the value of Target_Compay__c with the account Id and should resovle the issue.

Thanks
Shashikant
Samyra Chrisp 18Samyra Chrisp 18
Thank you for your suggestion.  That makes sense because Target_Company__c is the lookup field that links Investment__c to Accounts.  I made the replacement and I'm still getting an error:  Error:Apex trigger AllDealCounts caused an unexpected exception, contact your administrator: AllDealCounts: execution of AfterUpdate caused by: System.StringException: Invalid id: Synygy: External entry point

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'))) ;}
}
}
}
 
Shashikant SharmaShashikant Sharma
Try changing your query to only group by Target_Company__c like below
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];

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.
Samyra Chrisp 18Samyra Chrisp 18
Shashikant,

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?
Shashikant SharmaShashikant Sharma
This is your code. I tested in my org and worked fine.
 
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>();
Set<Id> accountIds = new Set<Id>();
for(Investment__c req:trigger.new)
{
DealIDs.add(req.ID);

// set of account Ids
accountIds.add( req.Target_Company__C );

}

// this was the line which was not working earlier as condition was Id in: accounts as accounts was empty
Accounts = [Select ID, DealCount__c from Account  where ID in :accountIds];


 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];
//system.assert( false, Requirements );
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'))) ;}
}
}

// update accounts
update accounts;

}

Thanks
Shashikant