You need to sign in to do that
Don't have an account?
Smita Hodiggeri
(How and where to start) to debug the non working code for some scenarios -- Dev Newbie
Hi Devs,
So i have inherited code which should create reneweal oppotunites based on policies which is not creating reneweal opportuites for some polices and I need to work out why. I am stumped at the moment about how to and where to strat. I would like to start by executing the code in Anon block and was wondering I can get help with the synatx? Here is the class any thoughts are appreciated.
global class CreatePolicyToOppBatch implements Database.Batchable<sObject>, Database.Stateful, database.allowscallouts,schedulable {
global Map<String, Decimal> insuranceMap = new Map<String, Decimal>();
global Map<String, Policy_Opportunity_Field_Map__mdt> fieldMap = new Map<String, Policy_Opportunity_Field_Map__mdt>();
global Database.QueryLocator start(Database.BatchableContext bc) {
String query = 'Select Id, Class_of_Business__c, Insurance_Start_Date__c, Account__c, Insurance_End_Date__c,Account_RecordType__c, Account__r.OwnerId';
for(Policy_Class_of_Insurance__mdt pm : [SELECT Id, Label, QualifiedApiName, Value__c FROM Policy_Class_of_Insurance__mdt]){
insuranceMap.put(pm.Label, pm.Value__c);
}
sObject sObj = Schema.getGlobalDescribe().get('Policies__c').newSObject() ;
Map<String, Schema.SObjectField> poFieldMapping = sObj.getSObjectType().getDescribe().fields.getMap();
sObject opp = Schema.getGlobalDescribe().get('Opportunity').newSObject() ;
Map<String, Schema.SObjectField> opFieldMapping = opp.getSObjectType().getDescribe().fields.getMap();
for(Policy_Opportunity_Field_Map__mdt po : [SELECT Id, Label,Policy_Field_Api__c ,Default_Value_AUS__c, Default_Value_NZ__c,Use_Default_If_Null__c,Default_Value__c FROM Policy_Opportunity_Field_Map__mdt]){
if(poFieldMapping.containsKey(po.Policy_Field_Api__c) && po.Policy_Field_Api__c <> null ){
if( !'Insurance_End_Date__c'.equals(po.Policy_Field_Api__c) && !'Class_of_Business__c'.equals(po.Policy_Field_Api__c) && !'Insurance_Start_Date__c'.equals(po.Policy_Field_Api__c) && !'Account__c'.equals(po.Policy_Field_Api__c) && !'Account_RecordType__c'.equals(po.Policy_Field_Api__c)) {
query= query + ', ' + po.Policy_Field_Api__c;
}
}
if(opFieldMapping.containsKey(po.Label)) {
fieldMap.put(po.Label,po);
}
}
query = query+' FROM Policies__c WHERE Policy_Status__c != \'Expired\' AND Policy_Status__c != \'Cancelled\' ';
System.debug(query);
return Database.getQueryLocator(query);
}
global void execute(SchedulableContext ctx){
CreatePolicyToOppBatch poBatch = new CreatePolicyToOppBatch();
Database.executeBatch(poBatch,50);
}
global void execute(Database.BatchableContext bc, List<sObject> scope){
System.debug('ecute>>'+ scope);
List<sObject> policyListToUpdate = new List<sObject>();
List<sObject> oppList = new List<sObject>();
Date policyStartDate;
for(sObject obj : scope){
if(obj.get('Insurance_Start_Date__c') <> null){
policyStartDate = Date.valueOf(obj.get('Insurance_Start_Date__c'));
System.debug('insuranceMap >>'+insuranceMap);
System.debug('Class_of_Business__c>>'+obj.get('Class_of_Business__c'));
Integer noofday =Integer.valueOf(insuranceMap.get(String.valueOf(obj.get('Class_of_Business__c'))));
System.debug(noofday);
System.debug(policyStartDate.daysBetween(system.today()));
if(policyStartDate.daysBetween(system.today()) == noofday ){
sObject opp = Schema.getGlobalDescribe().get('Opportunity').newSObject() ;
Map<String, Schema.SObjectField> fieldMapping = opp.getSObjectType().getDescribe().fields.getMap();
for(String fieldval : fieldMap.keySet()){
if(getValue(fieldMap,fieldval,obj) <> null ){
if(Schema.DisplayType.DATE == fieldMapping.get(fieldval).getDescribe().getType()){
Date d = Date.ValueOf(getValue(fieldMap,fieldval,obj));
if('Insurance_End_Date__c'.equals(fieldMap.get(fieldval).Policy_Field_Api__c) && ('Renewal_Date__c'.equals(fieldval) || 'CloseDate'.equals(fieldval))){
d = d.addDays(1);
}
opp.put(fieldval, d );
}
else if(Schema.DisplayType.DATETIME == fieldMapping.get(fieldval).getDescribe().getType()){
opp.put(fieldval, DateTime.ValueOf(getValue(fieldMap,fieldval,obj)) );
}
else if(Schema.DisplayType.CURRENCY == fieldMapping.get(fieldval).getDescribe().getType()){
opp.put(fieldval, Decimal.ValueOf(String.valueOf(getValue(fieldMap,fieldval,obj)) ));
}
else if(Schema.DisplayType.BOOLEAN == fieldMapping.get(fieldval).getDescribe().getType()){
opp.put(fieldval, Boolean.ValueOf(getValue(fieldMap,fieldval,obj)));
}
else if(Schema.DisplayType.DOUBLE == fieldMapping.get(fieldval).getDescribe().getType()){
opp.put(fieldval, Double.ValueOf(String.valueOf(getValue(fieldMap,fieldval,obj)) ));
}
else{
opp.put(fieldval, String.valueOf(getValue(fieldMap,fieldval,obj)));
}
}
}
opp.put('AccountId',String.valueOf(obj.get('Account__c') ));
if(obj.getSObject('Account__r') <> null && obj.getSObject('Account__r').get('OwnerId') <> null){
opp.put('OwnerId',String.valueOf(obj.getSObject('Account__r').get('OwnerId')));
}
oppList.add(opp);
System.debug(opp);
}
if(Date.valueOf(obj.get('Insurance_End_Date__c')) == System.today()){
obj.put('Policy_Status__c', 'Expired ');
policyListToUpdate.add(obj);
}
}
}
if(!oppList.isEmpty()){
insert oppList;
}
if(!policyListToUpdate.isEmpty()){
update policyListToUpdate;
}
}
global void finish(Database.BatchableContext bc){
}
public Static Object getValue(Map<String, Policy_Opportunity_Field_Map__mdt> fieldMap,String fieldval, sObject obj){
if(fieldMap.get(fieldval).Policy_Field_Api__c <> null && obj.get(fieldMap.get(fieldval).Policy_Field_Api__c) <> null){
return obj.get(fieldMap.get(fieldval).Policy_Field_Api__c);
}
else if(fieldMap.get(fieldval).Use_Default_If_Null__c == true ) {
if('New_Zealand_Account'.equals(String.valueOf(obj.get('Account_RecordType__c')))){
return fieldMap.get(fieldval).Default_Value_NZ__c;
}
else if('Australian_Account'.equals(String.valueOf(obj.get('Account_RecordType__c')))){
return fieldMap.get(fieldval).Default_Value_AUS__c;
}
else{
return fieldMap.get(fieldval).Default_Value__c;
}
}
return null;
}
global static string scheduleJob(){
CreatePolicyToOppBatch job = new CreatePolicyToOppBatch();
String CRON_EXPR = '0 0 5 * * ?' ; //every day 5 am
System.Schedule('CreatePolicyToOppBatch Job',CRON_EXPR,job);
return null;
}
}
So i have inherited code which should create reneweal oppotunites based on policies which is not creating reneweal opportuites for some polices and I need to work out why. I am stumped at the moment about how to and where to strat. I would like to start by executing the code in Anon block and was wondering I can get help with the synatx? Here is the class any thoughts are appreciated.
global class CreatePolicyToOppBatch implements Database.Batchable<sObject>, Database.Stateful, database.allowscallouts,schedulable {
global Map<String, Decimal> insuranceMap = new Map<String, Decimal>();
global Map<String, Policy_Opportunity_Field_Map__mdt> fieldMap = new Map<String, Policy_Opportunity_Field_Map__mdt>();
global Database.QueryLocator start(Database.BatchableContext bc) {
String query = 'Select Id, Class_of_Business__c, Insurance_Start_Date__c, Account__c, Insurance_End_Date__c,Account_RecordType__c, Account__r.OwnerId';
for(Policy_Class_of_Insurance__mdt pm : [SELECT Id, Label, QualifiedApiName, Value__c FROM Policy_Class_of_Insurance__mdt]){
insuranceMap.put(pm.Label, pm.Value__c);
}
sObject sObj = Schema.getGlobalDescribe().get('Policies__c').newSObject() ;
Map<String, Schema.SObjectField> poFieldMapping = sObj.getSObjectType().getDescribe().fields.getMap();
sObject opp = Schema.getGlobalDescribe().get('Opportunity').newSObject() ;
Map<String, Schema.SObjectField> opFieldMapping = opp.getSObjectType().getDescribe().fields.getMap();
for(Policy_Opportunity_Field_Map__mdt po : [SELECT Id, Label,Policy_Field_Api__c ,Default_Value_AUS__c, Default_Value_NZ__c,Use_Default_If_Null__c,Default_Value__c FROM Policy_Opportunity_Field_Map__mdt]){
if(poFieldMapping.containsKey(po.Policy_Field_Api__c) && po.Policy_Field_Api__c <> null ){
if( !'Insurance_End_Date__c'.equals(po.Policy_Field_Api__c) && !'Class_of_Business__c'.equals(po.Policy_Field_Api__c) && !'Insurance_Start_Date__c'.equals(po.Policy_Field_Api__c) && !'Account__c'.equals(po.Policy_Field_Api__c) && !'Account_RecordType__c'.equals(po.Policy_Field_Api__c)) {
query= query + ', ' + po.Policy_Field_Api__c;
}
}
if(opFieldMapping.containsKey(po.Label)) {
fieldMap.put(po.Label,po);
}
}
query = query+' FROM Policies__c WHERE Policy_Status__c != \'Expired\' AND Policy_Status__c != \'Cancelled\' ';
System.debug(query);
return Database.getQueryLocator(query);
}
global void execute(SchedulableContext ctx){
CreatePolicyToOppBatch poBatch = new CreatePolicyToOppBatch();
Database.executeBatch(poBatch,50);
}
global void execute(Database.BatchableContext bc, List<sObject> scope){
System.debug('ecute>>'+ scope);
List<sObject> policyListToUpdate = new List<sObject>();
List<sObject> oppList = new List<sObject>();
Date policyStartDate;
for(sObject obj : scope){
if(obj.get('Insurance_Start_Date__c') <> null){
policyStartDate = Date.valueOf(obj.get('Insurance_Start_Date__c'));
System.debug('insuranceMap >>'+insuranceMap);
System.debug('Class_of_Business__c>>'+obj.get('Class_of_Business__c'));
Integer noofday =Integer.valueOf(insuranceMap.get(String.valueOf(obj.get('Class_of_Business__c'))));
System.debug(noofday);
System.debug(policyStartDate.daysBetween(system.today()));
if(policyStartDate.daysBetween(system.today()) == noofday ){
sObject opp = Schema.getGlobalDescribe().get('Opportunity').newSObject() ;
Map<String, Schema.SObjectField> fieldMapping = opp.getSObjectType().getDescribe().fields.getMap();
for(String fieldval : fieldMap.keySet()){
if(getValue(fieldMap,fieldval,obj) <> null ){
if(Schema.DisplayType.DATE == fieldMapping.get(fieldval).getDescribe().getType()){
Date d = Date.ValueOf(getValue(fieldMap,fieldval,obj));
if('Insurance_End_Date__c'.equals(fieldMap.get(fieldval).Policy_Field_Api__c) && ('Renewal_Date__c'.equals(fieldval) || 'CloseDate'.equals(fieldval))){
d = d.addDays(1);
}
opp.put(fieldval, d );
}
else if(Schema.DisplayType.DATETIME == fieldMapping.get(fieldval).getDescribe().getType()){
opp.put(fieldval, DateTime.ValueOf(getValue(fieldMap,fieldval,obj)) );
}
else if(Schema.DisplayType.CURRENCY == fieldMapping.get(fieldval).getDescribe().getType()){
opp.put(fieldval, Decimal.ValueOf(String.valueOf(getValue(fieldMap,fieldval,obj)) ));
}
else if(Schema.DisplayType.BOOLEAN == fieldMapping.get(fieldval).getDescribe().getType()){
opp.put(fieldval, Boolean.ValueOf(getValue(fieldMap,fieldval,obj)));
}
else if(Schema.DisplayType.DOUBLE == fieldMapping.get(fieldval).getDescribe().getType()){
opp.put(fieldval, Double.ValueOf(String.valueOf(getValue(fieldMap,fieldval,obj)) ));
}
else{
opp.put(fieldval, String.valueOf(getValue(fieldMap,fieldval,obj)));
}
}
}
opp.put('AccountId',String.valueOf(obj.get('Account__c') ));
if(obj.getSObject('Account__r') <> null && obj.getSObject('Account__r').get('OwnerId') <> null){
opp.put('OwnerId',String.valueOf(obj.getSObject('Account__r').get('OwnerId')));
}
oppList.add(opp);
System.debug(opp);
}
if(Date.valueOf(obj.get('Insurance_End_Date__c')) == System.today()){
obj.put('Policy_Status__c', 'Expired ');
policyListToUpdate.add(obj);
}
}
}
if(!oppList.isEmpty()){
insert oppList;
}
if(!policyListToUpdate.isEmpty()){
update policyListToUpdate;
}
}
global void finish(Database.BatchableContext bc){
}
public Static Object getValue(Map<String, Policy_Opportunity_Field_Map__mdt> fieldMap,String fieldval, sObject obj){
if(fieldMap.get(fieldval).Policy_Field_Api__c <> null && obj.get(fieldMap.get(fieldval).Policy_Field_Api__c) <> null){
return obj.get(fieldMap.get(fieldval).Policy_Field_Api__c);
}
else if(fieldMap.get(fieldval).Use_Default_If_Null__c == true ) {
if('New_Zealand_Account'.equals(String.valueOf(obj.get('Account_RecordType__c')))){
return fieldMap.get(fieldval).Default_Value_NZ__c;
}
else if('Australian_Account'.equals(String.valueOf(obj.get('Account_RecordType__c')))){
return fieldMap.get(fieldval).Default_Value_AUS__c;
}
else{
return fieldMap.get(fieldval).Default_Value__c;
}
}
return null;
}
global static string scheduleJob(){
CreatePolicyToOppBatch job = new CreatePolicyToOppBatch();
String CRON_EXPR = '0 0 5 * * ?' ; //every day 5 am
System.Schedule('CreatePolicyToOppBatch Job',CRON_EXPR,job);
return null;
}
}
try with below code in developer console.
If this helps, Please mark it as best answer.
Thanks!!
My expectaion was piece of code where in I can pass the policy number and see how system behaves in the Anon window.
I need help in understanding the above code pasted. Can you please eloborate?