• Mitesh Saparia 8
  • NEWBIE
  • 20 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 4
    Replies
<apex:page standardController="Contact" extensions="datepickerController" docType="html-5.0" >
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
    
    <script>
        $(function () {
            $("#datepicker").datepicker({ 
                format: "dd/mm/yyyy",
                autoclose: true, 
                todayHighlight: true
            }).datepicker().val();
        });
        function getValues(element){
            console.log(element.value);
        }
    </script>
    <apex:pageMessages />
    <apex:sectionHeader title="Date Picker" subtitle="Create Contact" />
    <apex:form id="frm">   
        <apex:pageblock title="Contact" id="pb">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!Cancel}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection id="pbs">
                <apex:inputField value="{!Contact.Salutation}" />
                <apex:inputField value="{!Contact.FirstName}" />
                <apex:inputField value="{!Contact.LastName}" />                
                <apex:inputField value="{!Contact.Birthdate}" />  
                Birthday :--- <input type="text" id="datepicker" onchange = "getValues(this);" value="{!Contact.Birthdate}" />
                <apex:pageBlockSectionItem >
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            
        </apex:pageblock>
    </apex:form>
</apex:page>
trigger conlists on Account (before insert) {
    if(trigger.isInsert && trigger.isAfter)
    {
        List<Contact> lstCon = new List<Contact>();
        for(Account acc : trigger.new)
        {
            Contact con = new Contact();
            con.lastName=acc.Name;
            con.AccountId = acc.Id;
            lstCon.add(con);
        }
        insert(lstCon);
    }
    if(trigger.isUpdate && trigger.isAfter)
    {
        Map<Id,Account> mapACC = new Map<Id,Account>([select (select id,name from contacts)
                                                      from account where id IN: trigger.new]);
        List<Contact> newCon = new List<Contact>();
        for(Account updatedAcc : trigger.New)
        {
            Account oldAcc = trigger.oldMap.get(updatedAcc.Id);
            if(mapACC.ContainsKEY(updatedAcc.Id) && oldAcc.Name != updatedAcc.Name)
            {
                for(Contact con : mapACC.get(updatedAcc.Id).contacts)
                {
                    con.lastname = updatedAcc.Name;
                    newCon.add(con);
                }
            }
        }
        update newCon;
    }
}
trigger ProductDetail on Design_Selected_Junction__c (After insert) {
    if(Trigger.isInsert){
        Set<Id> designTemplateSet = new Set<Id>();
        Set<Id> oppSet = new Set<Id>();
        for(Design_Selected_Junction__c desiSele : trigger.new){
            designTemplateSet.add(desiSele.Design_Template__c);  
            oppSet.add(desiSele.Opportunity__c);  
            //System.debug('ID --->' +designTemplateSet);
            //System.debug('ID --->' +oppSet);
        }
        // sObject types to describe
        Map<String, Schema.SObjectField> fields = Schema.getGlobalDescribe().get('Design_Item__c').getDescribe().fields.getMap();
        List<String> listFields = new List<String>();
        for(Schema.SObjectField fieldRef : fields.values()){
            Schema.DescribeFieldResult parentfieldResult = fieldRef.getDescribe();
            if(parentfieldResult.isUpdateable()) {
                listFields.add(parentfieldResult.getName());
            }
        }
        String query = '';
        query =  'Select Id,' + String.join(listFields,',') + ',';
        System.debug('Query --->' +query);  
        String dynamicQuery = '';
        List<String> fieldNames;
        Set<String> childString = new Set<String>{
        'Handles__r','Prints__r','Linings__r','Closures__r','Inserts__r','Patches__r','Insulation_Padding__r','Tags__r'
        };
        String childStringQuery = '';
        String finalQuery = '';
        for(ChildRelationship childRel : Schema.getGlobalDescribe().get('Design_Item__c').getDescribe().getChildRelationships()){                       
            if(childString.contains(childRel.getRelationshipName())){
                DescribeSObjectResult describeResult = childRel.getChildSObject().getDescribe();
                System.debug('Child Relationship Name --->' +childRel.getRelationshipName());
                Map <String, Schema.SObjectField> fieldsMap = describeResult.fields.getMap();
                fieldNames = new List<String>();
                for(Schema.SObjectField sObjectField : fieldsMap.values()){
                    Schema.DescribeFieldResult fieldResult = sObjectField.getDescribe();                    if(fieldResult.isUpdateable()){
                        fieldNames.add(fieldResult.getName());
                    }
                    dynamicQuery +='(select Id,' + String.join(fieldNames, ',')
                     + ' from ' + String.valueOf(childRel.getRelationshipName())+'),'; 
                }
            }
        }
        dynamicQuery = dynamicQuery.removeEnd(',');
        childStringQuery += dynamicQuery;
        System.debug('Check dynamic ---> '+childStringQuery);
        String condition = 'Where Parent_template__c In:designTemplateSet'; 
        query += query + childStringQuery+' FROM Design_Item__c '+ condition;    
        //String finalQuery = query +','+ dynamicQuery +' from  Design_Item__c '+ condition;
        System.debug('Check final ---> '+query);
        List<Design_Item__c> designRecords = Database.query(finalQuery);
        System.debug('Check List ---> '+designRecords);
    }
}
trigger conlists on Account (before insert) {
    if(trigger.isInsert && trigger.isAfter)
    {
        List<Contact> lstCon = new List<Contact>();
        for(Account acc : trigger.new)
        {
            Contact con = new Contact();
            con.lastName=acc.Name;
            con.AccountId = acc.Id;
            lstCon.add(con);
        }
        insert(lstCon);
    }
    if(trigger.isUpdate && trigger.isAfter)
    {
        Map<Id,Account> mapACC = new Map<Id,Account>([select (select id,name from contacts)
                                                      from account where id IN: trigger.new]);
        List<Contact> newCon = new List<Contact>();
        for(Account updatedAcc : trigger.New)
        {
            Account oldAcc = trigger.oldMap.get(updatedAcc.Id);
            if(mapACC.ContainsKEY(updatedAcc.Id) && oldAcc.Name != updatedAcc.Name)
            {
                for(Contact con : mapACC.get(updatedAcc.Id).contacts)
                {
                    con.lastname = updatedAcc.Name;
                    newCon.add(con);
                }
            }
        }
        update newCon;
    }
}
trigger ProductDetail on Design_Selected_Junction__c (After insert) {
    if(Trigger.isInsert){
        Set<Id> designTemplateSet = new Set<Id>();
        Set<Id> oppSet = new Set<Id>();
        for(Design_Selected_Junction__c desiSele : trigger.new){
            designTemplateSet.add(desiSele.Design_Template__c);  
            oppSet.add(desiSele.Opportunity__c);  
            //System.debug('ID --->' +designTemplateSet);
            //System.debug('ID --->' +oppSet);
        }
        // sObject types to describe
        Map<String, Schema.SObjectField> fields = Schema.getGlobalDescribe().get('Design_Item__c').getDescribe().fields.getMap();
        List<String> listFields = new List<String>();
        for(Schema.SObjectField fieldRef : fields.values()){
            Schema.DescribeFieldResult parentfieldResult = fieldRef.getDescribe();
            if(parentfieldResult.isUpdateable()) {
                listFields.add(parentfieldResult.getName());
            }
        }
        String query = '';
        query =  'Select Id,' + String.join(listFields,',') + ',';
        System.debug('Query --->' +query);  
        String dynamicQuery = '';
        List<String> fieldNames;
        Set<String> childString = new Set<String>{
        'Handles__r','Prints__r','Linings__r','Closures__r','Inserts__r','Patches__r','Insulation_Padding__r','Tags__r'
        };
        String childStringQuery = '';
        String finalQuery = '';
        for(ChildRelationship childRel : Schema.getGlobalDescribe().get('Design_Item__c').getDescribe().getChildRelationships()){                       
            if(childString.contains(childRel.getRelationshipName())){
                DescribeSObjectResult describeResult = childRel.getChildSObject().getDescribe();
                System.debug('Child Relationship Name --->' +childRel.getRelationshipName());
                Map <String, Schema.SObjectField> fieldsMap = describeResult.fields.getMap();
                fieldNames = new List<String>();
                for(Schema.SObjectField sObjectField : fieldsMap.values()){
                    Schema.DescribeFieldResult fieldResult = sObjectField.getDescribe();                    if(fieldResult.isUpdateable()){
                        fieldNames.add(fieldResult.getName());
                    }
                    dynamicQuery +='(select Id,' + String.join(fieldNames, ',')
                     + ' from ' + String.valueOf(childRel.getRelationshipName())+'),'; 
                }
            }
        }
        dynamicQuery = dynamicQuery.removeEnd(',');
        childStringQuery += dynamicQuery;
        System.debug('Check dynamic ---> '+childStringQuery);
        String condition = 'Where Parent_template__c In:designTemplateSet'; 
        query += query + childStringQuery+' FROM Design_Item__c '+ condition;    
        //String finalQuery = query +','+ dynamicQuery +' from  Design_Item__c '+ condition;
        System.debug('Check final ---> '+query);
        List<Design_Item__c> designRecords = Database.query(finalQuery);
        System.debug('Check List ---> '+designRecords);
    }
}