• Naveen Kumar B H(bhns)
  • NEWBIE
  • 134 Points
  • Member since 2015
  • Salesforce developer
  • KVP BUSINESS SOLUTIONS

  • Chatter
    Feed
  • 4
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 19
    Replies
Hi All,
I have created a trigger that checks to see if another Lead or Contact has the exact same name. How can I avoid the for loop inside for loop to compare old and new lead values and avoid running SOQL inside loop to get matching contacts? Code below:
 
trigger DuplicateLeadOrContact on Lead (before insert) {
    list<Lead> leadList = new list<Lead>();
    list<Contact> conList = new list<Contact>();
    leadList = [SELECT Id, firstname, lastname from LEAD];
    for(Lead myLead : Trigger.New){
        if(myLead.firstName != null && myLead.lastName != null){
            for(Lead oldLead : leadList){
                if(myLead.firstName == oldLead.firstname && myLead.lastName == 
           oldLead.lastName){
                    myLead.addError('Lead already Exists with same Lead first 
               name and last name');
                }

            }

        }
        conList = [SELECT Id, firstname, lastname from Contact where firstname 
        =:myLead.firstName AND lastName =:myLead.lastName];
        if(conList.size() > 0){
            myLead.addError('Lead already Exists with same Contact first name 
        and last name');
        }
       }


}

 
  • March 17, 2018
  • Like
  • 0
Hi all
I am not sure why I am getting this error on my trigger, may someone please point me in the right direction. 
 
trigger PopulateRegion on Shipment_Order__c (after insert, after update) {

for(Shipment_Order__c shipmentorder : Trigger.new) {

If(shipmentorder.Hub_Shipment_Formula__c == False ){

        string region = Country_Region_List__c.getInstance(shipmentorder.Ship_From_Country__c).Region__c;
        shipmentorder.Ship_From_Zone1__c = region; 
}
    
    Else { 
    string region3 = Country_Region_List__c.getInstance(shipmentorder.Ship_From_Country__c).Region__c;
    string region2 = Country_Region_List__c.getInstance(shipmentorder.HUB_Country_Formula__c).Region__c;
    shipmentorder.Ship_From_Zone1__c = region3;
    shipmentorder.Hub_Ship_From_Zone__c= region2;

}

}
}

 
@RestResource(urlMapping='/mhc/*')

global  class CaseManager {

 

    @HttpGet

    global static void getCaseById() {

      

        // grab the caseId from the end of the URL
        String jsonResponse = '';
       RestRequest request = RestContext.request;
        RestResponse res = RestContext.response;
        string caseId= request.params.get('caseId');
          
            
        try{
        if(caseId!=null  ){
            
       Case result =  [SELECT CaseNumber,Subject,Status,Origin,Priority

                        FROM Case

                        WHERE Id = :caseId];
                     
             res.responseBody = Blob.valueOf(JSON.serialize(result));
            res.statusCode = 200;
            }
             else{
                
            throw new caserestexception(400,'aaaa');
            }
            }
           
            
        catch(caserestexception ex)
        {
             caserestexception aa;
             res.statusCode = 400;
            jsonResponse = '{"response": {"status": "Failure", "message": "' + ex + '"}}';
            res.responseBody = blob.valueOf(jsonResponse);
            return;
        }
                 
            
            
             
            
               


    }
    
}

I want to get my own error but i'm getting salesforce standard error.how to override standard error

 
global class inboundEmailattachment implements messaging.inboundemailhandler {
    global messaging.inboundemailresult handleinboundemail(messaging.inboundemail email,messaging.inboundEnvelope evn){
        messaging.inboundemailresult res=new messaging.inboundemailresult();
        contact con=[select lastname,email from contact where email=:email.fromaddress];
        case c=new case();
        c.Subject=email.subject;
        c.Description=email.plaintextbody;
        c.origin='web';
        c.status='new';
        c.priority='high';
        c.contactid=con.id;
        insert c;
        messaging.inboundemail.binaryattachment[] attach=email.binaryattachments;
        list<attachment> mylist=new list<attachment>();
        for(messaging.inboundemail.binaryattachment b:attach){
            attachment a=new attachment();
            a.name=b.filename;
            a.Body=b.body;
            a.parentid=c.id;
            mylist.add(a);
        }
        insert mylist;
        return res;
    }
}
for this i write test class but it not cover all lines of code
test class:
@istest
public class inboundemailattachmentTest {
    
   static testMethod void TestinBoundEmail()
   {
       Messaging.InboundEmail email = new Messaging.InboundEmail() ;
       Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
      
      email.subject = 'Create Contact';
      email.fromAddress = 'someaddress@email.com';
      email.plainTextBody = 'email body';
      Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
      attachment.body = blob.valueOf('my attachment text');
      attachment.fileName = 'textfileone.txt';
      attachment.mimeTypeSubType = 'text/plain';
      email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };
     
      inboundEmailattachment  testInbound=new inboundEmailattachment ();
      testInbound.handleInboundEmail(email, env);
   }
}
please teach me test class 
 
  • September 24, 2017
  • Like
  • 0

Hi All,

I am using on <apex:actionSupport> on an InputField, and on change of field value, i am passing InputField value to controller using <apex:param>. However the issue is, in controller, iam not getting latest InputField value. Please see below simpler code snippet:

VF page:
<apex:page controller="testcontroller">
     <apex:form >
             <apex:inputField value="{!acct.name}" >
                 <apex:actionSupport event="onchange" action="{!setvar}" reRender="">
                     <apex:param name="setvar" value="{!acct.name}" assignTo="{!var}"/>
                 </apex:actionSupport>
             </apex:inputField>
      </apex:form>
</apex:page>

Controller:
public class testcontroller{

    public String Account { get; set; }

public Account acct{get;set;}
public string var{get;set;}

    public testcontroller(){
        acct = new Account();
    
    }
    
     public void setvar(){
     
        system.debug('var#####'+var);
    
    }
}

Please help me out, if there is any other way to pass this latest field value on change to Controller.

Thanks
 

Hi All,
I have created a trigger that checks to see if another Lead or Contact has the exact same name. How can I avoid the for loop inside for loop to compare old and new lead values and avoid running SOQL inside loop to get matching contacts? Code below:
 
trigger DuplicateLeadOrContact on Lead (before insert) {
    list<Lead> leadList = new list<Lead>();
    list<Contact> conList = new list<Contact>();
    leadList = [SELECT Id, firstname, lastname from LEAD];
    for(Lead myLead : Trigger.New){
        if(myLead.firstName != null && myLead.lastName != null){
            for(Lead oldLead : leadList){
                if(myLead.firstName == oldLead.firstname && myLead.lastName == 
           oldLead.lastName){
                    myLead.addError('Lead already Exists with same Lead first 
               name and last name');
                }

            }

        }
        conList = [SELECT Id, firstname, lastname from Contact where firstname 
        =:myLead.firstName AND lastName =:myLead.lastName];
        if(conList.size() > 0){
            myLead.addError('Lead already Exists with same Contact first name 
        and last name');
        }
       }


}

 
  • March 17, 2018
  • Like
  • 0
Hello!
How to run action="{!save}" and oncomplete="reload()"  automaticly on VF page in SF1 without apex:commandButton?
 
<apex:page standardController="Project__c" id="page" docType="html-5.0">

<script>
function  reload(){
   sforce.one.back(true);
}
</script>

<apex:form id="form">

<apex:commandButton action="{!save}"  value="Save" oncomplete="reload()"/>
                      
</apex:form>

</apex:page>

Thank you!
Hi All,

I have a lightning custom button in lightning component and I want to call the another lightning component on click of this button. 
Please help me to achive this. 
Thanks in advance.

Parteek
Hi all , 
I am getting below error while change the Storage__C owner from X to Y. 
caused by: System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.

 
trigger StevContacts on Storage__C (after update) 
{
    
    List<Storage__C> Strs = [select Id, Type, (select Primary_Campaign__c from Contacts) from Storage__C where Id IN :Trigger.newMap.keySet()];
    List<Contact> contacts = new List<Contact>();
    Storage__C oldStrs;

    for(Storage__C s: Strs)
    {
        oldStrs = Trigger.oldMap.get(s.Id);
      
        if (oldStrs.Type != s.Type && s.Type == 'Stev')
        {       
            for(Contact c: s.Contacts)
            {
                c.Primary_Campaign__c='Stev';
                contacts.add(c);
            } 
        }
        else if (oldStrs.Type != s.Type && oldStrs.Type == 'Stev')
        {  
            for(Contact c: s.Contacts)
            {
                c.Primary_Campaign__c='ting';
                contacts.add(c);
            } 
        }
    update contacts;
    }
}
I have gone through documents . Found below below points  but not able fiqure it out .. where exactly i have to do changes in my code. 

Options to resolve error
1. You may find that the query in question needs to be more selective in the WHERE clause. According to the Salesforce standards & best practices - the where clause needs to subset 10% or less of the data.
2. A custom index on the field.
3. A possible quick fix may be to make the field in question an external ID. Since external IDs are indexed automatically, this will create the index and may solve the problem.

Appreciate your help 
Hi all,
Looking for the best way to complete switching on an API from Loan Origination Software into my SF Org. Challenge I'm facing is, I'm currently using the standard object Lead to house all of the Lead data, and I have two objects "Closed Contact" (standard object Contacts with only contact information) and "Closed Transaction" (custom object that houses all of the loan data from LOS). the API will help communicate all of the "In Process" loans that we have but I don't want to build out a 4th Object to house all of this data. I'm thinking I can do this one of two ways: 

1: Simply rename "Closed Contacts" back to "Contacts" and "Closed Transactions" to "Transactions" and reconfigure the fields based on what I  need for the LOS and have the LOS directly map the API field names to use the Leads object for the Leads status files, and everything else to import under the Contact and Transaction objects based on their statuses......OR....
2. Build out another custom object, "In Process".
***I would need to incorporate processes to also migrate data from multiple fields on the Leads object to multiple fields on both the Closed Contact and Closed Transaction page layouts if the status were to change and remove them from the Lead status and place them into "In Process", which I'm also not sure of how to do***

Is this even possible? If you're still reading this, thank you for your help in advance!!!! 
Hi all
I am not sure why I am getting this error on my trigger, may someone please point me in the right direction. 
 
trigger PopulateRegion on Shipment_Order__c (after insert, after update) {

for(Shipment_Order__c shipmentorder : Trigger.new) {

If(shipmentorder.Hub_Shipment_Formula__c == False ){

        string region = Country_Region_List__c.getInstance(shipmentorder.Ship_From_Country__c).Region__c;
        shipmentorder.Ship_From_Zone1__c = region; 
}
    
    Else { 
    string region3 = Country_Region_List__c.getInstance(shipmentorder.Ship_From_Country__c).Region__c;
    string region2 = Country_Region_List__c.getInstance(shipmentorder.HUB_Country_Formula__c).Region__c;
    shipmentorder.Ship_From_Zone1__c = region3;
    shipmentorder.Hub_Ship_From_Zone__c= region2;

}

}
}

 
HI Experts,

i have written a test class but i got code coverage 65%, i need more than 75%. kindly help me on this.
 
@istest
public class AccConUpd_test
{
 static testMethod void testMethod1() 
 {
 
 Account testAccount = new Account();
    testAccount.Name='Test Account' ;
    
    insert testAccount;
 
 Contact cont = new Contact();
        cont.FirstName='a1123';
        cont.LastName='rest';
        cont.Accountid= '0019000001xfaMI';
        cont.Employee_Id__c='1091637612';
        cont.PAN_Number__c='apgffg';
        cont.Working_Days_Per_Week__c=7;
        cont.Phone='12344';
        
        insert cont;
 }
}

User-added image


Thanks!

Sai
@RestResource(urlMapping='/mhc/*')

global  class CaseManager {

 

    @HttpGet

    global static void getCaseById() {

      

        // grab the caseId from the end of the URL
        String jsonResponse = '';
       RestRequest request = RestContext.request;
        RestResponse res = RestContext.response;
        string caseId= request.params.get('caseId');
          
            
        try{
        if(caseId!=null  ){
            
       Case result =  [SELECT CaseNumber,Subject,Status,Origin,Priority

                        FROM Case

                        WHERE Id = :caseId];
                     
             res.responseBody = Blob.valueOf(JSON.serialize(result));
            res.statusCode = 200;
            }
             else{
                
            throw new caserestexception(400,'aaaa');
            }
            }
           
            
        catch(caserestexception ex)
        {
             caserestexception aa;
             res.statusCode = 400;
            jsonResponse = '{"response": {"status": "Failure", "message": "' + ex + '"}}';
            res.responseBody = blob.valueOf(jsonResponse);
            return;
        }
                 
            
            
             
            
               


    }
    
}

I want to get my own error but i'm getting salesforce standard error.how to override standard error

 
trigger CaseOwnerChangeTrigger on Case (before insert,before update) 

  
    
    Map<ID, Account> AccownerIds = new Map<ID, Account>();

  
    set<id> accID = new set<id>();

for(Case cn : Trigger.new) 

   if(cn.AccountID!=null)
  { 
  system.debug(cn.AccountID);
      accID.add(cn.AccountID); 
  } 
}

 for(Account an : [select id,ownerid from account where id IN:accID]) 

AccownerIds.put(an.id,an.ownerid); 


for(case cb : Trigger.new)
 { 
if(AccownerIds.containsKey(cb.AccountID)) 

cb.ownerID = AccownerIds.get(cb.AccountID);
 } 
}
}
global class inboundEmailattachment implements messaging.inboundemailhandler {
    global messaging.inboundemailresult handleinboundemail(messaging.inboundemail email,messaging.inboundEnvelope evn){
        messaging.inboundemailresult res=new messaging.inboundemailresult();
        contact con=[select lastname,email from contact where email=:email.fromaddress];
        case c=new case();
        c.Subject=email.subject;
        c.Description=email.plaintextbody;
        c.origin='web';
        c.status='new';
        c.priority='high';
        c.contactid=con.id;
        insert c;
        messaging.inboundemail.binaryattachment[] attach=email.binaryattachments;
        list<attachment> mylist=new list<attachment>();
        for(messaging.inboundemail.binaryattachment b:attach){
            attachment a=new attachment();
            a.name=b.filename;
            a.Body=b.body;
            a.parentid=c.id;
            mylist.add(a);
        }
        insert mylist;
        return res;
    }
}
for this i write test class but it not cover all lines of code
test class:
@istest
public class inboundemailattachmentTest {
    
   static testMethod void TestinBoundEmail()
   {
       Messaging.InboundEmail email = new Messaging.InboundEmail() ;
       Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
      
      email.subject = 'Create Contact';
      email.fromAddress = 'someaddress@email.com';
      email.plainTextBody = 'email body';
      Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
      attachment.body = blob.valueOf('my attachment text');
      attachment.fileName = 'textfileone.txt';
      attachment.mimeTypeSubType = 'text/plain';
      email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };
     
      inboundEmailattachment  testInbound=new inboundEmailattachment ();
      testInbound.handleInboundEmail(email, env);
   }
}
please teach me test class 
 
  • September 24, 2017
  • Like
  • 0