• Aakash Saluja 7
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies
I have an  api class and have to wite test class but test class  is not covering :

class;
public class fileViewController { 
ApexPages.StandardController controller; 
public Boolean httpErrorReceived {get; set;} 
public String respFileString {get; set;} 
public String contentType {get;set;} 

public fileViewController(ApexPages.StandardController controller){ 
this.controller = controller; 
httpErrorReceived = false; 


public void viewFile(){ 
try{ 
Case req = getPortfolioUrl(); 
if(null !=req.Portfolio_Analysis_Report__c && req.View_checkbox_c){ 
String authorizationHeader = 'Bearer ' + PortfolioAnalysisCallouts.getAuthToken(); 

Http http = new Http(); 
HttpRequest request = new HttpRequest(); 
request.setMethod('GET'); 
request.setHeader('Authorization', authorizationHeader); 
request.setEndpoint(req.Portfolio_Analysis_Report__c); 
system.debug('Request==='+request); 

HttpResponse response = http.send(request); 
system.debug('Response==='+response); 
if(response.getStatusCode()==200 || response.getStatusCode()==202){ 
//respFileString = response.getBody(); 
respFileString = EncodingUtil.Base64Encode(response.getBodyAsBlob()); 
contentType = 'application/pdf'; 
}else{ 
respFileString = 'Connection Failed '; 
contentType = 'text/plain'; 
httpErrorReceived = true; 

}else{ 
respFileString = '\"Analysis Report\" is not available'; 
contentType = 'text/plain'; 
httpErrorReceived = true; 

}catch(Exception e){ 
respFileString = 'Connection Failed:"; 
contentType = 'text/plain'; 
httpErrorReceived = true; 

System.debug('respFileString=='+respFileString); 


private Case getPortfolioUrl() { 
return [select 
Id, 
Portfolio_Analysis_Report__c, 
View_checkbox_c 
from Case 
where Id =: ApexPages.currentPage().getParameters().get('id')]; 

private static String getUrl(){ 
URLParameters__c urls = URLParameters__c.getOrgDefaults();//custom setting 
String url = urls.newVal__c; 
return url; 



Mock class:
@isTest 

global class MockHttpResponseGenerator implements HttpCalloutMock { 

global HTTPResponse respond(HTTPRequest req) { 

System.assertEquals('https://example.com', req.getEndpoint()); 
System.assertEquals('GET', req.getMethod()); 


HttpResponse res = new HttpResponse(); 
res.setHeader('Content-Type', 'application/json'); 
res.setBody('{"test":"test1"}'); 
res.setStatusCode(200); 
return res; 



test class: 
@isTest 
public class ViewPortfolioPDFControllerTest { 
public static testMethod void method1() { 


case con = new case(); 
con.Subject = 'Test Subject'; 
con.Initial_Status__c ='Takeovers'; 
con.status='In Progress'; 
con.Total_Assets__c = 0; 
con.Portfolio_Analysis_Report__c= '/services/apexrest/claimApi'; 
con.View_checkbox_c=true; 
insert con; 

Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); 
Test.startTest(); 

apexpages.currentpage().getparameters().put('Cid' , con.Id); 
ApexPages.StandardController sc = new ApexPages.standardController(con); 
ViewPortfolioPDFController vpc= new ViewPortfolioPDFController(sc); 
vpc.viewPortfolioPDF(); 

Test.stopTest(); 
}

can anyone please help
Hi,
I have a requirement where there is a need to stop the record from coming into salesforce if it is a duplicate record ,i.e, the same record is already present in the system. The records are coming via integration and are getting saved but the scenario says that they should not even get saved either if it is a duplicate.
Is there any way that this can be acheived?

Thanks
Hi,

i am new to salesforce and i have a scenario where i have an object Search_Filter__c where i want to insert records based on the account.
if any account has 10 address it should create multiple records with the unique city value populated in that in the Search_Filter__c. like if there are two cties with the same name, it should create 1 record else two

i have written the code as:

trigger RcordInsert on Account (after insert, after update) {

map<id,account> mapAcc= new map<id,account>();
set<string> cities = new set<string>();

for(account acc: trigger.new){
    if(acc.KOL_Set_Search_flag__c != null){
        mapAcc.put(acc.id,acc);
        system.debug('id is'+mapAcc);
    }
}

list<account> listSearch = new list<account>([ SELECT name,
                                                      KOL_Set_Search_flag__c,
                                                      Middle_vod__c,
                                                      LastName,
                                                      (select City_vod__c,State_vod__c,Country_olr__c,Zip_vod__c from Address_vod__r )
                                               FROM account 
                                               WHERE id in : mapAcc.keyset()
                                            ]);
                                               
        list<Search_Filter__c> searchUpdateList  = new list<GBI_Search_Filter__c>();                                                                      
     for(account c : listSearch ){
      
  if(c.KOL_Set_Search_flag__c == true){
            KOL_GBI_Search_Filter__c searchFilter = new KOL_GBI_Search_Filter__c();
                searchFilter.KOL_FIRSTNAME__c   =  c.name;
                searchFilter.KOL_ID__c = c.id;
                searchFilter.KOL_MIDDLENAME__c  =  c.Middle_vod__c;
                searchFilter.KOL_LASTNAME__c    =  c.LastName;
               // For(Integer I = 0; I < c.Address_vod__r.size(); I++){
                for(Address_vod__c addr : c.Address_vod__r ){
                   
                searchFilter.KOL_CITY__c   =  addr.City_vod__c;
                searchFilter.KOL_STATE__c   =  addr.State_vod__c;
                searchFilter.KOL_POSTAL_ZIP_CODE__c   =  addr.Zip_vod__c;
                searchFilter.KOL_COUNTRY__c   =  addr.Country_olr__c;
                
               }
               
                searchUpdateList.add(searchFilter);
        }
        
     }     
     
     insert searchUpdateList;                                                                    
}

Thanks in advance
I have an  api class and have to wite test class but test class  is not covering :

class;
public class fileViewController { 
ApexPages.StandardController controller; 
public Boolean httpErrorReceived {get; set;} 
public String respFileString {get; set;} 
public String contentType {get;set;} 

public fileViewController(ApexPages.StandardController controller){ 
this.controller = controller; 
httpErrorReceived = false; 


public void viewFile(){ 
try{ 
Case req = getPortfolioUrl(); 
if(null !=req.Portfolio_Analysis_Report__c && req.View_checkbox_c){ 
String authorizationHeader = 'Bearer ' + PortfolioAnalysisCallouts.getAuthToken(); 

Http http = new Http(); 
HttpRequest request = new HttpRequest(); 
request.setMethod('GET'); 
request.setHeader('Authorization', authorizationHeader); 
request.setEndpoint(req.Portfolio_Analysis_Report__c); 
system.debug('Request==='+request); 

HttpResponse response = http.send(request); 
system.debug('Response==='+response); 
if(response.getStatusCode()==200 || response.getStatusCode()==202){ 
//respFileString = response.getBody(); 
respFileString = EncodingUtil.Base64Encode(response.getBodyAsBlob()); 
contentType = 'application/pdf'; 
}else{ 
respFileString = 'Connection Failed '; 
contentType = 'text/plain'; 
httpErrorReceived = true; 

}else{ 
respFileString = '\"Analysis Report\" is not available'; 
contentType = 'text/plain'; 
httpErrorReceived = true; 

}catch(Exception e){ 
respFileString = 'Connection Failed:"; 
contentType = 'text/plain'; 
httpErrorReceived = true; 

System.debug('respFileString=='+respFileString); 


private Case getPortfolioUrl() { 
return [select 
Id, 
Portfolio_Analysis_Report__c, 
View_checkbox_c 
from Case 
where Id =: ApexPages.currentPage().getParameters().get('id')]; 

private static String getUrl(){ 
URLParameters__c urls = URLParameters__c.getOrgDefaults();//custom setting 
String url = urls.newVal__c; 
return url; 



Mock class:
@isTest 

global class MockHttpResponseGenerator implements HttpCalloutMock { 

global HTTPResponse respond(HTTPRequest req) { 

System.assertEquals('https://example.com', req.getEndpoint()); 
System.assertEquals('GET', req.getMethod()); 


HttpResponse res = new HttpResponse(); 
res.setHeader('Content-Type', 'application/json'); 
res.setBody('{"test":"test1"}'); 
res.setStatusCode(200); 
return res; 



test class: 
@isTest 
public class ViewPortfolioPDFControllerTest { 
public static testMethod void method1() { 


case con = new case(); 
con.Subject = 'Test Subject'; 
con.Initial_Status__c ='Takeovers'; 
con.status='In Progress'; 
con.Total_Assets__c = 0; 
con.Portfolio_Analysis_Report__c= '/services/apexrest/claimApi'; 
con.View_checkbox_c=true; 
insert con; 

Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); 
Test.startTest(); 

apexpages.currentpage().getparameters().put('Cid' , con.Id); 
ApexPages.StandardController sc = new ApexPages.standardController(con); 
ViewPortfolioPDFController vpc= new ViewPortfolioPDFController(sc); 
vpc.viewPortfolioPDF(); 

Test.stopTest(); 
}

can anyone please help
Hi,

i am new to salesforce and i have a scenario where i have an object Search_Filter__c where i want to insert records based on the account.
if any account has 10 address it should create multiple records with the unique city value populated in that in the Search_Filter__c. like if there are two cties with the same name, it should create 1 record else two

i have written the code as:

trigger RcordInsert on Account (after insert, after update) {

map<id,account> mapAcc= new map<id,account>();
set<string> cities = new set<string>();

for(account acc: trigger.new){
    if(acc.KOL_Set_Search_flag__c != null){
        mapAcc.put(acc.id,acc);
        system.debug('id is'+mapAcc);
    }
}

list<account> listSearch = new list<account>([ SELECT name,
                                                      KOL_Set_Search_flag__c,
                                                      Middle_vod__c,
                                                      LastName,
                                                      (select City_vod__c,State_vod__c,Country_olr__c,Zip_vod__c from Address_vod__r )
                                               FROM account 
                                               WHERE id in : mapAcc.keyset()
                                            ]);
                                               
        list<Search_Filter__c> searchUpdateList  = new list<GBI_Search_Filter__c>();                                                                      
     for(account c : listSearch ){
      
  if(c.KOL_Set_Search_flag__c == true){
            KOL_GBI_Search_Filter__c searchFilter = new KOL_GBI_Search_Filter__c();
                searchFilter.KOL_FIRSTNAME__c   =  c.name;
                searchFilter.KOL_ID__c = c.id;
                searchFilter.KOL_MIDDLENAME__c  =  c.Middle_vod__c;
                searchFilter.KOL_LASTNAME__c    =  c.LastName;
               // For(Integer I = 0; I < c.Address_vod__r.size(); I++){
                for(Address_vod__c addr : c.Address_vod__r ){
                   
                searchFilter.KOL_CITY__c   =  addr.City_vod__c;
                searchFilter.KOL_STATE__c   =  addr.State_vod__c;
                searchFilter.KOL_POSTAL_ZIP_CODE__c   =  addr.Zip_vod__c;
                searchFilter.KOL_COUNTRY__c   =  addr.Country_olr__c;
                
               }
               
                searchUpdateList.add(searchFilter);
        }
        
     }     
     
     insert searchUpdateList;                                                                    
}

Thanks in advance