1

I was trying to make my hands dirty with the apex coding as I am really really weak in that. Below is the piece of Code I wrote which is getting called by a trigger(following the best practices). However I am getting this really frustrating error:

Initial term of field expression must be a concrete SObject

Here's my sceranio:

I want to retrieve a perticular record from the custom metadata type(Client_Settings__mdt) and fill in the remaining values in my layout automatically before the record gets saved. Below are my code snipets Trigger:

trigger ScoreCostSavings on SCORE_Cost_Savings__c (before insert,before update,before delete,
                            after insert,after update,after delete) {

    //check if this is before trigger or after trigger
    SCORE_Cost_Savings__c[] scs = Trigger.new;
    ScoreCodeFrameworkUtil scfu = new ScoreCodeFrameworkUtil();

    if(Trigger.isbefore){
        if(Trigger.isInsert){
        //Code that would fire before any record is inserted
        scfu.updID(scs);
        }
        else if(Trigger.isUpdate){
        //Code that would fire before any record is updated
        scfu.updID(scs);
        }
        else if(Trigger.isDelete){
        //Code that would fire before any record is deleted
        }
    }
    else{
        if(Trigger.isInsert){
        //Code that would fire after any record is updated
        }
        else if(Trigger.isUpdate){
        //Code that would fire after any record is updated
        }
        else if(Trigger.isDelete){
        //Code that would fire after any record is deleted
        }
    }
}

and here goes the Class:

public class ScoreCodeFrameworkUtil{
    private Integer count;

    public void updID(SCORE_Cost_Savings__c[] scrdta){
        //SCORE_Client_Settings__mdt[] scsm = [SELECT MasterLabel,Program_Framework__c,Sub_Program_Framework__c FROM SCORE_Client_Settings__mdt];
        List<SCORE_Client_Settings__mdt> scsm = [SELECT MasterLabel,NamespacePrefix,Program_Framework__c,Sub_Program_Framework__c FROM SCORE_Client_Settings__mdt where MasterLabel =: scrdta.Code__c Limit 1];

        if(!scsm.isEmpty()){
            //scsm[0].First_Name__c = billing.billingfirstname;
            scrdta.Program_Framework__c = scsm[0].Program_Framework__c;
        }
    }
}

Error line:

List scsm = [SELECT MasterLabel,NamespacePrefix,Program_Framework__c,Sub_Program_Framework__c FROM SCORE_Client_Settings__mdt where MasterLabel =: scrdta.Code__c Limit 1];

1

The problem here is the incoming parameter for the function is array and you are referencing a field directly from the array which is not allowed .

The below shows the approach of using loops on array and then using property to access the values

public void updID(SCORE_Cost_Savings__c[] scrdta){
    set<String> setscrdtaCodes = new set<String> ();
    for(SCORE_Cost_Savings__c sCode :scrdta){
        setscrdtaCodes.add(sCode.Code__c);
    }
    List<SCORE_Client_Settings__mdt> scsm = [SELECT MasterLabel,NamespacePrefix,Program_Framework__c,Sub_Program_Framework__c FROM SCORE_Client_Settings__mdt where MasterLabel IN : setscrdtaCodes Limit 1];

    if(!scsm.isEmpty()){
        //scsm[0].First_Name__c = billing.billingfirstname;
        scrdta.Program_Framework__c = scsm[0].Program_Framework__c;
    }
}

If you want single reference from the array then you can use below

public void updID(SCORE_Cost_Savings__c[] scrdta){
List<SCORE_Client_Settings__mdt> scsm = [SELECT MasterLabel,NamespacePrefix,Program_Framework__c,Sub_Program_Framework__c FROM SCORE_Client_Settings__mdt where MasterLabel =: scrdta[0]. Limit 1];

 if(!scsm.isEmpty()){
    //scsm[0].First_Name__c = billing.billingfirstname;
    scrdta.Program_Framework__c = scsm[0].Code__c;
  }
}
0

You can't get or set a field on a collection.

Proper Get:

Set<String> codes = new Set<String>();
for (Score_Cost_Savings__c record : scrdta)
    codes.add(record.Code__c);
List<SCORE_Client_Settings__mdt> metadata = [
    SELECT ... FROM SCORE_Client_Settings__mdt
    WHERE MasterLabel IN :codes LIMIT 1
];

Proper Set:

for (Score_Client_Settings__c record : scrdata)
    record.Program_Framework__c = metadata[0].Program_Framework__c

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.