You need to sign in to do that
Don't have an account?

Help with Test Class?
Hi Everyone,
I am new to salesforce, can you please provide test class for this class
public without sharing class UtilityClassWithoutSharing {
static final String ERROR_MSG = 'The Opportunity you have chosen does not meet all Opportunity Validation rules. Please update the opportunity and then re-assocaite it with the Deal.';
//Method used in Deal Trigger.
public static void updateOpp(map<id,id> oppIdDealIdMap, List<Deal__c> newDeals){
list<Opportunity> opps = [select id,Deal__c from Opportunity where id in:oppIdDealIdMap.keySet()];
list<Opportunity> oppsToUpdate = new list<Opportunity>();
if(opps!=null && opps.size()>0){
for(Opportunity opp:opps){
if(opp.Deal__c != oppIdDealIdMap.get(opp.id)){
opp.Deal__c = oppIdDealIdMap.get(opp.id);
oppsToUpdate.add(opp);
}
}
}
// for handeling proper error messages
//modified by Manmeet on 19th Dec 2012 for T-104876
if(oppsToUpdate!=null && oppsToUpdate.size()>0){
Database.SaveResult[] results = Database.update(oppsToUpdate,false);
System.debug('MPK: '+results);
Map<Id,String> mapErrors = new Map<Id,String>();
for(Integer index = 0 ; index < results.size(); index++){
Database.SaveResult result = results.get(index);
Opportunity oppty = oppsToUpdate.get(index);
if(!result.isSuccess() && String.valueOf(result.getErrors()[0].getStatusCode()).contains('FIELD_CUSTOM_VALIDATION_EXCEPTION')){
mapErrors.put(oppty.Deal__c,ERROR_MSG);
}
}
for(Deal__c deal : newDeals){
if(mapErrors.containsKey(deal.id)){
deal.addError(mapErrors.get(deal.id));
}
}
}
}
//Method used in Deal Trigger.
public static void updateOppToRemoveDealId(list<id> oppIdDealIdMap){
list<Opportunity> opps = [select id,Deal__c from Opportunity where Deal__c in:oppIdDealIdMap];
list<Opportunity> oppsToUpdate = new list<Opportunity>();
if(opps!=null && opps.size()>0){
for(Opportunity opp:opps){
opp.Deal__c = null;
oppsToUpdate.add(opp);
}
}
if(oppsToUpdate!=null && oppsToUpdate.size()>0){
update oppsToUpdate;
}
}
}
Thanks
I am new to salesforce, can you please provide test class for this class
public without sharing class UtilityClassWithoutSharing {
static final String ERROR_MSG = 'The Opportunity you have chosen does not meet all Opportunity Validation rules. Please update the opportunity and then re-assocaite it with the Deal.';
//Method used in Deal Trigger.
public static void updateOpp(map<id,id> oppIdDealIdMap, List<Deal__c> newDeals){
list<Opportunity> opps = [select id,Deal__c from Opportunity where id in:oppIdDealIdMap.keySet()];
list<Opportunity> oppsToUpdate = new list<Opportunity>();
if(opps!=null && opps.size()>0){
for(Opportunity opp:opps){
if(opp.Deal__c != oppIdDealIdMap.get(opp.id)){
opp.Deal__c = oppIdDealIdMap.get(opp.id);
oppsToUpdate.add(opp);
}
}
}
// for handeling proper error messages
//modified by Manmeet on 19th Dec 2012 for T-104876
if(oppsToUpdate!=null && oppsToUpdate.size()>0){
Database.SaveResult[] results = Database.update(oppsToUpdate,false);
System.debug('MPK: '+results);
Map<Id,String> mapErrors = new Map<Id,String>();
for(Integer index = 0 ; index < results.size(); index++){
Database.SaveResult result = results.get(index);
Opportunity oppty = oppsToUpdate.get(index);
if(!result.isSuccess() && String.valueOf(result.getErrors()[0].getStatusCode()).contains('FIELD_CUSTOM_VALIDATION_EXCEPTION')){
mapErrors.put(oppty.Deal__c,ERROR_MSG);
}
}
for(Deal__c deal : newDeals){
if(mapErrors.containsKey(deal.id)){
deal.addError(mapErrors.get(deal.id));
}
}
}
}
//Method used in Deal Trigger.
public static void updateOppToRemoveDealId(list<id> oppIdDealIdMap){
list<Opportunity> opps = [select id,Deal__c from Opportunity where Deal__c in:oppIdDealIdMap];
list<Opportunity> oppsToUpdate = new list<Opportunity>();
if(opps!=null && opps.size()>0){
for(Opportunity opp:opps){
opp.Deal__c = null;
oppsToUpdate.add(opp);
}
}
if(oppsToUpdate!=null && oppsToUpdate.size()>0){
update oppsToUpdate;
}
}
}
Thanks
This article will help
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm