0

I know that there's already several posts on SE for this error but none of them explain this for me.

I'm testing my code by editing one Project Deadline date field, the upProjects size is one and as you can see, I'm adding the Project Month records to a list (existPMs).

But I still get the error

List has more than 1 row for assignment to SObject: External entry point

Error message

when I execute the code.

trigger ProjectLifecycle on MPM4_BASE__Milestone1_Project__c (after insert, after update) {

    List<WEID__c> weids = WEID__c.getall().values();
    Set<Id> validRecordTypeIds = new Set<Id>();

    // add Project Record Type Ids from Custom Setting to list of valid Ids      
    for(WEID__c weid: weids) {
        try {
            validRecordTypeIds.add(weid.WEProjectId__c);
        } catch (System.StringException e) {
            System.debug(
                System.LoggingLevel.ERROR,
                'Invalid Record Type Id ' + weid.WEProjectId__c
            );
        }
    }

    List<MPM4_BASE__Milestone1_Project__c> insProjects = new List<MPM4_BASE__Milestone1_Project__c>();
    List<MPM4_BASE__Milestone1_Project__c> upProjects = new List<MPM4_BASE__Milestone1_Project__c>();
    Map<Id,Integer> projectDuration = new Map<Id,Integer>();
    Map<Integer,String> monthName = new Map<Integer,String>{1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December'};
    List<Project_Month__c> newPMs = new List<Project_Month__c>();
    List<Project_Month__c> existPMs = new List<Project_Month__c>();

    for(MPM4_BASE__Milestone1_Project__c p : Trigger.New){

        //check that Project record type is valid
        if(validRecordTypeIds.contains(p.RecordTypeId))
        {
            try{
                MPM4_BASE__Milestone1_Project__c oldP = Trigger.OldMap.get(p.Id);

                if(oldP.MPM4_BASE__Kickoff__c != p.MPM4_BASE__Kickoff__c
                   ||
                   oldP.MPM4_BASE__Deadline__c != p.MPM4_BASE__Deadline__c)
                {
                    upProjects.add(p);
                    projectDuration.put(p.Id, p.MPM4_BASE__Kickoff__c.monthsBetween(p.MPM4_BASE__Deadline__c));
                }
            //if old.Map is null, record has been inserted & project months should be added
            }catch(system.NullPointerException e){
                insProjects.add(p);
                projectDuration.put(p.Id, p.MPM4_BASE__Kickoff__c.monthsBetween(p.MPM4_BASE__Deadline__c));
            }
        }
    }

    if(insProjects.size() > 0){

        for(MPM4_BASE__Milestone1_Project__c p : insProjects){

            Date k = p.MPM4_BASE__Kickoff__c;
            Integer km = p.MPM4_BASE__Kickoff__c.month();
            Integer dur = projectDuration.get(p.Id);

            //create a set of Project Month records for each Project
            for(Integer m = km; m < km + dur + 1; m++){

                Project_Month__c pm = new Project_Month__c();
                pm.Project__c = p.Id;
                pm.Name = monthName.get(m)+' '+ k.addMonths(m - 1).year();
                pm.month__c = k.addMonths(m - km + 1).toStartOfMonth() - 1;
                pm.Project_Value__c = p.Opportunity_Total_Net_FSR__c;
                newPMs.add(pm);
                system.debug('newPMs contains ' + newPMs);
            }
        }
    }

    system.debug('upProjects size is ' + upProjects.size() );

    if(upProjects.size() > 0){

        for(MPM4_BASE__Milestone1_Project__c p : [SELECT Id,
                                                 (SELECT Id FROM Project_Months__r) FROM MPM4_BASE__Milestone1_Project__c
                                                   WHERE Id IN :upProjects]){
                                                       existPMs.add(p.Project_Months__r);
                                                   }
        delete existPMs;

        for(MPM4_BASE__Milestone1_Project__c p : upProjects){

            Date k = p.MPM4_BASE__Kickoff__c;
            Integer km = p.MPM4_BASE__Kickoff__c.month();
            Integer dur = projectDuration.get(p.Id);

            for(Integer m = km; m < km + dur + 1; m++){

                Project_Month__c pm = new Project_Month__c();
                pm.Project__c = p.Id;
                pm.Name = monthName.get(m)+' '+ k.addMonths(m - 1).year();
                pm.month__c = k.addMonths(m - km + 1).toStartOfMonth() - 1;
                pm.Project_Value__c = p.Opportunity_Total_Net_FSR__c;
                newPMs.add(pm);
                system.debug('newPMs contains ' + newPMs);
            }
        } 
    }
    insert newPMs;
}

I'd used some code from Trailhead as the template for this! (The last sample code on the page).

I've also tried changing the above to

List<MPM4_BASE__Milestone1_Project__c> pList = [SELECT Id,
                                               (SELECT Id FROM Project_Months__r) FROM MPM4_BASE__Milestone1_Project__c
                                                 WHERE Id IN :upProjects];{
                                               existPMs.add(pList.Project_Months__r);
                                           }
delete existPMs;

but that causes the error

Initial term of field expression must be a concrete SObject:

9
  • I only see on query in your code and it is assigning it to a list type so that should not produce the error. What line specifically is this failing at?
    – Eric
    Jun 19, 2015 at 20:55
  • @Eric Just to be clear, I have deleted some of the code but that's my only SOQL query. It doesn't help that it's not giving me a line. All I know is that if I set a checkpoint at the line if(insProjects.size() > 0){ that's the last line that's executed. I'll update my question in a second with a screenshot of the error.
    – Alex S
    Jun 19, 2015 at 20:59
  • Why are you populating insProjects in the catch methoid
    – Rao
    Jun 19, 2015 at 21:02
  • @rao hopefully my //comment explains the reason. Could populating the list from there cause an issue?
    – Alex S
    Jun 19, 2015 at 21:05
  • @Eric now that you've confirmed that I'm not going completely crazy, I've updated the question with my complete code, in case there's an error somewhere else that's causing confusion. The SOQL query's not appearing in my debug log.
    – Alex S
    Jun 19, 2015 at 21:12

2 Answers 2

2

Fix this:

existPMs.add(p.Project_Months__r)

needs to be

existPMs.addAll(p.Project_Months__r)

The run in dev console and see where the error is coming from. It may not even be in the trigger at all but may be elsewhere.

2
  • Just after List<Project_Month__c>.add(Object), I have System.QueryException: List has more than 1 row for assignment to SObject..does that explain it? Apologies, I didn't realize that you could find the line there. And it looks like the SOQL query did execute..I could post the log in a Gist, since I'm not 100% sure what I'm looking for?
    – Alex S
    Jun 19, 2015 at 21:44
  • 2
    I guess that error would make sense there just interesting it was not found at compile that Method List<sObject>.add(List<sObject>) was not valid.....
    – Eric
    Jun 19, 2015 at 21:58
-1

I'm just looking at the last updated code and it seems to be incorrect:-

Existing code:-

List<MPM4_BASE__Milestone1_Project__c> pList = [SELECT Id,
                                               (SELECT Id FROM Project_Months__r) FROM MPM4_BASE__Milestone1_Project__c
                                                 WHERE Id IN :upProjects];{
                                               existPMs.add(pList.Project_Months__r);
                                           }

delete existPMs;

Issue:- you are using list value as a instance of MPM4_BASE__Milestone1_Project__c, you would need to use list methods/ mechanism to access list members for e.g. pList[0].Project_Months__r

Suggested code:-

For(MPM4_BASE__Milestone1_Project__c project = [SELECT Id,
                                               (SELECT Id FROM Project_Months__r) FROM MPM4_BASE__Milestone1_Project__c
                                                 WHERE Id IN :upProjects])
{
   existPMs.add(project .Project_Months__r);
}
delete existPMs;
2
  • Thanks for the suggestion but if you take a look at my full code (not the second snippet) you'll see that I'm already using your suggestion. Also, the = would need to be changed to an :
    – Alex S
    Jun 19, 2015 at 21:32
  • Oh ok, you are right, with so many edits, i was not able to keep track of it. Which line are you getting error at ? and what's the exact error message?
    – Anshul
    Jun 19, 2015 at 21:34

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.