• Neha Kamra
  • NEWBIE
  • 35 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 1
    Questions
  • 5
    Replies
While creating trigger in Trailhead.,got this error: Challenge Not yet complete... here's what's wrong: 
The trigger logic failed to prevent double booking of a speaker. Please double check the 'RejectDoubleBooking' trigger code.
Here is code:

trigger RejectDoubleBooking on Session_Speaker__c (before insert, before update) {

    //collect ID's to reduce data calls 
    List<Id> speakerIds = new List<Id>();    
    Map<Id,DateTime> requested_bookings = new Map<Id,DateTime>();
    
    //get all speakers related to the trigger
    //set booking map with ids to fill later
    for(Session_Speaker__c newItem : trigger.new) {
        requested_bookings.put(newItem.Session__c,null); 
        speakerIds.add(newItem.Speaker__c);
    }
    
    //fill out the start date/time for the related sessions
    List<Session__c> related_sessions = [SELECT ID, Session_Date__c from Session__c WHERE ID IN :requested_bookings.keySet()];
    for(Session__c related_session : related_sessions) {
        requested_bookings.put(related_session.Id,related_session.Session_Date__c);
    }
    
    //get related speaker sessions to check against
    List<Session_Speaker__c> related_speakers = [SELECT ID, Speaker__c, Session__c, Session__r.Session_Date__c from Session_Speaker__c WHERE Speaker__c IN :speakerIds];
    
    //check one list against the other
    for(Session_Speaker__c requested_session_speaker : trigger.new) {
        DateTime booking_time = requested_bookings.get(requested_session_speaker.Session__c);
        for(Session_Speaker__c related_speaker : related_speakers) {
            if(related_speaker.Speaker__c == requested_session_speaker.Speaker__c &&
               related_speaker.Session__r.Session_Date__c == booking_time) {
                   requested_session_speaker.addError('The speaker is already booked at that time');
               }
        }
    }


}


Thanks,


 
While creating trigger in Trailhead.,got this error: Challenge Not yet complete... here's what's wrong: 
The trigger logic failed to prevent double booking of a speaker. Please double check the 'RejectDoubleBooking' trigger code.
Here is code:

trigger RejectDoubleBooking on Session_Speaker__c (before insert, before update) {

    //collect ID's to reduce data calls 
    List<Id> speakerIds = new List<Id>();    
    Map<Id,DateTime> requested_bookings = new Map<Id,DateTime>();
    
    //get all speakers related to the trigger
    //set booking map with ids to fill later
    for(Session_Speaker__c newItem : trigger.new) {
        requested_bookings.put(newItem.Session__c,null); 
        speakerIds.add(newItem.Speaker__c);
    }
    
    //fill out the start date/time for the related sessions
    List<Session__c> related_sessions = [SELECT ID, Session_Date__c from Session__c WHERE ID IN :requested_bookings.keySet()];
    for(Session__c related_session : related_sessions) {
        requested_bookings.put(related_session.Id,related_session.Session_Date__c);
    }
    
    //get related speaker sessions to check against
    List<Session_Speaker__c> related_speakers = [SELECT ID, Speaker__c, Session__c, Session__r.Session_Date__c from Session_Speaker__c WHERE Speaker__c IN :speakerIds];
    
    //check one list against the other
    for(Session_Speaker__c requested_session_speaker : trigger.new) {
        DateTime booking_time = requested_bookings.get(requested_session_speaker.Session__c);
        for(Session_Speaker__c related_speaker : related_speakers) {
            if(related_speaker.Speaker__c == requested_session_speaker.Speaker__c &&
               related_speaker.Session__r.Session_Date__c == booking_time) {
                   requested_session_speaker.addError('The speaker is already booked at that time');
               }
        }
    }


}


Thanks,


 
Challenge is as below:--
To complete this challenge, add a validation rule which will block the insertion of a contact if the contact is related to an account and has a mailing postal code (which has the API Name MailingPostalCode) different from the account's shipping postal code (which has the API Name ShippingPostalCode).
Name the validation rule 'Contact must be in Account ZIP Code'.
A contact with a MailingPostalCode that has an account and does not match the associated Account ShippingPostalCode should return with a validation error and not be inserted.
The validation rule should ONLY apply to contact records with an associated account. Contact records with no associated parent account can be added with any MailingPostalCode value. (Hint: you can use the ISBLANK function for this check)


--------------------------------------------
My validation rule is as attached pictureUser-added image

And Error is- Challenge Not yet complete... here's what's wrong: 
The validation rule failed to enforce the business logic
  • September 02, 2016
  • Like
  • 0
Hi All,
I am not a developer and having difficulties on writing this test code. I have gotten some help to get this far, but can't get my code coverage over 17%. I have been advised i need system asserts but I'm lost. Any help would be much appreciated.
Thank you in advance! I'll get you beer at Dreamforce :)

Current Test Code
@ isTest
private class TestCaseTravel {
 
    static testMethod void testtravelController() {
       //
       Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
       User s = new User( Alias = 'standt', Email='standarduser@seon.com.com', 
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', Division = 'School Bus', UserName='standarduser@seon.com');
    
       
        insert s;

User t = new User( Alias = 'standt', Email='standardusert@seon.com.com', 
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', Division = 'Transit', UserName='standardusert@seon.com');
    
       
        insert t;

User o = new User( Alias = 'standt', Email='standarduser0@seon.com.com', 
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', Division = 'Other', UserName='standarduser0@seon.com');
    
       
        insert o;

        /**
         *  Create dummy objects for testing. Might need to add missing required fields
         */
          List<Travel__c> travels = new List<Travel__c>{};
               new Travel__c (
                  Name='Test Travel'
                  ,Departure_Date__c = Date.today()
                  ,Return_Date__c = Date.today()
                  ,Approval_Status__c = 'Approved'
                  ,Travel_Purpose__c = 'test'
              )
              ;new Travel__c(
                  Name='Test Travel 2'
                  ,Departure_Date__c = Date.today()
                  ,Return_Date__c = Date.today()
                  ,Approval_Status__c = 'Approved'
                  ,Travel_Purpose__c = 'test'
                 ,OwnerId = s.Id
        )
              ;new Travel__c(
                  Name='Test Travel 3'
                  ,Departure_Date__c = Date.today()
                  ,Return_Date__c = Date.today()
                  ,Approval_Status__c = 'Approved'
                  ,Travel_Purpose__c = 'test'
                 ,OwnerId = t.Id
              )
              ;new Travel__c(
                  Name='Test Travel 4'
                  ,Departure_Date__c = Date.today()
                  ,Return_Date__c = Date.today()
                  ,Approval_Status__c = 'Approved'
                  ,Travel_Purpose__c = 'test'
                 ,OwnerId = o.Id
              
              
              )
          ;
          insert travels;


Event e = new Event();
        e.Subject = 'Test Event';
        e.Location = 'Test Location';
        e.Description = 'Test Description';
        e.StartDateTime = Datetime.now();
        e.EndDateTime = Datetime.now();
         
        insert e;
        

        Test.startTest();
        TravelCalendar_Controller controller = new TravelCalendar_Controller();
        controller.pageLoad();
        Test.stopTest();

        //System.assertEquals(someValue,controller.someProperty);
        //System.assertNotEquals(true,controller.events.isEmpty());
        //System.assertEquals(someValue,controller.events.get(0).someProperty);
        //System.assertEquals(false,controller.includeMyEvents);
    }
}

Apex Controller
public class TravelCalendar_Controller {



    public Boolean includeMyEvents {get;set;}

    public list<calEvent> events {get;set;}

    

    //The calendar plugin is expecting dates is a certain format. We can use this string to get it formated correctly

    String dtFormat = 'EEE, d MMM yyyy HH:mm:ss z';

    

    //constructor

    public TravelCalendar_Controller() {

        //Default showing my events to on

        includeMyEvents = false;

    }

    

    public PageReference pageLoad() {

        events = new list<calEvent>();

 
        //Get My Travel

        for(Travel__c mytrav : [select Id, Owner.FirstName, Owner.LastName, Name, Departure_Date__c, Return_Date__c, Owner_Territory__c from Travel__c where Approval_Status__c = 'Approved' and OwnerID = :UserInfo.getUserId() ]){

            DateTime startDT = myTrav.Departure_Date__c;

            DateTime endDT = myTrav.Return_Date__c;

            calEvent mytravEvent = new calEvent();

            

            mytravEvent.title = 'Trip:'+ ' ' + myTrav.Name+ ' ' + myTrav.Owner.FirstName+ ' ' + myTrav.Owner.LastName;

            mytravEvent.allDay = true;

            mytravEvent.startString = startDT.format(dtFormat);

            mytravEvent.endString = endDT.format(dtFormat);

            mytravEvent.url = '/' + myTrav.Id;

            mytravEvent.className = 'event-mytravel';

            events.add(mytravEvent);
    }
 
         //Get School Bus Travel

        for(Travel__c sbtrav : [select Id, Owner.FirstName, Owner.LastName, Name, Departure_Date__c, Return_Date__c, Owner_Territory__c from Travel__c where Approval_Status__c = 'Approved' and  Owner_Division__c = 'School Bus' and OwnerID != :UserInfo.getUserId() ]){

            DateTime startDT = sbTrav.Departure_Date__c;

            DateTime endDT = sbTrav.Return_Date__c;

            calEvent sbtravEvent = new calEvent();

            

            sbtravEvent.title = 'Trip:'+ ' ' + sbTrav.Name+ ' ' + sbTrav.Owner.FirstName+ ' ' + sbTrav.Owner.LastName;

            sbtravEvent.allDay = true;

            sbtravEvent.startString = startDT.format(dtFormat);

            sbtravEvent.endString = endDT.format(dtFormat);

            sbtravEvent.url = '/' + sbTrav.Id;

            sbtravEvent.className = 'event-sbtravel';

            events.add(sbtravEvent);

        }
 
         //Get Transit Travel

        for(Travel__c trtrav : [select Id, Owner.FirstName, Owner.LastName, Name, Departure_Date__c, Return_Date__c, Owner_Territory__c from Travel__c where Approval_Status__c = 'Approved' and  Owner_Division__c = 'Transit' and  OwnerID != :UserInfo.getUserId() ]){

            DateTime startDT = trTrav.Departure_Date__c;

            DateTime endDT = trTrav.Return_Date__c;

            calEvent trtravEvent = new calEvent();

            

            trtravEvent.title = 'Trip:'+ ' ' + trTrav.Name+ ' ' + trTrav.Owner.FirstName+ ' ' + trTrav.Owner.LastName;

            trtravEvent.allDay = true;

            trtravEvent.startString = startDT.format(dtFormat);

            trtravEvent.endString = endDT.format(dtFormat);

            trtravEvent.url = '/' + trTrav.Id;

            trtravEvent.className = 'event-trtravel';

            events.add(trtravEvent);

        }
        



//Get Other Travel

        for(Travel__c ottrav : [select Id, Owner.FirstName, Owner.LastName, Name, Departure_Date__c, Return_Date__c, Owner_Territory__c from Travel__c where Approval_Status__c = 'Approved' and  Owner_Division__c != 'School Bus' and  Owner_Division__c != 'Transit'and OwnerID != :UserInfo.getUserId() ]){

            DateTime startDT = otTrav.Departure_Date__c;

            DateTime endDT = otTrav.Return_Date__c;

            calEvent ottravEvent = new calEvent();

            

            ottravEvent.title = 'Trip:'+ ' ' + otTrav.Name+ ' ' + otTrav.Owner.FirstName+ ' ' + otTrav.Owner.LastName;

            ottravEvent.allDay = true;

            ottravEvent.startString = startDT.format(dtFormat);

            ottravEvent.endString = endDT.format(dtFormat);

            ottravEvent.url = '/' + otTrav.Id;

            ottravEvent.className = 'event-ottravel';

            events.add(ottravEvent);

        }

//Get my Events if we have selected the correct option

        if(includeMyEvents){

            for(Event evnt: [select Id, Subject, isAllDayEvent, StartDateTime, EndDateTime from Event where OwnerID = :UserInfo.getUserId()]){

                DateTime startDT = evnt.StartDateTime;

                DateTime endDT = evnt.EndDateTime;

                calEvent myEvent = new calEvent();

                

                myEvent.title = evnt.Subject;

                myEvent.allDay = evnt.isAllDayEvent;

                myEvent.startString = startDT.format(dtFormat);

                myEvent.endString = endDT.format(dtFormat);

                myEvent.url = '/' + evnt.Id;

                myEvent.className = 'event-personal';

                events.add(myEvent);

            }

        }

        return null;

    }

    

    public PageReference toggleMyEvents() {

        if(includeMyEvents){

            includeMyEvents = false;

        }

        else{

            includeMyEvents = true;

        }

        pageload();

        return null;

    }


    //Class to hold calendar event data

    public class calEvent{

        public String title {get;set;}

        public Boolean allDay {get;set;}

        public String startString {get;private set;}

        public String endString {get;private set;}

        public String url {get;set;}

        public String className {get;set;}

    }

}
VF Page
<apex:page controller="TravelCalendar_Controller" action="{!pageLoad}">
    
    <apex:relatedList list="TravelCalendar_Controller" />

    <link href="{!$Resource.fullCalendarCSS}" rel="stylesheet" />

    <link href="{!$Resource.fullCalendarPrintCSS}" rel="stylesheet" media="print" />
    
    <apex:includeScript value="{!$Resource.moment_min_js}"  />
    

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

    <script src="{!$Resource.fullCalendarMinJS}"></script>

    

    <script>

        //We need to wrap everything in a doc.ready function so that the code fires after the DOM is loaded

        $(document).ready(function() {   

            //Call the fullCallendar method. You can replace the '#calendar' with the ID of the dom element where you want the calendar to go. 

            $('#calendar').fullCalendar({

                header: {

                    left: 'prev,next today',

                    center: 'title',

                    right: 'month,agendaWeek,agendaDay'

                },

                editable: false,

                events:

                [

                    //At run time, this APEX Repeat will reneder the array elements for the events array

                    <apex:repeat value="{!events}" var="e">

                        {

                            title: "{!e.title}",

                            start: '{!e.startString}',

                            end: '{!e.endString}',

                            url: '{!e.url}',

                            allDay: {!e.allDay},

                            className: '{!e.className}'

                        },

                    </apex:repeat>

                ]

            });

            

        });

    

    </script>

    

    <!--some styling. Modify this to fit your needs-->

    <style>

        #cal-options {float:left;}

        #cal-legend { float:right;}

        #cal-legend ul {margin:0;padding:0;list-style:none;}

        #cal-legend ul li {margin:0;padding:5px;float:left;}

        #cal-legend ul li span {display:block; height:16px; width:16px; margin-right:4px; float:left; border-radius:4px;}

        #calendar {margin-top:20px;}

        #calendar a:hover {color:#fff !important;}

        

        .fc-event-inner {padding:3px;}

        .event-personal {background:#1797c0;border-color:#1797c0;}
        
        .event-mytravel {background:#00447b;border-color:#00447b;}
        
        .event-sbtravel {background:#FCB514;border-color:#FCB514;}
        
        .event-trtravel {background:#00A5DB;border-color:#00A5DB;}
        
        .event-ottravel {background:#676C74;border-color:#676C74;}
        

    </style>

    

    <apex:sectionHeader title="Approved Travel Calendar"/>

    <apex:outputPanel id="calPanel">

        <apex:form >

            <div id="cal-options">

                <apex:commandButton value="{!IF(includeMyEvents,'Hide My Events','Show My Events')}" action="{!toggleMyEvents}"/>

            </div>

            <div id="cal-legend">

                <ul>
                    
                    <li><span class="event-mytravel"></span>My Travel</li>
                    
                    <li><span class="event-sbtravel"></span>School Bus Travel</li>
                    
                    <li><span class="event-trtravel"></span>Transit Travel</li>
                    
                    <li><span class="event-ottravel"></span>Other Travel</li>
                    
                   <li style="{!IF(includeMyEvents,'','display:none')}"><span class="event-personal"></span>My Events</li>

                </ul>

                <div style="clear:both;"><!--fix floats--></div>

            </div>

            <div style="clear:both;"><!--fix floats--></div>

            <div id="calendar"></div>

        </apex:form>

    </apex:outputPanel>

</apex:page>
Calendar
User-added image





 
Hello Everyone ,
                       I am working on Custom Calendar in VisualForce page. I follow  steps from this  http://www.codebycody.com/2013/06/create-calendar-view-in-salesforcecom.html . Everything is doing fine but i dont know how to add new event in custom calendar on click on date.
So any idea how i can add new events ??
My controller
public class CalendarExample_Controller {



    public Boolean includeMyEvents {get;set;}

    public list<calEvent> events {get;set;}

    

    //The calendar plugin is expecting dates is a certain format. We can use this string to get it formated correctly

    String dtFormat = 'yyyy-MM-dd HH:mm:ss';

    

    //constructor

    public CalendarExample_Controller() {

        //Default showing my events to on

        includeMyEvents = true;

    }

    

    public PageReference pageLoad() {

        events = new list<calEvent>();

        //Get Contact's Birthdays

        for(Contact cont : [select Id, Birthdate, FirstName, LastName from Contact where Birthdate != null]){

            //here we need to replace the birth year with the current year so that it will show up on this years calendar

            DateTime startDT = datetime.newInstance(Date.Today().Year(),cont.Birthdate.Month(), cont.Birthdate.Day());

            calEvent bday = new calEvent();

            

            bday.title = cont.FirstName + ' ' + cont.LastName + '\'s Birthday!';

            bday.allDay = true;

            bday.startString = startDT.format(dtFormat);

            //Because this is an all day event that only spans one day, we can leave the send date null

            bday.endString = '';

            bday.url = '/' + cont.Id;

            bday.className = 'event-birthday';

            events.add(bday);

        }

        

        //Get Campaigns

        for(Campaign camp : [select Id, Name, StartDate, EndDate from Campaign where IsActive = true]){

            DateTime startDT = camp.StartDate;

            DateTime endDT = camp.EndDate;

            calEvent campEvent = new calEvent();

            

            campEvent.title = camp.Name;

            campEvent.allDay = true;

            campEvent.startString = startDT.format(dtFormat);

            campEvent.endString = endDT.format(dtFormat);

            campEvent.url = '/' + camp.Id;

            campEvent.className = 'event-campaign';

            events.add(campEvent);

        }

        

        //Get my Events if we have selected the correct option

        if(includeMyEvents){

            for(Event evnt: [select Id, Subject, isAllDayEvent, StartDateTime, EndDateTime from Event where OwnerID = :UserInfo.getUserId()]){

                DateTime startDT = evnt.StartDateTime;

                DateTime endDT = evnt.EndDateTime;

                calEvent myEvent = new calEvent();

                

                myEvent.title = evnt.Subject;

                myEvent.allDay = evnt.isAllDayEvent;

                myEvent.startString = startDT.format(dtFormat);

                myEvent.endString = endDT.format(dtFormat);

                myEvent.url = '/' + evnt.Id;

                myEvent.className = 'event-personal';

                events.add(myEvent);

            }

        }

        return null;

    }

    

    public PageReference toggleMyEvents() {

        if(includeMyEvents){

            includeMyEvents = false;

        }

        else{

            includeMyEvents = true;

        }

        pageload();

        return null;

    }



    

    //Class to hold calendar event data

    public class calEvent{

        public String title {get;set;}

        public Boolean allDay {get;set;}

        public String startString {get;private set;}

        public String endString {get;private set;}

        public String url {get;set;}

        public String className {get;set;}

    }
    }
visualforce page
<apex:page controller="CalendarExample_Controller" action="{!pageLoad}">

    <link href="{!$Resource.fullCalendarCSS}" rel="stylesheet" />

    <link href="{!$Resource.fullCalendarPrintCSS}" rel="stylesheet" media="print" />

    

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

    <script src="{!$Resource.fullCalendarMinJS}"></script>

    <apex:includeScript value="{!URLFOR($Resource.momentmin)}"  />

    <script>

        //We need to wrap everything in a doc.ready function so that the code fires after the DOM is loaded

        $(document).ready(function() {   

            //Call the fullCallendar method. You can replace the '#calendar' with the ID of the dom element where you want the calendar to go. 

            $('#calendar').fullCalendar({
                  
  

                header: {

                    left: 'prev,next today',

                    center: 'title',

                    right: 'month,agendaWeek,agendaDay'

                },

                editable: true,

                events:

                [

                    //At run time, this APEX Repeat will reneder the array elements for the events array

                    <apex:repeat value="{!events}" var="e">

                        {

                            title: "{!e.title}",

                            start: '{!e.startString}',

                            end: '{!e.endString}',

                            url: '{!e.url}',

                            allDay: {!e.allDay},

                            className: '{!e.className}'

                        },

                    </apex:repeat>

                ],
                
                
         
            });

           
        });

    

    </script>

       

    <!--some styling. Modify this to fit your needs-->

    <style>

        #cal-options {float:left;}

        #cal-legend { float:right;}

        #cal-legend ul {margin:0;padding:0;list-style:none;}

        #cal-legend ul li {margin:0;padding:5px;float:left;}

        #cal-legend ul li span {display:block; height:16px; width:16px; margin-right:4px; float:left; border-radius:4px;}

        #calendar {margin-top:20px;}

        #calendar a:hover {color:#fff !important;}

        

        .fc-event-inner {padding:3px;}

        .event-birthday {background:#56458c;border-color:#56458c;}

        .event-campaign {background:#cc9933;border-color:#cc9933;}

        .event-personal {background:#1797c0;border-color:#1797c0;}

    </style>

    

    <apex:sectionHeader title="My Calendar Example"/>

    <apex:outputPanel id="calPanel">

        <apex:form >

            <div id="cal-options">
                 
                <apex:commandButton value="{!IF(includeMyEvents,'Hide My Events','Show My Events')}" action="{!toggleMyEvents}"/>

            </div>

            <div id="cal-legend">

                <ul>

                    <li><span class="event-birthday"></span>Contact's Birthdays</li>

                    <li><span class="event-campaign"></span>Campaigns</li>

                    <li style="{!IF(includeMyEvents,'','display:none')}"><span class="event-personal"></span>My Events</li>

                </ul>

                <div style="clear:both;"><!--fix floats--></div>

            </div>

            <div style="clear:both;"><!--fix floats--></div>

            <div id="calendar"></div>

        </apex:form>

    </apex:outputPanel>

</apex:page>


 

what are the Difference between SOQL & SOSL? Where these commands exactly used in salesforce?

 

 

Please reply.......

 

Regards:

VVNRAO

  • November 23, 2010
  • Like
  • 0
Trailhead, is it possible to follow other users progress?