Sunday, October 25, 2020

Write a batch a in salesforce to get a user last login and last password change datetime on contact custom field.
>>>

global class Batchtoupdatelastloginoncontact implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC){
        DateTime rightNow =  DateTime.now();
        DateTime d24hAgo = rightNow.addHours(-24);
        string query='SELECT ContactId, LastLoginDate, LastPasswordChangeDate, Id FROM User where  ContactId!=NULL and (NumberOfFailedLogins >0 OR LastLoginDate >=:d24hAgo)';
        system.debug('String'+query);
        return Database.getQueryLocator(query);

    }
     global void execute(Database.BatchableContext BC, List<User> userlist) {
         Set<Id> setofuserid=new Set<Id>();
         
         Map<Id,String> mapofstatus=new Map<Id,String>();
         List<LoginHistory> lghlist=[SELECT Id, UserId, LoginTime, Status FROM LoginHistory where UserId in:userlist order by LoginTime desc];
         for(LoginHistory lh:lghlist){
             if (!setofuserid.contains(lh.UserId)){
                 mapofstatus.put(lh.UserId,lh.Status);
             }
             setofuserid.add(lh.UserId);
         }
System.debug('Map:::'+mapofstatus);
         List<Contact> listcontact=new List<Contact>();
         for(User u:userlist){
             Contact c=new Contact();
             c.Id=u.ContactId;
             c.Last_Login__c=u.LastLoginDate;
             c.Last_Rest__c=u.LastPasswordChangeDate;
             c.User_Login_Status__c=mapofstatus.get(u.Id);
             listcontact.add(c);
         }
         system.debug('Contact:::'+listcontact);
             update listcontact;

     }
    global void finish(Database.BatchableContext BC) {

    }

}

Saturday, April 18, 2020

Two object student and school :

1) When student get insert count the number of student for the school also if you update school on student then change the count on both school:

Apex trigger 

trigger StudentCountforschool on Student__c (after insert,after update,after delete) {
    if(Trigger.isinsert){
        Countstudentofschool.countstudent(Trigger.new);
        System.debug('Trigger is after insert');
    }
    else if(Trigger.isupdate){
       Countstudentofschool.countstudentupdate(Trigger.new,Trigger.oldMap);
    }
    else if(Trigger.isdelete){
        Countstudentofschool.countstudentdelete(Trigger.old);
    }

Appex class:


public class Countstudentofschool { //Below code has use aggregateresult to count the student for school on insert public static void countstudent(Student__c[] student){ Set<Id> setOfSchoolId=new Set<Id>(); for(Student__c id:student){ setOfSchoolId.add(id.school__c); } //AggregateResult to get list of student with the count of student related to school List<AggregateResult> AgrList = [Select school__c,count(id) co from Student__c where school__c in:setOfSchoolId GROUP BY school__c]; List<School__c> sch=new List<School__c>(); for(AggregateResult a:AgrList){ School__c sc=new School__c(); sc.id=(Id)a.get('school__c'); //GET the id of school and change the data type to id sc.Count__c=(Integer)a.get('co');//get the count of student and change the data type to Integer sch.add(sc);// add the school into the list } if(sch.size()>0){ update sch; //Check if the size is grater than zero } } //after update below code will update the count on school public static void countstudentupdate(Student__c[] student,Map<Id,Student__c> oldMap){ Set<Id> setOfSchoolId=new Set<Id>(); for(Student__c i:student){ if(i.School__c!=oldMap.get(i.Id).School__c){ setOfSchoolId.add(i.school__c); setOfSchoolId.add(oldMap.get(i.Id).School__c); } } //AggregateResult to get list of student with the count of student related to school List<AggregateResult> AgrList = [Select school__c,count(id) co from Student__c where school__c in:setOfSchoolId GROUP BY school__c]; List<School__c> sch=new List<School__c>(); for(AggregateResult a:AgrList){ School__c sc=new School__c(); sc.id=(Id)a.get('school__c'); //GET the id of school and change the data type to id sc.Count__c=(Integer)a.get('co');//get the count of student and change the data type to Integer sch.add(sc);// add the school into the list } if(sch.size()>0){ update sch; //Check if the size is grater than zero } } //after delete below code will update the count on school public static void countstudentdelete(Student__c[] student){ Set<Id> setofschoolid=new Set<Id>(); for(Student__c s:student){ setofschoolid.add(s.School__c); } List<AggregateResult> listAggre=[Select school__c,count(Id) co from Student__c where school__c in:setofschoolid group by school__c]; List<School__c> listsch=new List<School__c>(); for(AggregateResult a:listAggre){ School__c sch=new School__c(); sch.id=(Id)a.get('school__c'); sch.count__c=(Integer)a.get('co'); listsch.add(sch); } if(listsch.size()>0){ update listsch; } } /* Below code is using inner query for the counting the student for school public static void countstudent(Student__c[] student){ Set<id> setofschoolid=new set<id>(); for(Student__c id:student){ setofschoolid.add(id.school__c); } List<school__c> lists=[Select id,(select id from Students__r) from school__c where Id In:setofschoolid]; System.debug('C value: '+lists); List<School__c> sch=new List<School__c>(); for(school__c i:lists){ School__c s=new School__c(); s.Id=i.Id; s.Count__c=i.Students__r.size(); sch.add(s); } if(sch.size()>0) update sch; }*/ }

Python Wizard Programming

* Course Objective:

1) Go through all the basic of Python Programming and Develop a Basic Game with the help of that.
2) Improve the programming skills

* Course Details:

1) Course Duration will be Three Weeks.
2) Every Week 4 days.
3) Every Day 1 hour.
4) For Wednesday separate task will be assign.
5) On Saturday there will will test.

* Course Fees:

Rs. 1999.

* Contact:

Aakash - 7620788079.

Write a batch a in salesforce to get a user last login and last password change datetime on contact custom field. >>> global class ...