1

i have this code i getting the error the following error, i'm new to salesforce developer someone can help me?

    public class ActiveContactListController {

    @AuraEnabled
    public static List<AccountContactRelation> getContacts(Id recordId) {

        List<AccountContactRelation> ConList = [
            SELECT 
                id, Contact.FirstName,Contact.LastName,Contact.MobilePhone,Roles 
            FROM AccountContactRelation 
            WHERE AccountId =: recordId 
            AND 
            IsActive = true];

        List<ContactsWithRoles> contactsWithRoles;
        for (AccountContactRelation accContRel :ConList)
        {
            ContactsWithRoles contWithRoles = new ContactsWithRoles();
            contWithRoles.accConRelWithRoles = accContRel;
            contWithRoles.roles = accContRel.roles.replace(';',';\n');
            contactsWithRoles.add(contWithRoles);

        }
        **return contactsWithRoles;**
    } 

       public class ContactsWithRoles
       {
           String roles{get; set;}
           AccountContactRelation accConRelWithRoles{get; set;}
       }

}
  • Looks like your method signature is wrong - it says your method should return a List<AccountContactRelation> but your comment says you're returning List<ContactsWithRoles> – John Towers Jan 24 at 16:49
4

This is due to how Type Systems work.

While your ActiveContactListController.ContactsWithRoles object contains an AccountContactRelation object, it (ContactsWithRoles) is not the same as an AccountContactRelation object.

The declared return type in your method needs to match (or extend) the type of the variable (or statement) that you actually return. Since we can't extend SObjects, that means you'll need an exact type match.

You'll either need to change your declared return type to List<ContactsWithRoles>, or change what you're returning so it is actually a List<AccountContactRelation>.

| improve this answer | |

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.