0

I am working on a trigger and I receive the Method does not exist problem, for the following line of code: parentIds(crdLine.Credit_Request__c);. Any thoughts on the issue. Problem states void parentIds(Id)

trigger MakingApprovalRequiredTrigger on Credit_Lines__c (After Insert, After Update, After Delete){

Map<Id, List<Credit_lines__c>> parentIds = new Map<id, List<credit_lines__c>>();

If(Trigger.IsInsert || Trigger.IsUpdate){
    For(Credit_Lines__c crdLine : Trigger.New){
        If(crdLine.Credit_Request__c != Null){
            parentIds(crdLine.Credit_Request__c);
        }
    }
}
2

parentIds is a map so you cannot just pass a parameter in it but rather you have to put values in them. For ex in your code-

trigger MakingApprovalRequiredTrigger on Credit_Lines__c (After Insert, After Update, After Delete){

Map<Id, List<Credit_lines__c>> parentIds = new Map<id, List<credit_lines__c>>();

If(Trigger.IsInsert || Trigger.IsUpdate){
    For(Credit_Lines__c crdLine : Trigger.New){
        If(crdLine.Credit_Request__c != Null){
            //Creating a temporary list so that it can reinitialised with every new coming value in for loop and can be put in map
            List<Credit_lines__c> tempCreditLines = new list<Credit_lines__c>();

            //Checking if map already contains key for crdLine.Credit_Request__c
            if(parentIds.conTainsKey(crdLine.Credit_Request__c)){
                //If map already contains key for crdLine.Credit_Request__c then taking the existing list
                tempCreditLines = parentIds.get(crdLine.Credit_Request__c);
            }

            //Adding the coming credit record to list
            tempCreditLines.add(crdLine);

            //put value in map for the list created above and the key crdLine.Credit_Request__c
            parentIds.put(crdLine.Credit_Request__c, tempCreditLines);
        }
    }
}
1

If I understand, you're trying to sort a list of Credit_lines__c by Credit_Request__c.

You should start by reading the documentation about the put method of the Map class.

You have to check first if the Id is already in your map. Then, if so, you have to add a new record to the list. If not, you have to put a whole new list containing your record in your Map.

Here's the right code:

for(Credit_Lines__c crdLine : Trigger.New){
        If(crdLine.Credit_Request__c != Null){
            if(parentIds.containsKey(crdLine.Credit_Request__c)){
                 parentIds.get(crdLine.Credit_Request__c).add(crdLine);
            }else{
                 List<Credit_lines__c> creditLines = new List<Credit_lines__c>{crdLine};
                 parentIds.put(crdLine.Credit_Request__c, creditLines);
            }
        }
}
1
  • Hey Martin I guess parentIds(crdLine.Credit_Request__c); will not be there as this is a map. Aug 3 '17 at 15:49

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.