1

I have the below apex code in my trigger but when I try to save it I'm getting an "Error: Compile Error: unexpected token: List at line 16 column 4"

List<AggregateResult> result = [
         select OpportunityId, count(Id),Test__c
         from OpportunityLineItems
         where (test__c = NULL) and
                OpportuntiyId in :oppsToCheckMap.keySet() group by OpportunityId];

Here is the complete trigger:

trigger AddressValidation on Opportunity (before insert, before update) {

Map<Id, Opportunity> oppsToCheckMap = new Map<Id, Opportunity>();

for(Opportunity opp : trigger.new) {

    if(opp.Stage == 'Closed Won') { 
        if(trigger.isInsert || 
           trigger.isUpdate && trigger.oldMap.get(opp.Id).Stage != opp.Stage)
            oppsToCheckMap.put(opp.Id, opp);
        }
    }            

}

List<AggregateResult> result = [
         select OpportunityId, count(Id),Test__c
         from OpportunityLineItems
         where (Test__c = NULL) and
                OpportuntiyId in :oppsToCheckMap.keySet() group by OpportunityId];

for(AggregateResult aggCount : result) {

    integer roleCount = (integer)aggCount.get('expr0');
    if(roleCount == 0) {
        Id oppId = (Id)aggCount.get('OpportunityId');
        Opportunity opp = oppsToCheckMap.get(oppId);
        opp.Stage.addError('Requires Facility Addresss to be Updated on Product/Line Item');
    }

 }
} 
2
  • The problem is probably with the line before this one.
    – martin
    Aug 11, 2015 at 15:47
  • I'm not entirely sure, but aren't you still supposed to group it by Facilities__C even if you placed there Facilities_C = null
    – dotmiko
    Aug 11, 2015 at 15:50

1 Answer 1

0

You were missing a bracket after an if statement.

trigger AddressValidation on Opportunity (before insert, before update) {

    Map<Id, Opportunity> oppsToCheckMap = new Map<Id, Opportunity>();

    for(Opportunity opp : trigger.new) {

        if(opp.Stage == 'Closed Won') { 
            if(trigger.isInsert || 
               trigger.isUpdate && trigger.oldMap.get(opp.Id).Stage != opp.Stage){ // Missing bracket here.
                oppsToCheckMap.put(opp.Id, opp);
            }
        }            

    }


    List<Opportunity> result = [
             SELECT Id, (select OpportunityId, Test__c
                         from OpportunityLineItems // now we can make it plural
                         where Test__c = NULL)
             FROM Opportunity
             WHERE Id in :oppsToCheckMap.keySet()];


    for(Opportunity o : result) {

        integer roleCount = o.OpportunityLineItems.size();
        if(roleCount == 0) {
            Id oppId = o.Id;
            Opportunity opp = oppsToCheckMap.get(oppId);
            opp.Stage.addError('Requires Facility Addresss to be Updated on Product/Line Item');
        }

     }
}
9
  • Thanks Martin . But now I'm getting a new error Error "Error: Compile Error: sObject type 'OpportunityLineItems' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 16 column 36" Aug 11, 2015 at 16:03
  • @SalesforceLearner you only should make opportunitylineitem plural if you are querying for it as a child object
    – martin
    Aug 11, 2015 at 16:05
  • I'm getting error like "Error: Compile Error: field 'Test__c' can not be filtered in query call at line 16 column 36 " . I can't filter by a custom field on opportunitylineitem? How can I get the count of Id's? it is a longtextarea field Aug 11, 2015 at 16:15
  • @SalesforceLearner I believe you will need to also group the query by Test__c and then go through the results to find the aggregate where Test__c is NULL.
    – martin
    Aug 11, 2015 at 16:19
  • 1
    I think the issue is we cant filter using the long text area fields in the SOQL. Since test__c is longtextarea field we are getting this error. I was able to save if I use a regular number field Aug 11, 2015 at 17:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .