-1

I have written an Apex code for whenever a new Record for City_census custom Object is inserted, the approver should automatically be the owner of that record based on their City.

In Custom Metadata, I have created a Queue for 4 different cities.

These 4 Cities should be the basis of which Record owner to assign as the approver.

Below is the code I have written for this: Not sure if this would work. But I keep getting this error under logs:

Method does not exist or incorrect signature: void indexOf(Group) from the type String

Where did I have done wrong?

    //WHENEVER A NEW RECORD IS ADDED
    public static void submitForApproval(List<City_Census__c> census){
        
    //Set<Id> cityName = new Set<Id>();
    //Map<Id, List<Contact>> cityName = new Map<Id, List<Contact>>();
    census = new List<City_Census__c>();
    
    Boolean isAutoApproved = true;
    String approverCityBased;
    
    Group queueApprover = new Group();
    //List<City_Census__c> cityCensus = new List<City_Census__c>();
    List<Group> listOfApprovers = [SELECT Id, Name FROM Group WHERE Type = 'Queue'];
    //String cityName = listOfApprovers.addAll(Name);
    
    for(Group City : listOfApprovers){
        Group cityName = new Group(Id = queueApprover.Id);
        if(City.Name.indexOf(cityName) != -1){
            approverCityBased = City.Name;
        }
    }
    
    
    queueApprover = [SELECT Id, Name FROM Group WHERE Type = 'Queue' AND Name =: approverCityBased];
    String approverId = queueApprover.Id;
    
    for(City_Census__c cityRecord : census){
        cityRecord.OwnerId = approverId;
        
        if(cityRecord.Approval_Status__c == null || !isAutoApproved){
            cityRecord.Approval_Status__c = 'Approval';
        }
        
        if(isAutoApproved){
            cityRecord.Approval_Status__c = 'Approved';
            cityRecord.Approved_By__c = approverCityBased;
        }
        
    }
2
  • Welcome to Salesforce Stack Exchange (SFSE).
    – Moonpie
    Sep 6, 2021 at 1:22
  • 1
    It is unclear what you are actually trying to achieve here.
    – Adrian Larson
    Sep 6, 2021 at 15:27

2 Answers 2

5

This is one of those cases where the error correctly tells you what the issue is.

indexOf() is a String method.

This means it must be used/called on a String; but in this case it also takes a String as the parameter:

someString.indexOf(someOtherString) will search inside someString to see if someOtherString occurs within it.

Apex Reference Guide: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_System_String_indexOf


In your first for loop, you instantiate the variable cityName as a Group type. Then you try to call indexOf(cityName).

That appears to be where you are getting your error.


However, there are number of other issues I see in your code....

For example: You pass in a List<City_Census__c> that you named census; then you immediately wipe out the list that you passed in with census = new List<City_Census__c>). So even if you fix your current error, when you get to the second for loop, census will be empty.

Another example: You set isAutoApproved to true then never modify it, so it will always be true. Therefore inside the second for loop, there is no need to even consider isAutoApproved in any of the if conditions.


With these (and other) issues as well as seeing all the commented out lines where it appears that you have tried a number of different things, it seems that you are just sort of "hacking" at the code to try to get to something that works. (My apologies if my assumption is incorrect.) In my experience, that never bodes well.... You will probably not get the output you want; but if you do, you will probably not understand why you get it - which causes even more headache when a tester or user reports odd behavior and you try to track down the cause.

I would suggest that you take a step back, re-think through the logic of what you are trying to achieve (maybe write something down), write out some pseudocode, then methodically write your class.

0

You are looking for a type Group as a substring in a string, for this to work, you need to convert from Group to String before using the variable as a parameter.

1
  • But converting the empty Group to a String won't give a city name.
    – Moonpie
    Sep 6, 2021 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.