Tuesday, 22 December 2015

How to create a case record from a VF page and attach a particular file to the record?

Below is code snippet for the same.


VF Page:-
<apex:page controller="creatingcaseAttachment">
  <apex:form >
      <apex:pageBlock title="Customer Issue">
      
          <apex:pageBlockSection title="Case Creation ">
              <apex:inputField value="{!customerCase.accountid}"/>  
              <apex:inputField value="{!customerCase.contactId}"/>  
              <apex:inputField value="{!customerCase.Origin}"/>  
              <apex:inputField value="{!customerCase.Status}"/>  
              <apex:inputField value="{!customerCase.Reason}"/>
              <apex:inputField value="{!customerCase.Subject}"/>  
              
              <apex:outputPanel style="float:right;">
                <apex:inputfile value="{!objAttachment.body}" filename="{!objAttachment.name}" ></apex:inputfile>
                <apex:commandButton value="Create Case" action="{!CaseCreation}"/>
               </apex:outputPanel>
               
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page> 

Controller:-
public with sharing class creatingcaseAttachment {
    
    public Case customerCase{get;set;}
     Public attachment objAttachment{get; set;}
    
    public creatingcaseAttachment (){
        customerCase = new Case();
        objAttachment = new Attachment();
    } 
     Public PageReference CaseCreation(){
        Insert customerCase;
        
        objAttachment.ParentId = customerCase.id;
        insert objAttachment;
        
        PageReference p  = new PageReference('/'+customerCase.id);
        p.setRedirect(true);
        return p;
    }
}

Monday, 21 December 2015

How to remove particular key from a map?

This is pretty simple one, One of my friend asked me how to remove a particular key-value pair from a map.

Below is code snippet for the same. Hope It will help all.

Map<String, String> colorCodes =   new Map<String, String>();

colorCodes.put('Red', 'FF0000');
colorCodes.put('Blue', '0000A0');
colorCodes.put('Blue2', '0000A0');

// If you know the key then just use like below
     colorCodes.remove('Blue2');
System.debug('printing=== '+colorCodes);
//if you are not sure what about the key or need some manipulation then follow below approach
for(String key:colorCodes.keySet().clone()) {
    // myMap.get(key), process that value
    colorCodes.remove('Blue2');
}
System.debug('printing==ater= '+colorCodes);



Thursday, 10 December 2015

How to get sandbox name in apex?

As we all know sandbox username looks like "behera@mycompany.com.dev "

dev- is the sandbox name, How can we get it this dynamically ?

Use below code :-

if(UserInfo.getUserName().substringAfterLast('.com')!= null)
                    contact.Email = contactInfo.Email+'.'+UserInfo.getUserName().substringAfterLast('.');
                else
                    contact.Email = contactInfo.Email;
}

UserInfo.getUserName().substringAfterLast('.') - will give you sandbox name.

UserInfo.getUserName().substringAfterLast('.com') will tell you whether anything is there after " .com ". if not there that means we are in production otherwise in sandbox. 
We can also used same method to identify type of org like prod or non prod. There is another approach for this. 

Organization org = [select id,IsSandbox from Organization];
org.IsSandbox = true if it a sandbox. It willwork classs of higher version.

Feel free to suggest more.

Thursday, 3 December 2015

Workaround for COMPILE ERROR: Field is not writeable: Account.IsPartner

As part of my requirement, I had to create an account making isPartner as true, Unfortunately I got this  COMPILE ERROR: Field is not writable: Account.IsPartner 



Then I first inserted that account then updated isPartner as true.


Account acc = new Account(name = 'Asish');
insert acc;
System.debug('Inserted account Ispartner ---'+acc.Ispartner);
acc.Ispartner = true;
update acc;
System.debug('Inserted account---'+acc);

For more info on Ispartner field--  https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_account.htm