Cannot validate on saving new record - salesforce

Salesforce Guru:
I am writing a Salesforce visualforce page extension controller to implement validation and other biz logic in it. However, my code doesn't work as expected. It seems the code validating sub category and serial number is never reached. The system debug log shows the sub category is always null though I have input values in VF page. Can you pls help? thank you very much!
public class CaseCreationExtension {
ApexPages.StandardController stdCtrl;
Case newCase;
public CaseCreationExtension(ApexPages.StandardController controller){
if (!Test.isRunningTest()){
controller.addFields(new List<String>{'Categ__c', 'Sub_Category__c', 'Id', 'Serial_Number__c', 'Refund_Required__c', 'Total_Value__c', 'Per_Month__c'});
system.debug('AddFields');
}
system.debug('Initial Controller');
this.stdCtrl = controller;
newCase = (Case)stdCtrl.getRecord();
system.debug('AccountId' + newCase.AccountId);
}
public PageReference validateSaveRedirect(){
system.debug('Sub Category:' + newCase.Sub_Category__c);
system.debug('Serial Number:' + newCase.Serial_Number__c);
if(newCase.Sub_Category__c == 'Cancellations' && newCase.Serial_Number__c == null){
system.debug('Adding error message');
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Serial Number must be specified for cancellation case.');
ApexPages.addMessage(myMsg);
system.debug('finished Adding error message');
return null;
}else{
if(null==stdCtrl.save()){
return null;
}
/*try{
stdCtrl.save();
}
catch(system.Exception e){
ApexPages.Message msg = new apexPages.Message(Apexpages.Severity.ERROR, e.getdmlMessage(0));
ApexPages.addMessage(msg);
return null;
}*/
stdCtrl.save();
system.debug('New Case Id:' + newCase.Id);
PageReference pr = new PageReference('/' + newCase.Id);
pr.setRedirect(true);
return pr;
}
}
public PageReference cancelRedirect(){
stdCtrl.cancel();
system.debug('New Case Id:' + newCase.Contact);
PageReference pr = new PageReference('/' + newCase.Contact);
pr.setRedirect(true);
return pr;
}
}

What did you base your VF form on? <apex:inputField value="{!case.Sub_Category__c}"/>? It seems the newCase variable is private + it doesn't have any getter/setter.
From what I remember the act of calling (Case)stdCtrl.getRecord(); "decouples" the variables. It's more of a copy than a reference to same object. So it's likely the stdCtrl has then values you've set but they weren't passed on to the newCase because it was "cloned" out of stdCtrl before the user has edited the data. Insert a System.debug(stdCtrl); into your method; if I'm right - you'll see your new values in there.
You could move the (Case)stdCtrl.getRecord(); into that method.
Or - change your apex to say public Case newCase {get;set;} and modify the VF form to reference {!newCase.Sub_Category__c} etc.

Related

Salesforce Code Coverage Failure. Your code coverage is 12%. You need at least 75% coverage to complete this deployment

I wanted to deploy my code to production. In this apex code, I am calling a third party api for opportunity on click of button which triggers the doSomething() from VF page. I want to fix this issue and push the below code to my production account.
Here is my apex class code
{
private ApexPages.StandardController standardController;
public DetailButtonController(ApexPages.StandardController standardController)
{
this.standardController = standardController;
}
public PageReference doSomething()
{
// Apex code for handling record from a Detail page goes here
Id recordId = standardController.getId();
Opportunity record = (Opportunity) standardController.getRecord();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint('https://mergeasy.com/merge_file');
req.setMethod('POST');
//function to Convert date to mm/dd/yyy
Date dToday = record.Closing_Date__c;
String clos_date = 'On or before ' + dToday.month() + '/' + dToday.day() + '/' + dToday.year();
Date dAcc = record.Offer_Acceptance_Date__c;
String acc_date = dAcc.month() + '/' + dAcc.day() + '/' + dAcc.year();
String str1 = '' + record.Purchase_Price__c ;
String f_p_price = str1.SubStringBefore('.');
String str2 = '' + record.Escrow_Deposit__c ;
String e_d_price = str2.SubStringBefore('.');
String str3 = '' + record.Balance__c ;
String b_price = str3.SubStringBefore('.');
if(record.Second_Seller_Name_Phone__c==null && record.Second_Seller_Email__c==null && record.Name!=null && record.Company_Profile__c!=null){
req.setBody('seller_name='+record.Name+'&buyer_name='+record.Company_Profile__c+'&county='+record.County_Contract__c+'&street_address='+record.Left_Main__Address_1__c+'&p_price='+f_p_price+'&escrow_deposit='+e_d_price+'&title_agent='+record.Escrow_Agent_Name__c+'&title_address='+record.Escrow_Address__c+'&title_phone='+record.Escrow_Number__c+'&balance='+b_price+'&accept_date='+acc_date+'&closing_date='+clos_date+'&inspection_days='+record.Inspection_Days__c+'&special_clause='+record.Special_Clauses__c+'&doc_id=XXXXXXXXXX&doc_name=Contract.pdf&delivery_method=docusign&sign_order=true&recipient1_email='+record.Email__c+'&recipient1_name='+record.Name+'&recipient2_name='+record.Company_Profile__c+'&recipient2_email=developer.c2c#gmail.com&docusign_doc_name=Contract - Attorney Involved&email_subject=Contract:'+record.Left_Main__Address_1__c+'&email_body=Hi please sign the attached contract');
}
else if(record.Second_Seller_Name_Phone__c!=null && record.Second_Seller_Email__c!=null && record.Name!=null && record.Company_Profile__c!=null){
String name = record.Name + ' and ' + record.Second_Seller_Name_Phone__c ;
req.setBody('seller_name='+name+'&buyer_ame='+record.Company_Profile__c+'&county='+record.County_Contract__c+'&street_address='+record.Left_Main__Address_1__c+'&p_price='+f_p_price+'&escrow_deposit='+e_d_price+'&title_agent='+record.Escrow_Agent_Name__c+'&title_address='+record.Escrow_Address__c+'&title_phone='+record.Escrow_Number__c+'&balance='+b_price+'&accept_date='+acc_date+'&closing_date='+clos_date+'&inspection_days='+record.Inspection_Days__c+'&special_clause='+record.Special_Clauses__c+'&doc_id=XXXXXXXXXX&doc_name=Contract.pdf&delivery_method=docusign&sign_order=true&recipient1_email='+record.Email__c+'&recipient1_name='+record.Name+'&recipient2_name='+record.Second_Seller_Name_Phone__c+'&recipient2_email='+record.Second_Seller_Email__c+'&recipient3_email=developer.c2c#gmail.com&recipient3_name='+record.Company_Profile__c+'&docusign_doc_name=Contract - Normal(1S1B).pdf&email_subject=Contract:'+record.Left_Main__Address_1__c+'&email_body=Hi please sign the attached contract');
}
req.setHeader('Authorization', 'Bearer XXXXXXXXXXXXXX');
try {
res = http.send(req);
} catch(System.CalloutException e) {
System.debug('Callout error: '+ e);
System.debug(res.toString());
}
return null;
}
}
Here is the test class, which is showing 90% code coverage.
//testClasst.apxc
#isTest
public class testClassBt {
#isTest
static void testPostCallout() {
System.Test.setMock(HttpCalloutMock.class, new TestClass());
Opportunity opp = new Opportunity();
opp.Name='Rickson Developer';
opp.StageName='Underwrite';
opp.CloseDate= date.newInstance(1991, 2, 21);
opp.Closing_Date__c= date.newInstance(1991, 2, 21);
opp.Offer_Acceptance_Date__c =date.newInstance(1991, 2, 21);
opp.Purchase_Price__c = 1200.00;
opp.Escrow_Deposit__c= 1200.00;
opp.Company_Profile__c='RFTA Properties, LLC';
opp.County_Contract__c='Orange';
opp.Left_Main__Address_1__c='123 Main Street';
opp.Escrow_Agent_Name__c='Test Agent';
opp.Escrow_Address__c='123 Main street';
opp.Escrow_Number__c='9892132382';
opp.Inspection_Days__c=34;
opp.Special_Clauses__c='Test';
insert opp;
ApexPages.StandardController standardController = new ApexPages.StandardController(opp);
DetailButtonController strResp = new DetailButtonController(standardController);
strResp.doSomething();
}
}
//TestClass.apxc
#isTest
global class TestClass implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest request) {
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody('{"animal": {"id":1, "name":"Tiger"}}');
response.setStatusCode(200);
return response;
}
}
assuming that during the validation process you run just the test methods of this class, did you try to run your test class in Sandbox first?
Some IDE and the Salesforce Developer Console itself show you the covered lines after the unit test execution.
Just follow the green lines to debug the code and understand where the exception has been thrown.
If you could post the Test class too, we can help you more.

Apex Batch Class to update Contact field from custom object field

I have an apex batch class which I need to update a contact boolean field 'Active_Subscriptions__c' based on the state of a related custom object field.
The custom object 'Subscriptions__c' has a 'Contact__c' master detail field, and also has a 'IsActive__c' boolean field.
I want to run a batch that will find any subscriptions that are related to a contact record by Id. If it finds any subscriptions that are active, it needs to set the 'Active_Subscriptions__c' on the contact record to true, else set to false.
I am fairly new to apex and can't seem to get this to trigger the results I need, thanks in advance.
global class BatchContactUpdate implements Database.Batchable<sObject>{
List <Subscription__c> mapSubs = new List <Subscription__c> ();
List <Contact> contactlist = new List <Contact> ();
global Database.QueryLocator start(Database.BatchableContext BC) {
return DataBase.getQueryLocator([SELECT Id, Contact__c FROM Subscription__c WHERE Contact__c =:contactlist]);
}
global void execute(Database.BatchableContext BC , List <Subscription__c> mapSubs) {
for (Subscription__c sub : mapSubs){
for (Contact con : contactList){
if (con.Id == sub.Contact__c && sub.IsActive__c == true){
contactlist.add(new Contact(
Active_Subscriptions__c = true
));
} else {
contactlist.add(new Contact(
Active_Subscriptions__c = false
));
}
}
}
update contactlist;
}
global void finish(Database.BatchableContext BC){
}
}
Looks like you contact list is empty to start off with, so it wont return any results. But for your query, why dont you do something like
[SELECT Id, (SELECT Id FROM Subscriptions__r WHERE Is_Active__c = true) FROM Contact];
and in your execute do
List<Contact> cList = new List<contact>();
for(Contact c : scope){
c.Active_Subscriptions__c = c.Subscriptions__r.size() > 0;
cList.add(c);
}
//etc... to start

update a already inserted image in database using java spring

I am tying like this , I want to use same method for inserting and updating, also same form to insert and update. please help me.. this is my controller, it is working fine if I choose new image while updating but if I didnot select image while updating and only update other details then image updated to null in database..
public ModelAndView addOrUpdateFoodItems(#RequestParam(name = "file")CommonsMultipartFile file,
#RequestParam(name = "food_name")String food_name,
#RequestParam(name = "food_type")String food_type,
#RequestParam(name = "food_tags")String food_tags,
#RequestParam(name = "food_desription")String food_desription,
#RequestParam(name = "protein_intake")String protein_intake,
#RequestParam(name = "calorie_intake")String calorie_intake,
#RequestParam(name = "carbs_intake")String carbs_intake,ModelAndView model, HttpServletRequest request){
FoodItemDetails foodItemDetails;
foodItemDetails= (FoodItemDetails) request.getSession().getAttribute("FoodItemDetails");
if(foodItemDetails==null){
foodItemDetails=new FoodItemDetails();
}else{
System.out.println(foodItemDetails.getFood_name());
}
foodItemDetails.setFood_name(food_name);
foodItemDetails.setFood_type(food_type);
foodItemDetails.setFood_tags(food_tags);
foodItemDetails.setFood_desription(food_desription);
foodItemDetails.setProtein_intake(protein_intake);
foodItemDetails.setCalorie_intake(calorie_intake);
foodItemDetails.setCarbs_intake(carbs_intake);
if(file.getOriginalFilename()!=null){
foodItemDetails.setFood_item_image(file.getBytes());
foodItemDetails.setFood_item_image_name(file.getOriginalFilename());}
else{
foodItemDetails.setFood_item_image_name(foodItemDetails.getFood_item_image_name());
foodItemDetails.setFood_item_image(foodItemDetails.getFood_item_image());
}
boolean done= trainerService.saveOrUpdateFoodItems(foodItemDetails);
model.setViewName("redirect:/trainer/addFoodItems");
return model;
}

Compare two different SOQL queries

I am new to salesforce and I am stuck with a situation here.
I have a class which is scheduled every hour. I hit an account with the below code and an email is sent out to MAROPOST (Marketing automation tool). When this happen I want to track the Account and create a case or a log which says Welcome Email is sent so that I don't hit the same Account again.
Please help. Below is the working class. Please help
public class PD_WelcomeMaroPost {
public static string sendEmailThroughMaro(string myInpEmail) {
string successContacts = '';
string failureContacts = '';
// SQL to fetch FBO who Joined Today
list<Account> conts = new list<Account> ([SELECT name, Email_FLP_com__c,
(SELECT Id
FROM Stripe_Subscriptons__r
WHERE Start_Date__c= TODAY
AND Status__c='active'
AND Welcome_Email__C = false
LIMIT 1)
from account
where ID IN (
select Distributor__c
from Stripe_Subscripton__c
where Start_Date__c= TODAY
AND Status__c='active'
AND Welcome_Email__C = false)
AND Email_FLP_com__c != NULL
LIMIT 100]);
system.debug('>>>>>>>>>>' + conts);
overallEmail myEmail = new overallEmail();
List<Stripe_Subscripton__c> subsToUpdate = new List<Stripe_Subscripton__c>();
for(Account c : conts){
myEmail.email.campaign_id = 172;
myEmail.email.contact.Email = c.Email_FLP_com__c;
myEmail.email.contact.first_name = c.name;
/**MAp<String, String> tags = new Map<String, String>();
tags.put('firstName', c.name);
myEmail.email.tags = tags;**/
system.debug('#### Input JSON: ' + JSON.serialize(myEmail));
try{
String endpoint = 'http://api.maropost.com/accounts/1173/emails/deliver.json?auth_token=j-V4sx8ueUT7eKM8us_Cz5JqXBzoRrNS3p1lEZyPUPGcwWNoVNZpKQ';
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setHeader('Content-type', 'application/json');
req.setbody(JSON.serialize(myEmail));
Http http = new Http();
system.debug('Sending email');
HTTPResponse response = http.send(req);
system.debug('sent email');
string resultBodyGet = '';
resultBodyGet = response.getBody();
system.debug('Output response:' + resultBodyGet);
maroResponse myMaroResponse = new maroResponse();
myMaroResponse = (maroResponse) JSON.deserialize(resultBodyGet, maroResponse.class);
system.debug('#### myMaroResponse: ' + myMaroResponse);
if(myMaroResponse.message == 'Email was sent successfully')
successContacts = successContacts + ';' + c.Email_FLP_com__c;
else
failureContacts = failureContacts + ';' + c.Email_FLP_com__c;
}
catch (exception e) {
failureContacts = failureContacts + ';' + c.Email_FLP_com__c;
system.debug('#### Exception caught: ' + e.getMessage());
}
c.Stripe_Subscriptons__r[0].Welcome_Email__c = true;
subsToUpdate.add(c.Stripe_Subscriptons__r[0]);
}
Update subsToUpdate;
return 'successContacts=' + successContacts + '---' + 'failureContacts=' + failureContacts;
}
public class maroResponse {
public string message {get;set;}
}
public class overallEmail {
public emailJson email = new emailJson();
}
public class emailJson {
public Integer campaign_id;
public contactJson contact = new contactJson();
// Public Map<String, String> tags;
}
public class contactJson {
public string email;
public string first_name;
}
}
You're making a callout in a loop, there's governor limit of max 100 callouts. See Limits class to obtain current & max numbers programatically rather than hardcoding it.
Other than that it should be pretty simple change. First add your filter to the query and add a "subquery" (something like a JOIN) that pulls the related list of subscriptions
list<Account> conts = new list<Account> ([SELECT name, Email_FLP_com__c,
(SELECT Id
FROM Stripe_Subscriptions__r
WHERE Start_Date__c= TODAY
AND Status__c='active'
AND Welcome_Email__C = false
LIMIT 1)
from account
where ID IN (
select Distributor__c
from Stripe_Subscripton__c
where Start_Date__c= TODAY
AND Status__c='active'
AND Welcome_Email__C = false)
AND Email_FLP_com__c != NULL
LIMIT 100]);
Then it's just few lines more
List<Stripe_Subscription__c> subsToUpdate = new List<Stripe_Subscription__c>();
for(Account a : conts){
// do your maropost code here
a.Stripe_Subscriptions__r[0].Welcome_Email__c = true;
subsToUpdate.add(a.Stripe_Subscriptions__r[0]);
}
update subsToUpdate;
Of course you might want to set that checkbox to true only if callout went OK ;)
After reading your code, I don't see where you tried to accomplish this. If you post your attempt I'd be glad to help fix it.
Instead I'll give you different logic for what you are trying to do.
1.) create new checkbox field
2.) in batch query where box is not checked
3.) send email
4.) check checkbox
to answer your comment here is some sample code, you will need to fix it yourself, i am just making temp names
for(sobjectname gg:[your query]){
Send email;
gg.checkbox = checked;
update gg;
}
it'd be better to make it bulkified though
list<yourSObject> tobeupdated = new list<yourSObject>([Your query]);
for(yourSObject gg: tobeupdated){
send email;
gg.checkbox = true;
}
update tobeupdated;

what is wrong with my test class in apex force.com code?

I have written a working class in Apex. It is an Email service extender, that processes incoming emails.
It is working perfect on my sandbox enviroment.
I have created a test class, so I can also deploy it to my production, but when validating the code, I get the only 72% of my code is tested.
This is my main class
global class inboundEmail implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
Lead lead;
String [] mFromUserParams;
String [] sourceText;
String mCaseObject;
try{
sourceText = email.toAddresses[0].split('#');
String [] mParams = sourceText[0].split('\\.');
**// FROM THIS LINE TO THE END - NOT COVERED**
mFromUserParams = email.fromAddress.split('#');
mCaseObject = mParams[0];
if (mCaseObject == 'lead'){
lead = new Lead();
lead.LastName = mFromUserParams[0];
lead.Company = email.fromAddress;
lead.OwnerId = mParams[1];
lead.LeadSource = mParams[2];
lead.Email = email.fromAddress;
lead.RequirementsDescription__c = email.subject + email.plainTextBody;
insert lead;
result.success = true;
} else if (mCaseObject == 'case'){
result.success = true;
} else {
result.success = false;
}
}catch(Exception e){
result.success = false;
result.message = 'Oops, I failed.';
}
return result;
}
}
This is my test class
#isTest
private class inboundEmailTest {
public static testMethod void inboundEmail(){
// Create a new email, envelope object and Header
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
envelope.toAddress = 'lead.owner.new#cpeneac.cl.apex.sandbox.salesforce.com';
envelope.fromAddress = 'user#acme.com';
email.subject = 'Please contact me';
email.fromName = 'Test From Name';
email.plainTextBody = 'Hello, this a test email body. for testing Bye';
// setup controller object
inboundEmail catcher = new inboundEmail();
Messaging.InboundEmailResult result = catcher.handleInboundEmail(email, envelope);
}
}
According to the error message, ALL lines in the Try/Catch block from the 3rd line are not covered. (marked in the code).
in your test method you're setting envelope.toAddress but in your email service you're splitting the first element of the actual InboundEmail objects toAddresses. this probably causes either an ArrayIndexOutOfBoundsException or a NPE because the element 0 does not exist. so the code coverage will be poor because your test always jumps into the exception handling and leaves the rest of you code uncovered. just set the emails toAddresses and you should have a better coverage.
h9nry
In your test code, can you add a scenario that causes the lead insert to fail? This will cause the code in your catch block to execute and provide you the needed code test coverage.
The email.fromAddress is not a list by default, so just setting that to a string and not a list solved this.

Resources