0

Trying to create class to create the record name based on Type(custom field), Postal code, and number of records with existing postal code.
If the postal code does not exist on any Product records and type is equal to 'Field' then name is F_Postalcode value If the postal code already exist on Product record and type is Field then name is F_Postalcode value_001. The numeric value will increase based on the number of product records with the same zip code.
If the Type is not Field then the value needs to be an 'R'

I am getting errors at lines:
"message": "expecting a colon, found 'prodIds.Zip_Code__c' (17:14)"
"message": "Unexpected token 'prodIds.Zip_Code__c'. (17:140)"
"message": "Extra 'IDENTIFIER', at 'AND'. (17:173)"
"message": "Variable does not exist: Type__c (23:45)"
"message": "Condition expression must be of type Boolean: String (23:73)"

This is what I have so far:

global class ProductTriggerHandler {

public Map<Id, SObject> updateRecordsMap= new Map<Id, SObject>();
public Id RTId;

global override void bulkBeforeExtended() {
    
 if( Trigger.isInsert){
        List<Id> productIds = new List<Id>();
    
        for (Product__c prods:(List<Product__c >) trigger.New)
        {
            productIds.add(prods.Id);
        }

        Map<Id,Product__c>  prodMap = new Map<Id,Product__c>
        ([select id,Name,Type__c,Zip_Code__c from Product__c where id in : productIds]);
    
        for (Product__c newprod : (List<Product__c >) trigger.New)
        {
            Product__c prod = prodMap.containskey(newprod.Id) ? prodMap.get(newprod.Zip_Code__c) : null; 
            string fieldtype =  prodMap.Type__c = 'Field' ? 'F' : 'R';

            if(prod != null){
                string prodname = fieldtype;
                decimal prodnum = prodMap.size()+1;
                newprod.Name = fieldtype + prod.Zip_Code__c + prodnum;  
            }
        }
    }
2
  • Please edit your post to include the error messages. Nov 9, 2020 at 21:27
  • FYI: When you're getting errors, you should be including the error messages (and stack traces). They're simple enough in this case, but anything you can do to make it easier for people to answer increases your chance of getting a good and accurate answer.
    – Derek F
    Nov 9, 2020 at 21:28

1 Answer 1

4

Your second error is the easier one to diagnose and resolve.

Ternaries have the following syntax <boolean expression> ? <value if true> : <value if false>;

You've mistaken the assignment operator (single =) for the comparison operator (double =, i.e. ==). You need to be using the comparison operator

As for the first error

An Id is just that, an Id. It has no additional fields. An Id can never have a Postal Code (though the SObject record having that Id might).

The query you're trying to run here doesn't make much sense. You already have the records with your given Ids from Trigger.new, querying them again won't tell you anything new (and won't return anything at all if this is a Before Insert trigger, and inserted records don't gain Ids until the After Insert context).

What I suspect you want to do here is gather a set of Postal Codes rather than Ids (you say you want to find how many times it has been used before), and then query strictly on the postal codes.

Instead of a Map<Id, Product__c>, I'd suggest making a Map<String, List<Product__c>> (which will require you to loop over the results to populate). With that approach, the key is the Postal Code, and the List<Product__c> is there to get you the number of things under that postal code (using the .size() method of the list class).

That approach would allow you to add new items to the list so that you don't need to add any other logic to increment your count if other Product__c records in the same trigger chunk use the same postal code.

You also have an issue in your query. If you want to make use of variable binding, you need to prepend : to the apex variable in your query.

Wrong
[SELECT Id FROM Account WHERE Id IN accountIds]

Right
[SELECT Id FROM Account WHERE Id IN :accountIds]

0

You must log in to answer this question.

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