5

I have a SOQL query in my server side controller Like

SELECT Id,PDCN__r.Id,PDCN__r.Name,PDCN__r.Brand_Trademark_Name__c,PDCN__r.Package_Standard_Name__c 
                                           FROM PDCNGrpJunc__c WHERE PDCN_Group__c = 'aBmq00000008UIUCA2'
                                           ORDER BY createdDate DESC

This Query Returns results when I specify PDCN_Group__c = 'aBmq00000008UIUCA2'. Now I have a method which takes a parameter groupId, when I write that groupId in SOQL Query I get Error-: FROM PDCNGrpJunc__c WHERE PDCN_Group__c = '+groupId+' ^ ERROR at Row:2:Column:70 invalid ID field: +groupId+

public static list < PDCNGrpJunc__c > fetchJunction(String groupId) {
        System.debug('****groupId****'+groupId);
        List < PDCNGrpJunc__c > objJuncList = new List < PDCNGrpJunc__c > ();
        List < PDCNGrpJunc__c > objJunc = [SELECT Id,PDCN__r.Id,PDCN__r.Name,PDCN__r.Brand_Trademark_Name__c,PDCN__r.Package_Standard_Name__c 
                                           FROM PDCNGrpJunc__c WHERE PDCN_Group__c = '+groupId+'
                                           ORDER BY createdDate DESC ];

        // play for loop on objJunc and add each Junction to objJuncList List.
        for (PDCNGrpJunc__c c: objJunc) {
            objJuncList.add(c);
        }
        // return the List of Junctions
        return objJuncList;
    }

Could Someone Tell Me what I might be doing Wrong and How to pass the parameter into SOQL Query. Thanks!!

2
  • What is the error you are getting. Help us help you
    – Eric
    Aug 25, 2018 at 9:16
  • Apologies, I added the Error Aug 25, 2018 at 9:20

1 Answer 1

7
public static list < PDCNGrpJunc__c > fetchJunction(String groupId) {
        System.debug('****groupId****'+groupId);
        List < PDCNGrpJunc__c > objJuncList = new List < PDCNGrpJunc__c > ();
        List < PDCNGrpJunc__c > objJunc = [SELECT Id,PDCN__r.Id,PDCN__r.Name,PDCN__r.Brand_Trademark_Name__c,PDCN__r.Package_Standard_Name__c 
                                           FROM PDCNGrpJunc__c WHERE PDCN_Group__c = :groupId
                                           ORDER BY createdDate DESC ];

        // play for loop on objJunc and add each Junction to objJuncList List.
        for (PDCNGrpJunc__c c: objJunc) {
            objJuncList.add(c);
        }
        // return the List of Junctions
        return objJuncList;
    }

You have to use parameters as PDCN_Group__c = :groupId.

Accessing Variables in SOQL Queries

SOQL statements in Apex can reference Apex code variables and expressions if they are preceded by a colon (:). The use of a local variable within a SOQL statement is called a bind.

This example shows how to use the targetDepartment variable in the WHERE clause.

String targetDepartment = 'Wingo';
Contact[] techContacts = [SELECT FirstName,LastName 
                          FROM Contact WHERE Department=:targetDepartment];
1
  • Thanks A Lot!! That Solved It. How to mark this as Best Answer? Aug 25, 2018 at 9:22

You must log in to answer this question.

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