Wednesday, January 23, 2019

Line: 15, Column: 1 System.QueryException: List has no rows for assignment to SObject

you usually encounter this error when your soql query doesn't return any data and you tried to store the results retrieved from the SOQL to the sObject type variable or list of sobject type variable. 

Sunday, November 11, 2018

How to get Days, Hours and Minutes in specified format when two datetime fields are subtracted in formula field in salesforce

In this post we would learn how to get Days, Hours and Minutes in specified format when two datetime fields are subtracted in formula field in salesforce ?

Solution: 

TEXT(
FLOOR((Now()- CreatedDate))
) & " Days " &
TEXT(
FLOOR(MOD(((Now()- CreatedDate))*24,24))
) &" Hours " &
TEXT(
ROUND(MOD(((Now()- CreatedDate))*24*60),0)

)

you can replace Now() and createddate according to your datetime fields which you want to get datetime from.

Thursday, September 6, 2018

How to insert records of two different objects to the database using one single dml statement in salesforce??

This is the question that is usually asked in companies during interview that how can you  insert records of two different objects using one single dml statement. so after reading this article you will be able to explain your interviewer that how to do this.


  1.  First you need to create the instance of each object whether you are working with salesforce standard objects or custom objects. for example
          Account acc1 = new Account(Name = 'anyName');
          Contact con1 = new Contact(LastName = 'anyLastName');
         
       2.  Then you need to create the list of sObject to insert those instances created above into it.

          List<sObject> sObjectList = new List<sObject>();
  
       3.  Now you have to add those instances of different objects to the sObject List

          sObjectList.add(acc1);
          sObjectList.add(con1);
      
        4.  Now simply write one insert dml statement for sObjectList to insert both objects records                     simultaneously to the database.
     
            insert sobjectList;

hope you get useful information by reading the article.

Thanks.



Tuesday, September 4, 2018

How to enable user to enter only integer values to the input box using jQuery.


Answer:

Put this code in script tags and you'll be only able to enter only integer values in the input box.

$(document).ready(function(){
    $("input").keyup(function(){
      $(this).val( $(this).val().replace(/[^0-9]+/, '') );
        if($(this).val() == ''){
          $(this).val(0);                                 
        } 
    });
});



Friday, August 24, 2018

How to pass parameters to a JS function from VF page and then call apex method.??

Suppose requirement is that you want to send the id of a record to the apex method and your records are displaying on visualforce page using apex repeat. So this example will solve your problem.

visualforce page code


<apex:repeat value="{!contactList}" var="contact">
                    <tr>
                        <td>{!contact.Account.Name}</td>
                        <td onclick="myFunc('{!contact.Id}'); return false;">{!contact.FirstName}</td>
                        <td>{!contact.LastName}</td>
                        <td>{!contact.Phone}</td>
                    </tr>                                 
</apex:repeat>

so on the click of firstname you will call a js function that will call your action function and then you can call your apex method and pass your parameters


<script>
function myFunc(conId){           
         callActionFunction(conId);         
 }
         
</script>

your action function code put this code to your visualforce page


<apex:form id="fd">
            <apex:actionFunction action="{!callApexMethod}" name="callActionFunction"  rerender="">
            <apex:param id="myParam" name="contactid" value="" />
            </apex:actionFunction>
           
  </apex:form>

Apex Class


public void callApexMethod(){
         string passedParam1 = Apexpages.currentPage().getParameters().get('contactid');
      // write your logic here what you wanna perfom with id you just received from the contact recordyou click
}

Hopefully you get some information from this blog. If you have any problem you can comment down so that i can solve your problem. Thanks!