function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anil SavaliyaAnil Savaliya 

How to Create Batch Apex,I am new in Batch Apex coding?

I have requirment to make callout from trigger ,Because limitation ,We can't  write call out in trigger, so I writed future method for call out,But I am facing now callout limit issue,Do you any body have code,I can make batch for call out . here is my code.

Calling from Trigger Below method:
/*********************Trigger**************************/
trigger contactFollow_checkEntitySubscription on Contacts_Followed_By__c(after insert, after delete, after update) {
if(Trigger.isInsert || Trigger.isUpdate){
        for(Contacts_Followed_By__c  contactFollow: Trigger.new){
  
            contactFollowIdSet.add(contactFollow.Id);
           
        }
--------------------------
------
AddAndDeleteRelSciController.RecordToGetAddRelSci(contactFollowIdSet);
}

/***********Class *********/

Public Class AddAndDeleteRelSciController{
Public static String JSONstring;
Public static Map<String, List<String>> userContactFollowMap = new Map<String, List<String>>();

Public Static  void RecordToGetAddRelSci(Set<Id> contactFollowersIdSet){
    //query all the user fields from the contactFollowersIdSet
    List<Contacts_Followed_By__c> conFollowsList = [Select Id, User__c, User__r.Name, User__r.FederationIdentifier, Contact__c, Contact__r.rels__EntityId__c
                                                   FROM Contacts_Followed_By__c  WHERE Id IN :contactFollowersIdSet];
                           
    for(Contacts_Followed_By__c conFollower : conFollowsList)
    {
        List<String> entityIdLIst = new List<String>();
        if(userContactFollowMap.containsKey(conFollower.User__R.FederationIdentifier))
        {
            entityIdLIst = userContactFollowMap.get(conFollower.User__R.FederationIdentifier);
        }
        entityIdLIst.add(conFollower.Contact__r.rels__EntityId__c);
        userContactFollowMap.put(conFollower.User__R.FederationIdentifier, entityIdLIst);
        System.debug('*************** ' +userContactFollowMap);
        AddRelSci(conFollower.User__R.FederationIdentifier, entityIdLIst);
       
    }
}

@Future (callout=True)
Public Static  void AddRelSci(String UsrName,List<String> Cntc){
    Try{ System.debug('*****Final Test****'+UsrName+'---'+ Cntc) ;
        If(UsrName != Null & !Cntc.isEmpty() ){
           
            integer i;
            String endpointUrl =  Relationship_API_Setting__c.getValues('RelSciAPI').Add_Method_EndPointUrl__c;
            String username = Relationship_API_Setting__c.getValues('RelSciAPI').UserName__c;
            String password = Relationship_API_Setting__c.getValues('RelSciAPI').Password__c;
            HttpRequest req = new HttpRequest();
            req.setMethod('POST');
            req.setHeader('Username', Username);
            req.setHeader('password', password);
            req.setHeader('UserIdentifier',UsrName);
            req.setHeader('Content-Type', 'application/json');
            req.setHeader('Accept','application/json');
            req.setEndpoint(endpointUrl);
            String JS = '[{EntityId: ';
            integer S ;
            for(S=0;Cntc.size()>S;S++){
                JS = JS+''+Cntc[S]+'},{EntityId: ';
            }
            JSONstring = JS.SubString(0,JS.length()-12)+']';
            System.debug('*******Add JSON String*******'+JSONstring);
            req.setBody(JS);
            If(Relationship_API_Setting__c.getValues('RelSciAPI').RelSci_API_is_Available__c){
                Http http = new Http();
                HttpResponse resp = http.send(req);
                system.debug('*******Add Status Response*******'+resp.getStatus());
                system.debug('*******Add Body Response*******'+resp.getBody());
                If(resp.getStatus() != 'OK'){
                    Exception__c ex= new Exception__c();  
                    ex.Erro_occur_for_user__c = UsrName ;
                    ex.Error_Occured_for_EntityID__c = JSONstring;
                    ex.Error_Code__c = string.valueof(resp.getStatusCode());
                    ex.Error_Status__c = resp.getStatus();
                    ex.Error_Message__c = resp.getBody();
                    ex.Error_Module__c = 'Relationship Science';
                    ex.Error_Generated_From_Method__c = 'ADD';
                    insert ex;
                }
            }
            Else{
                Exception__c ex= new Exception__c();  
                ex.Erro_occur_for_user__c = UsrName ;
                ex.Error_Occured_for_EntityID__c = JSONstring;
                ex.Error_Code__c = '001';
                ex.Error_Status__c = 'RelSci API is Down';
                ex.Error_Message__c = 'RelSci API is Down';
                ex.Error_Module__c = 'Relationship Science';
                ex.Error_Generated_From_Method__c = 'ADD';
                insert ex;
            }
        }
    }
    Catch (Exception E)
    {
        Exception__c ex= new Exception__c();
        ex.Erro_occur_for_user__c = UsrName ;
        ex.Error_Occured_for_EntityID__c = JSONstring;
        ex.Error_Code__c = '000';
        ex.Error_Status__c = 'Unexpected Exception';
        ex.Error_Message__c = E.getMessage();
        ex.Error_Module__c = 'Relationship Science';
        ex.Error_Generated_From_Method__c = 'ADD';
        insert ex;
    }
}

}

How can method from trigger in form batch,So i can't hit limit 10 call out ?
Ramu_SFDCRamu_SFDC
see if this helps

http://salesforce.stackexchange.com/questions/22916/scheduled-apex-callouts-and-running-a-batch