<apex:page controller="TriggerCreator" sidebar="false" tabStyle="TriggerCreator__tab">
<div style="float:right" >
<apex:image value="{!$Resource.TriggerCreatorLogo}" />
</div>
<div >
<apex:form id="fm" >
<apex:pageBlock id="pg" >
<h1>Enter Trigger Details </h1><br/><br/>
<apex:panelGrid columns="2" >
<apex:outputLabel styleClass="outputLabel" value="Trigger Name:"/>
<apex:inputText styleClass="inputText" value="{!TrName}"/>
<apex:outputLabel styleClass="outputLabel" value="Object Name:" />
<!-- <apex:inputText styleClass="inputText" value="{!ObjName}" id="ObjName"/> -->
<apex:actionRegion >
<apex:selectList value="{!selectedObject}" size="1">
<apex:selectOptions value="{!ObjectNames}"/>
</apex:selectList>
</apex:actionRegion>
<apex:outputLabel styleClass="outputLabel" value="Trigger Event:"/>
<apex:selectList value="{!type}" multiselect="true" >
<apex:selectOption itemValue="Before Insert" itemLabel="Before Insert"/>
<apex:selectOption itemValue="After Insert" itemLabel="After Insert"/>
<apex:selectOption itemValue="Before Update" itemLabel="Before Update"/>
<apex:selectOption itemValue="After Update" itemLabel="After Update"/>
<apex:selectOption itemValue="Before delete" itemLabel="Before delete"/>
<apex:selectOption itemValue="After delete" itemLabel="After delete"/>
<apex:selectOption itemValue="after Undelete" itemLabel="After Undelete"/>
</apex:selectList>
</apex:panelGrid>
<div style="padding-left:105px;" >
<apex:commandButton style="background:black;width:150px;height:40px;color:white;font-size:12pt;" action="{!TriggerCreator}" oncomplete="Showoptions('{!str}','{!type}','{!TrName}');fun('{!str}','{!tid}','{!type}','{!TrName}');" value="Create Trigger" status="actStatusId" rerender="fm"/>
<apex:actionStatus id="actStatusId" >
<apex:facet name="start" >
<img src="{!$Resource.Searching}" />
</apex:facet>
</apex:actionStatus>
</div>
<apex:pageMessages ></apex:pageMessages>
</apex:pageBlock>
<p id="options" ></p>
<p id="refresh" ></p>
</apex:form>
</div>
<script type="text/javascript">
function Showoptions(str,tp,tnam){
if(!str.includes('Invalid')&&!str.includes('DUPLICATE')&&!str.includes('UNKNOWN')&&tp!='[]'&&tnam!=''&&/^[a-zA-Z0-9- ]*$/.test(tnam)){
<!-- document.getElementById("options").innerHTML='<h2>To edit the Trigger </h2><a style="text-decoration: none;" href="javascript:fun(dd);"><h3>clickhere</h3></a>'; -->
document.getElementById("refresh").innerHTML='<h2>To Create New Trigger </h2><a style="text-decoration: none;" href="javascript:refresh();"><h3>clickhere</h3></a>';
}
}
</script>
<script>
function refresh() {
window.location.reload();
}
</script>
<script>
function fun(st,eid,tp,tnam){
if(!st.includes('Invalid')&&!st.includes('DUPLICATE')&&!st.includes('UNKNOWN')&&tp!='[]'&&tnam!=''&&/^[a-zA-Z0-9- ]*$/.test(tnam)){
if(confirm("Press OK to Edit the Created Trigger"))
window.open("/"+eid+"/e?retURL=%2Fsetup%2Fbuild%2FviewApexTrigger.apexp%3Fid%3D"+eid+"%26id%3D"+eid);
}
}
</script>
<style>
h1 {
color:#04cef7;
width:150px;
clear:left;
font-size:15pt;
}
.outputLabel{
width:100px;
clear:left;
text-align:right;
font-weight:bold;
}
.inputText, .outputLabel{
float:left;
}
h2 {
font-weight:bold;
font-size:20px;
color:#04cef7;
clear: left;
text-align:center;
width:500px;
}
a,h3{color:#0655d3;
font-weight:bold;
font-size:18px;
text-decoration: none;}
</style>
</apex:page>
public class TriggerCreator {
public String type { get; set; }
public String ObjName { get; set; }
public String TrName { get; set; }
Public String str {get;set;}
public string tid {get;set;}
public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public String selectedObject {get; set;}
public String selectedField {get; set;}
Public void TriggerCreator(){
map<string,string> mapinfo= new map<string,string>();
system.debug('++++++++++++++++++'+type);
// string patte = '/^[a-zA-Z0-9_]+$/';
if(TrName=='' ||!Pattern.matches('[a-zA-Z0-9_]+',TrName)){ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please Enter Valid TriggerName.'));}
else if(selectedObject ==''){ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please Enter ObjectName.'));}
else if(type=='[]'){ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select Event.'));}
else{
mapinfo.put('Name',TrName);
mapinfo.put('TableEnumOrId',selectedObject);
string type1=type.remove('[');
string type2=type1.remove(']');
mapinfo.put('Body','trigger '+ TrName + ' on '+ selectedObject +' ('+type2+'){ }');
String serialized = JSON.serialize(mapinfo);
Httprequest req = new HttpRequest();
req.setEndpoint('https://ghattamaneni-chaithanya-dev-ed.my.salesforce.com/services/data/v27.0/sobjects/ApexTrigger'); // Enter the org.
req.setMethod('POST');
req.setHeader('Content-Type','application/json');
req.setHeader('Authorization','Bearer '+UserInfo.getSessionID());
req.setBody(serialized);
Http httpReq = new Http();
HttpResponse res = httpReq.send(req);
System.debug('>>>>>>>>'+res.getBody());
str=string.valueof(res.getBody());
system.debug('-----------'+str);
if(str.contains('true')){ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM, 'Trigger Created Successfully!'));
string rstr=str.remove('"');
string[] strs=rstr.split(':');
string[] fstr=strs[1].split(',');
tid=fstr[0];
system.debug('.................'+tid);
}
else if(str.contains('INVALID')){ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Error Occured Please Check The Trigger Name OR Object Name and Try again'));}
else if(str.contains('DUPLICATE')){ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Trigger with this name is already Exist Please Enter different Name'));}
else if(str.contains('UNKNOWN')){ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Error Occured Please Check Trigger Name and Try again'));}
}
}
public List<SelectOption> getObjectNames()
{
List<SelectOption> objNames = new List<SelectOption>();
List<String> entities = new List<String>(schemaMap.keySet());
entities.sort();
////////////////////////////////
Schema.DescribeSobjectResult[] results = Schema.describeSObjects(entities );
for (DescribeSObjectResult result : results){
if(result.isUndeletable())
objNames.add(new SelectOption(result.getName(),result.getName()));
}
/* for(String name : entities)
{
if(isundeletable())
objNames.add(new SelectOption(name,name));
}*/
return objNames;
}
}