Play Games

Search This Blog

Wednesday, October 28, 2015

Insert Image as Attachment in Salesforce

If you have the image url and if you want to insert that image as attachment to any object,
then you need to do get request and then insert attachment with the content of response data.

Apex class :
public class InsertImageAsAttachment{
    public void insertImage() {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Purple_flower_%284764445139%29.jpg/581px-Purple_flower_%284764445139%29.jpg');
        req.setMethod('GET');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        blob picture = res.getBodyAsBlob();
        Attachment attachObj = new Attachment (ParentId = '00128000003yhTi',Body = picture,Name = 'flower.jpg');
        insert attachObj;
   }
}

In this example,I have taken one image in google whose url is "https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Purple_flower_%284764445139%29.jpg/581px-Purple_flower_%284764445139%29.jpg".
ParentId will be the id of object to which you need to attach image as attachment.

When you call insertImage() ,the image will get inserted as attachment.
Output:




Note: 1)You need to add the endpoint url in remote site settings.
          2)The endpoint url will be the url of image.
          3) If the image is in any external system which needs authentication,then you need
              to do authentication as well.
      

Monday, October 19, 2015

How to get current page parameter (url parameters) value in javascript function of visualforce page


syntax: var paramValue = '{!$CurrentPage.parameters.urlParameter}' where urlParameter is the url parameter name.

Example:
        Visualforce Page :

  <apex:page >
  <script>
      window.onload = function loadPage() {
          var paramValue = '{!$CurrentPage.parameters.oppId}';
          alert(paramValue);
      }
  </script>
</apex:page>

If you run the above vf page by passing oppId parameter like below
"https://c.ap2.visual.force.com/apex/urlParameter?oppId=00628000003CrDz"
then in the alert you will get the required parameter value.

Wednesday, October 14, 2015

Error: Compile Error: Incompatible types since an instance of SObject is never an instance of opportunity

Generally we get this error when we are not doing type casting properly.
Recently I got this error when I am saving the below apex class.

Apex class :
public class OpportunityStatusChartController {
    public Opportunity opp;
 
    public OpportunityStatusChartController(ApexPages.StandardController std) {
        opp=(Opportunity) std.getRecord();
    }

}
Error:

Even though code is correct,I am getting this error.
Finally I understood that a class with name "Opportunity" is created in my instance
which is causing this error.
Solution: When ever you face this error,please check whether a class with that name
 is created and if a class exists,delete it

Tuesday, October 13, 2015

If Condition in rendered attribute in Visualforce Page

syntax : <apex:pageBlockSection title="My Content Section" columns="2" rendered="{!IF(condition,true,false)}">
Example:
Apex Class:
public class IfConditionInRendered {
    public integer count {get;set;}
    public IfConditionInRendered(ApexPages.StandardController controller) {
        count = 1;
    }
}
visualforce Page
<apex:page standardController="Account" extensions="IfConditionInRendered">
    <apex:form >
        <apex:pageBlock title="Account Section" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="My Content Section" columns="2" rendered="{!IF(count==1,true,false)}">
                <apex:inputField value="{!account.name}"/>
                <apex:inputField value="{!account.site}"/>
                <apex:inputField value="{!account.type}"/>
                <apex:inputField value="{!account.accountNumber}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Initial Output:

Now change count value in controller  to 2.
Output :




Saturday, October 10, 2015

Special character issue in Dataloader

Sometimes we have special characters like John Villase�or in the csv file that is provided by external system or Client. If we directly load the data by using dataloader ,you dont get exact text in salesforce.

Please follow the below steps to resolve the issue.

1.First save  it in excel format(.xls) //suppose file name is test.xls
2.Now open the text.xls file and select file--->save as--->choose desktop(As ur wish) --> then give the file name 'sample' and save as type :unicode text
3.Then open the sample file in notepad,here you have tab space in between the headers,so you need to replace the tab space with ','(use replace all option in notepad).
4.Then save the file as below
  filename:test.csv
  saveastype: all file
 Encoding:UTF-8



Friday, October 9, 2015

How to get Object name based on ID in salesforce


id m='006Z000000El76yIAB';
String sObjName = m.getSObjectType().getDescribe().getName();
system.debug('Object Name :'+sObjName );

output: Object Name :Opportunity