0

I have 3 objects: university is the junction object, remaining 2 are master, in university I have to query and get the records from remaining 2 objects. I use the following code:

list<University__c> univerlist = trigger.new;
list< University__c > wantedfields = [select id, sahi__Select_Course__c, sahi__Select_Course__r.sahi__Technology__c, sahi__Select_Faculty__c, sahi__Select_Faculty__r.sahi__First_Name__c, sahi__Select_Faculty__r.CreatedDate
                                      from University__c 
                                      where sahi__Select_Course__c = : univerlist[0].sahi__Select_Course__c 
                                      and sahi__Select_Faculty__c = :univerlist[0].sahi__Select_Faculty__c];
    system.debug('wantedfields'+wantedfields[0]);
system.debug('univerlist[0].sahi__Select_Course__c'+univerlist[0].sahi__Select_Course__c);
    system.debug('univerlist[0].sahi__Select_Course__r'+univerlist[0].sahi__Select_Course__r);
    system.debug('sahi__Select_Course__r.sahi__Technology__c'+univerlist[0].sahi__Select_Course__r.sahi__Technology__c);
    system.debug('sahi__Select_Faculty__r.sahi__First_Name__c'+univerlist[0].sahi__Select_Faculty__r.sahi__First_Name__c);
    system.debug('sahi__Select_Faculty__r.CreatedDate'+univerlist[0].sahi__Select_Faculty__r.CreatedDate);

I am new to Salesforce. Please suggest me what to do, how to get values instead of nulls in system.debug(...).

1

I'm not completely sure what your business req are as you did not describe them in detail, but seeing your code I assumed you'd want to build a trigger on University__c SObject.

The reason why it was giving you null is that because your hardcoding it to be dependent on the first record of the list, but your not checking if the first record has the selected fields or not. Here is a code which would add first all the found ids in a Set and then creating the SQL statement based on the values from the set. I hope it makes sense.

Note: I haven't tried the code out so it might contain some syntax errors.

 trigger Test on University__c (before insert, before update)
    { 
        Set<id> courses = new set<id>();
        Set<id> faculties = new set<id>();
        for(University__c u : Trigger.new)
        {

          if(u.sahi__Select_Course__c!=null)
          courses.add(u.sahi__Select_Course__c);

          if(u.sahi__Select_Faculty__c!=null)
          faculties.add(u.sahi__Select_Faculty__c);
        }

        list< University__c > wantedfields=[select id,sahi__Select_Course__c,sahi__Select_Course__r.sahi__Technology__c,sahi__Select_Faculty__c,sahi__Select_Faculty__r.sahi__First_Name__c,sahi__Select_Faculty__r.CreatedDate  from University__c where  sahi__Select_Course__c in: courses   and sahi__Select_Faculty__c in: faculties];


        for(University__c u: wantedfields)
        {

                             system.debug('u.sahi__Select_Course__c'+u.sahi__Select_  Course__c);
                    system.debug(u.sahi__Select_Course__r'+u.sahi__Select_Course__r);
                    system.debug('u.sahi__Technology__c'+u.sahi__Select_Course__r.sahi__Technology__c);
                    system.debug('sahi__Select_Faculty__r.sahi__First_Name__c'+u.sahi__Select_Faculty__r.sahi__First_Name__c);
                    system.debug('sahi__Select_Faculty__r.CreatedDate'+u.sahi__Select_Faculty__r.CreatedDate);

        }
       } 
0

trigger sendingEmailToFaculty on University__c(before insert,before update,after insert, after update) {

if(trigger.isBefore && trigger.isInsert){ sendEmailToFacultyClass.preventDuplicate(trigger.new); }else if(trigger.isAfter &&(trigger.isInsert || trigger.isUpdate )){

sendEmailToFacultyClass.sendEmail(trigger.new);

} }

public class sendEmailToFacultyClass { public static list < sahi__University__c > sendEmail(list < sahi__University__c > universitis) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); University__c uv1 = [select sahi__Select_Course__r.sahi__Start_Date__c, sahi__Select_Course__r.sahi__Technology__c, sahi__Select_Faculty__r.sahi__First_Name__c, sahi__Select_Faculty__r.sahi__email__c, sahi__Select_Faculty__r.CreatedDate from University__c where id = : universitis[0].id ]; for (University__c unvs: universitis) {

        /*University__c uv1 = [select sahi__Select_Course__r.sahi__Start_Date__c, sahi__Select_Course__r.sahi__Technology__c,
            sahi__Select_Faculty__r.sahi__First_Name__c, sahi__Select_Faculty__r.sahi__email__c, sahi__Select_Faculty__r.CreatedDate from University__c where id = : universitis[0].id
        ];*/
        system.debug('coursname.sahi__Technology__c ' + uv1.sahi__Select_Course__r.sahi__Technology__c);

        string email1 = uv1.sahi__Select_Faculty__r.sahi__email__c;
        string name1 = uv1.sahi__Select_Faculty__r.sahi__First_Name__c;
        string tname1 = uv1.sahi__Select_Course__r.sahi__Technology__c;
        Datetime crtddatre1 = uv1.sahi__Select_Faculty__r.CreatedDate;
        system.debug(email1);
        system.debug('faculty' + uv1.sahi__Select_Faculty__r.sahi__email__c);
        if (uv1.id != null && email1 != null) {
            mail.setToAddresses(new String[] {
                email1
            });
            mail.setSubject('Automated email: Contact created');

            String body1 = 'Dear ' + name1 + '.' + '\n' + 'Your technology ' + tname1 + ' on ' + crtddatre1 + ' is confirmed' + '.' + '\n' + 'Thanks for speaking at the conference!';

            mail.setPlainTextBody(body1);
            Messaging.sendEmail(new Messaging.SingleEMailMessage[] {
                mail
            });


        }

    }
    return universitis;
}

public static list < University__c > preventDuplicate(list < University__c > uts) {
    for (University__c unvs: uts) {
        //sahi__course__c course = [select id, sahi__Start_Date__c from sahi__course__c where id = : unvs.sahi__Select_Course__c];
        //system.debug('course' + course);
        //list < University__c > duplicates = [select id from University__c where sahi__Select_Faculty__c = : unvs.sahi__Select_Faculty__c and sahi__Select_Course__r.sahi__Start_Date__c = : course.sahi__Start_Date__c];
        list < University__c > duplicates = [select id from University__c where sahi__Select_Faculty__c = : unvs.sahi__Select_Faculty__c and sahi__Select_Course__c = : unvs.sahi__Select_Course__c];
        system.debug('duplicates' + duplicates);
        if (!duplicates.isEmpty()) {
            unvs.addError('please select different faculty different date');
        }

    }
    return uts;
}

} i did like this,i should not write soql in for loop,how to avoid it,pls explain

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.