Skip to main content 3 月 5 日~ 6 日にサンフランシスコで開催される TDX (Salesforce+ でも配信) で「Developer Conference for the AI Agent Era (AI エージェント時代に向けた開発者向けカンファレンス)」にぜひご参加ください。お申し込みはこちら
E C が「#Apex」で質問
Hello,

I am trying to deploy my first trigger to production and getting this error. Can someone assist? Below is the code for the trigger:

trigger rosterTrigger on Roster__c  (after insert) {

    

        public set<String> strTypes = new set<String>();

        public set<String> strYears = new set<String>();

        public list<Contact> lstContact = new list<Contact>();

        public list<Contact_to_Roster_Association__c> lstContactAssociations = new list<Contact_to_Roster_Association__c> ();

        

         for(Roster__c objRoster : trigger.new)

         {

             strTypes.add(objRoster.Program_Type__c);

             strYears.add(objRoster.Program_Year__c);

         }

                 

         for(Contact objContact : [Select Id ,Alumni_Type__c,  Alumni_Year__c from Contact where Alumni_Type__c IN:  strTypes OR Alumni_Year__c IN : strYears])

         {

             

             if(objContact.Alumni_Type__c !=null && objContact.Alumni_Year__c !=null)

             {

                 lstContact.add(objContact);

             }

             

         }

        

         for(Roster__c objC : trigger.new)

         {

             for(Contact objContact : lstContact)

            {

                Contact_to_Roster_Association__c objRA = new Contact_to_Roster_Association__c();

                objRA.Contact__c = objContact.Id;

                objRA.Roster__c = objC.Id;

                lstContactAssociations.add(objRA);

            }

         }

        

        if(!lstContactAssociations.isEmpty())

        {

            insert lstContactAssociations;

        }

}

More info about the trigger here: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000B2ug 
4 件の回答
  1. 2015年3月31日 18:56
    Hi LFI,

    To cover the code for this you can create one test class and insert below code there it will cover your code...

    Contact objContact = new Contact(LastName='Tst');

    objContact.Alumni_Type__c = 'testType';

    objContact.Alumni_Year__c  = '2002';

    insert objContact;

    Roster__c objR = new Roster__c ();

    objR.Program_Type__c = 'testType';

    objR.Program_Year__c = '2002';

    insert objR;

    P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

    Thanks,

    Sandeep

    Salesforce Certified Developer 

     
  2. 2015年3月31日 20:36

    I got it, below is the final code:

    @isTest 

    private class TriggerTestClass {

    static testMethod void validateTrigger() {

        

        Contact objContact = new Contact(LastName='Tst', User_ID__c='11111111');

        objContact.LFI__c= 'LFI';

        objContact.LFI_Year__c= '2005';

        insert objContact;

        Roster__c objR = new Roster__c ();

        objR.Program_Type__c = 'LFI';

        objR.Program_Year__c = '2005';

        insert objR;

        }

    }

    Biggest thanks to Sandeep for getting me started!

     
  3. 2015年4月1日 4:44
    Hi LFI,

    It was too late here so I signed off for the day...Good that you were able to solve the errors....

    Thanks LFI!!
  4. 2015年3月31日 19:03
    Getting error: Error: Compile Error: Field is not writeable: Contact.Alumni_Type__c at line 6 column 5​

    @isTest 

    private class HelloWorldTestClass {

    static testMethod void validateHelloWorld() {

        Contact objContact = new Contact(LastName='Tst');

        objContact.Alumni_Type__c= 'LFI';

        objContact.Alumni_Year__c= '2002';

        insert objContact;

        Roster__c objR = new Roster__c ();

        objR.Program_Type__c = 'testType';

        objR.Program_Year__c = '2002';

        insert objR;

        }

    }
0/9000