• Abhinav_SFDC
  • NEWBIE
  • 0 Points
  • Member since 2014
  • Salesforce Developer


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 1
    Replies
i am new in coding .
   
    global class Batch_InvoiceCreation implements Database.Batchable<sObject>,Schedulable {
    global String query;
    global Batch_InvoiceCreation() {
   
    }
    global void execute(SchedulableContext sc){
    Database.executebatch(this);
    }
   
    global Database.QueryLocator start(Database.BatchableContext BC) {
   
    date d = (date)date.today().addDays(30);
   
    query='SELECT Account__c,Ageing__c,AmountAdjusted__c,AmountSettled__c,Amount__c,'+
    'Applicable_Tax__c,Bad_Debts__c,Contracts_Billing_Entity__c,Contracts_Country__c,Contract__c,Conversion_Rate_To_USD__c,'+
    'Conversion_Rate__c,CreatedById,CreatedDate,Credit_Days__c,Currency__c,Frequency__c,From__c,InvoiceDate__c,InvoiceNumber__c,'+
    'Invoice_Amount_in_INR__c,Invoice_Amount__c,Invoice_Billing_Entity__c,Invoice_Cancelled__c,Invoice_Completed_Date_Reference__c,'+
    'Invoice_Completed_Date__c,Invoice_Due_Date_Reference__c,Invoice_Due_Date__c,Invoi__c,IsDeleted,LastActivityDate,LastModifiedById,'+
    'LastModifiedDate,LastReferencedDate,LastViewedDate,Last_Activity_Description__c,Monthsbtw_inv__c,Month__c,Name,No_of_of_months__c,'+
    'Opportunity__c,Outstanding_Amount__c,OwnerId,Payment_Status__c,Product_Purchased__c,Property_Address__c,Property_Name__c,'+
    'Reason_for_Cancellation__c,Sales_Owner__c,Sales_Price_in_INR__c,Settlement_Count__c,Status_Reference__c,Status__c,'+
    'Subscritption_end_date__c,Subscritption_start_date__c,SystemModstamp,Total_Amount_in_INR_formula__c,Batch_Invoice_Number__c,Is_Auto_Invoice__c,'+
    'Total_Amount_in_USD_formula__c,Total_Amount_in_USD__c,Total_Amount__c,To__c,Type__c,Vertical__c,Year__c FROM Invoice__c WHERE (Frequency__c =\'Yearly\' OR  Frequency__c =\'Quarterly\' OR  Frequency__c =\'Half yearly\') AND '+
    'Contract__r.Auto_Renewal__c =\'Yes\' AND Vertical__c=\'Hospi\'';
    query=query+'AND To__c='+string.valueof(d).substring(0,10);//NEXT_MONTHAND Type__c=\'Fresh\' ';
    //Quarterly
    //Half yearly
    system.debug('TEST 2------------->'+query);
    system.debug('TEST BP------------->'+Database.getQueryLocator(query));
    return Database.getQueryLocator(query);
   
   
    }
   
    global void execute(Database.BatchableContext BC, List<Invoice__c> scope) {
    List<Invoice__c> listInvoiceClone = (List<Invoice__c>)scope;
    List<Invoice__c> listInsertRecords= new List<Invoice__c>() ;
    List<Invoice__c> listInsertRecordCl= new List<Invoice__c>() ;
    List<Invoice__c> listUpdatRecords= new List<Invoice__c>() ;
    Invoice__c inCreate= new Invoice__c() ;
    listInsertRecordCl=listInvoiceClone.deepClone();
    set<string>   setInvoicId= new set<string>();
   
    Map<string ,List<description__c>> MapDescrpt = new Map<string, List<description__c>>();
   
    for(Invoice__c inObj:listInvoiceClone) {
   
    setInvoicId.add(inObj.id);
   
    }
   
    List<Description__c> ListDescription=[select Id,Description__c,From__c,Invoice__c,Invoice__r.name,Sr_No__c,To__c,Amount__c from Description__c where Invoice__c IN: setInvoicId];
   
    for(Description__c desObj:ListDescription) {
    if(MapDescrpt.containskey(desObj.Invoice__r.name)) {
    MapDescrpt.get(desObj.Invoice__r.name).add(desObj);
    }
    else {
    MapDescrpt.put(desObj.Invoice__r.name,new List<Description__c>());
    MapDescrpt.get(desObj.Invoice__r.name).add(desObj);
    }

   
    }
    system.debug('listInsertRecordCl.size()'+listInsertRecordCl.size());
    system.debug('MapDescrpt)'+MapDescrpt);
    for(integer j=0;j<listInsertRecordCl.size();j++ ) {
   
    if(listInsertRecordCl[j].Frequency__c=='Yearly') {
    listInsertRecordCl[j].To__c =listInsertRecordCl[j].To__c.addDays(365);
    listInsertRecordCl[j].From__c =listInsertRecordCl[j].From__c.addDays(365);
    }
   
    else if(listInsertRecordCl[j].Frequency__c=='Half yearly') {
    listInsertRecordCl[j].To__c =listInsertRecordCl[j].To__c.addDays(182);
    listInsertRecordCl[j].From__c =listInsertRecordCl[j].From__c.addDays(182);
    }
   
    else if(listInsertRecordCl[j].Frequency__c=='Quarterly') {
    listInsertRecordCl[j].To__c =listInsertRecordCl[j].To__c.addDays(91);
    listInsertRecordCl[j].From__c =listInsertRecordCl[j].From__c.addDays(91);
    }
   
    listInsertRecordCl[j].InvoiceNumber__c = listInsertRecordCl[j].InvoiceNumber__c+j;
    listInsertRecordCl[j].Batch_Invoice_Number__c =listInvoiceClone[j].Name;
    //listInsertRecordCl[j].Is_Auto_Invoice__c =true;
    listInsertRecordCl[j].InvoiceDate__c =system.Today();
    listInsertRecordCl[j].Type__c ='Renewal';
    listInsertRecords.add(listInsertRecordCl[j]);
   
    system.debug('TESTqqq------->'+listInsertRecords);
    }
   
    List<Description__c>  listInvoceDescriptionInst= new List<Description__c> ();
    if(listInsertRecords.size() > 0) {
    system.debug('TEST------->'+listInsertRecords);
    insert listInsertRecords;
   
        for(Invoice__c inObj:listInsertRecords) {
            if(MapDescrpt.containskey(inObj.Batch_Invoice_Number__c)) {
                for(Description__c desObj:MapDescrpt.get(inObj.Batch_Invoice_Number__c)) {
                    if(!desObj.Description__c.contains('Setup Fee')) {
                   
                        Description__c DesObj1 = new  Description__c();
                        DesObj1.Description__c =desObj.Description__c;
                        DesObj1.Invoice__c=inObj.id;
                        DesObj1.Amount__c=desObj.Amount__c;
                        DesObj1.Sr_No__c =desObj.Sr_No__c;
                        listInvoceDescriptionInst.add(DesObj1);

                    }
               
                }
           
            }
       
        }
    system.debug('TEST GAU------>'+listInvoceDescriptionInst);
        if(listInvoceDescriptionInst.size() > 0) {
            insert  listInvoceDescriptionInst;
            for(Invoice__c inObj1:listInsertRecords) {
                inObj1.Is_Auto_Invoice__c =true;
                listUpdatRecords.add(inObj1);
            }
            if(listUpdatRecords.size() > 0) {
                update  listUpdatRecords;

            }
        }
       
    }
  
    }
   
    global void finish(Database.BatchableContext BC) {
   
    }
   
    }
=============================================================================================================
this is my test class===>

@isTest
private class Test_InvoiceUpdate {
static TestMethod void Test_BatchInvoiceCreation() {
    Database.QueryLocator QL;
    Database.BatchableContext BC;
    SchedulableContext SC;
    List<Invoice__c> InvoiceList = new List<Invoice__c>();
    List<Description__c> DesList=new List<Description__c>();
    Batch_InvoiceCreation  AU = new Batch_InvoiceCreation ();
    QL = AU.start(bc);
    Database.QueryLocatorIterator QIT =  QL.iterator();
   
   
    AU.execute(SC);
    AU.finish(BC);
   
    Account acc=new Account ();
    acc.name='DummyTest';
    Insert acc;
   
    Contract Contr=new Contract();
    Contr.AccountId=acc.Id;
    Contr.Status='Draft';
    Insert Contr;
   
    Invoice__c Invc=new Invoice__c();
    Invc.Contract__c=Contr.id;
    Invc.Vertical__c='PS';
    Invc.InvoiceDate__c=system.today();
    Invc.Credit_Days__c=10;
    Invc.Currency__c='USD';
    Invc.Frequency__c='Yearly';
    Invc.Conversion_Rate__c=100;
    Invc.Is_Auto_Invoice__c = true;
    Invc.Conversion_Rate_To_USD__c=1.000000;
    Invc.Type__c='Fresh';
    Invc.Property_Name__c='TestMon';
    Invc.From__c=system.today()-20;
    Invc.To__c=system.today();
    insert Invc;
    System.debug('Invc-------->'+Invc);
    InvoiceList.add(Invc);
    AU.execute(BC, InvoiceList );
    System.debug('InvoiceList-------->'+InvoiceList);
   
    Invoice__c Invc1=new Invoice__c();
    Invc1.Contract__c=Contr.id;
    Invc1.Vertical__c='PS';
    Invc1.InvoiceDate__c=system.today();
    Invc1.Credit_Days__c=10;
    Invc1.Currency__c='USD';
    Invc1.Batch_Invoice_Number__c = 'abhi 01';
    Invc1.Frequency__c='Half yearly';
    Invc1.Conversion_Rate__c=100;
    Invc1.Is_Auto_Invoice__c = true;
    Invc1.Conversion_Rate_To_USD__c=1.000000;
    Invc1.Type__c='Fresh';
    Invc1.Property_Name__c='TestMon';
    Invc1.From__c=system.today()-20;
    Invc1.To__c=system.today();
    insert Invc1;
    System.debug('Invc-------->'+Invc);
    InvoiceList.add(Invc1);
    AU.execute(BC, InvoiceList );
    System.debug('InvoiceList-------->'+InvoiceList);
   
    Invoice__c Invc2=new Invoice__c();
    Invc2.Contract__c=Contr.id;
    Invc2.Vertical__c='PS';
    Invc2.InvoiceDate__c=system.today();
    Invc2.Credit_Days__c=10;
    Invc2.Currency__c='USD';
    Invc2.Frequency__c='Quarterly';
    Invc2.Conversion_Rate__c=100;
    Invc2.Conversion_Rate_To_USD__c=1.000000;
    Invc2.Type__c='Fresh';
    Invc2.Is_Auto_Invoice__c = true;   
    Invc2.Property_Name__c='TestMon';
    Invc2.From__c=system.today()-20;
    Invc2.To__c=system.today();
    insert Invc2;
    System.debug('Invc-------->'+Invc);
    InvoiceList.add(Invc2);
    AU.execute(BC, InvoiceList );
    System.debug('InvoiceList-------->'+InvoiceList);
   
    list<Invoice__c> i = [select id, name from Invoice__c where id = :InvoiceList];
   
    Description__c DesObj=new Description__c();
    DesObj.Invoice__c=Invc.id;
    DesObj.Description__c = 'Setup fee';
    DesObj.Sr_No__c=10;
    Insert DesObj;
    DesList.add(DesObj);
    System.debug('DesList-------->'+DesList);
   
    Description__c DesObj2=new Description__c();
    DesObj2.Invoice__c=Invc2.id;
    DesObj2.Description__c = 'Half yearly subscription';
    DesObj2.Sr_No__c=10;
    DesObj2.Amount__c=1234.00;
    Insert DesObj2;
    DesList.add(DesObj2);
    System.debug('DesList-------->'+DesList);
   
    Description__c DesObj1=new Description__c();
    DesObj1.Invoice__c=Invc1.id;
    DesObj1.Description__c = 'Half yearly subscription';
    DesObj1.Sr_No__c=10;
    DesObj1.Amount__c=1234.00;
    Insert DesObj1;
    DesList.add(DesObj1);
    System.debug('DesList-------->'+DesList);
   
   
    Test.startTest();
    Batch_InvoiceCreation  BatchInv=new Batch_InvoiceCreation ();
    ID batchprocessid = Database.executeBatch(BatchInv);
    Test.stopTest();

}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
trigger autoContact on Account (after insert) {
   
    List<contact> newContact = new List<contact>();
   
  
    for(Account acc: Trigger.new){
      
            Contact con = new Contact();
            con.FirstName = acc.Name;
            con.LastName = acc.Short_Name__c;
            con.Phone = acc.Phone;
            con.salutation = 'Mr.';
            con.AccountId = acc.id;
            newContact.add(con);
      }
           insert newContact;
     }
====================================================================

@isTest
Public Class autoContactTest{
    Static testmethod void MethodOne(){
        Account a = new Account();
        a.Name = 'ABVP';
        a.Phone = '0000000000';
        a.Short_Name__c = 'Abi';
       
        insert a;
       
        Contact c = new Contact();
        c.FirstName = 'ABVP';
        c.LastName = 'Abi';
        c.Phone = '0000000000';
        c.AccountId = '0019000000tgMdl';
        insert c;
          }
}

thanks in adv...:)
trigger Sample on Record_Type__c(before insert){
    String[] names = new String[]{'First Record','Second Record','Third Record'};
    Map<Id, RecordType> regions = new Map<Id,RecordType>([SELECT Name from RecordType WHERE SobjectType = 'Record_Type__c' AND Name IN :names]);
    AggregateResult res = [SELECT MAX(Record_Number__c) max1, MAX(Record_Number1__c) max2,MAX(Record_Number2__c) max3 FROM Record_Type__c  WHERE RecordType.Name IN :names group by RecordTypeId];

    Integer max1 = Integer.valueOf(((String)res.get('max1')).right(3) );
    Integer max2 = Integer.valueOf(((String)res.get('max2')).right(3) );
    Integer max3 = Integer.valueOf(((String)res.get('max3')).right(3) );

    for(Record_Type__c theRT: trigger.new){
       

            if( regions.get(theRT.RecordTypeId).Name == 'First Record'){   //region corresponds to 'US'
                String num1 = String.valueOf( (max1 != null) ? ++max1 : 1 );//will use the incremented value of max unless max was null (else use 1 instead)
                for(Integer i=0; i< (3 - num1.length() ); i++){ //should pad with leading zeros to ensure format of {000}
                    num1 = '0' + num1;
                }
                theRT.Record_Number__c= 'A - ' + num1;
            }
            else if( regions.get(theRT.RecordTypeId).Name == 'Second Record'){    //region corresponds to 'ROW'
                String num2 = String.valueOf( (max2 != null) ? ++max2 : 1 );
                for(Integer i=0; i< (3 - num2.length() ); i++){ //should pad with leading zeros to ensure format of {000}
                    num2 = '0' + num2;
                }
                theRT.Record_Number1__c= 'S - ' + num2;
            }
            else if( regions.get(theRT.RecordTypeId).Name == 'Third Record'){    //region corresponds to 'ROW'
                String num3 = String.valueOf( (max3 != null) ? ++max3 : 1 );
                for(Integer i=0; i< (3 - num3.length() ); i++){ //should pad with leading zeros to ensure format of {000}
                    num3 = '0' + num3;
                }
                theRT.Record_Number2__c= 'T - ' + num3;
            }
       
    }
}
==========================================================================================
when i create an record it shows an error====
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Sample caused an unexpected exception, contact your administrator: Sample: execution of BeforeInsert caused by: System.QueryException: List has more than 1 row for assignment to SObject: Trigger.Sample: line 4, column 1
RecordNumber: execution of AfterInsert caused by: System.NullPointerException: Argument cannot be null.: Trigger.RecordNumber: line 12, column 1



trigger RecordNumber on Record_Type__c (after insert) {
    List<Record_Type__c> rt = new List<Record_Type__c> ([Select id, name,RecordTypeid, Record_Number__c from Record_Type__c order by  Record_Number__c ]);
    String startNum = '00000';
   
    if(rt != null){
        startNum = rt.get(0).Record_Number__c ;
        startNum = startNum.substring(0,8);
        }   
    for(Record_Type__c r : Trigger.new){
        if(r.RecordTypeid == '01290000000TQfL'){
            string word1 = 'F';
             startNum = String.valueOf(Integer.valueOf(startNum) + 1);
             startNum = '00000'.substring(0, 5-startNum.length()) + startNum ;
             r.Record_Number__c = Word1 + startNum ; 
            }
            else if(r.RecordTypeid == '01290000000TQff'){
                string word2 = 'S';
                startNum = String.valueOf(Integer.valueOf(startNum) + 1);
                startNum = '00000'.substring(0, 5-startNum.length()) + startNum ;
                r.Record_Number__c = Word2 + startNum;
            }
             else if(r.RecordTypeid == '01290000000TQfa'){
                string word3 = 'T';
                startNum = String.valueOf(Integer.valueOf(startNum) + 1);
                startNum = '00000'.substring(0, 5-startNum.length()) + startNum ;
                r.Record_Number__c = Word3 + startNum;
             }
    }
}


where Record_Number__c is a text field