Search In This Blog

2020-07-03

Upload file/attachment | Apex Visualforce | Salesforce

template:
<apex:form>
    <apex:inputFile value="{!file}" />
<apex:commandbutton action="{!upload}" value="Upload" />
</apex:form>
Apex:
// file
public blob file {get;set;}
pubilc PageReference upload(){
    ContentVersion photofile = new ContentVersion();
    if(file != null){
        photofile.VersionData = file;
        photofile.Title='title';
        photofile.IsMajorVersion = false;
        photofile.PathOnClient='/title';
        insert photofile;
    }
    try{
        insert photofile;
    } catch(Exception Ex){
        System.debug('Ex==>'+Ex);
    }
    photofile = [select id, ContentDocumentId from ContentVersion WHERE Id =: photofile.Id];

    ContentDocumentLink cdl = new ContentDocumentLink();
    cdl.ContentDocumentId = photofile.ContentDocumentId;
    cdl.LinkedEntityId = [parentId]; 
    cdl.ShareType = 'V';
    cdl.Visibility = 'AllUsers';

    try{
        insert cdl;
    } catch(Exception Ex){
        System.debug('Ex==>'+Ex); 
    }
}
// Attachment
public blob file {get;set;}
pubilc PageReference upload(){
    try {
        if(file != null){
            attachment.body = file;
            attachment.name='title';
            attachment.OwnerId = UserInfo.getUserId();
            attachment.ParentId = [parentId];
            attachment.IsPrivate = true;

            insert attachment;
        }else{
            attachment = new Attachment();
        }
    } catch (Exception e) {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'error'));
        return null;
    }
}

2020-05-15

Rule: trigger on the first day at 9 months later / 2 months earlier

9 month later  九ヶ月後
AND(
MONTH(DataOfferScheduledDate__c) <= 3 && TODAY() = DATE(YEAR(DataOfferScheduledDate__c), MONTH(DataOfferScheduledDate__c ) + 9, 1),
MONTH(DataOfferScheduledDate__c) > 3 && TODAY() = DATE(YEAR(DataOfferScheduledDate__c) + 1, MONTH(DataOfferScheduledDate__c) - 3, 1)
)
* Green number should sum up to 12


2 months earlier 二ヶ月前
AND(
MONTH(DataOfferScheduledDate__c) <= 2 && TODAY() = DATE(YEAR(DataOfferScheduledDate__c) - 1, MONTH(DataOfferScheduledDate__c ) + 10, 1),
MONTH(DataOfferScheduledDate__c) > 2 && TODAY() = DATE(YEAR(DataOfferScheduledDate__c), MONTH(DataOfferScheduledDate__c) - 2, 1)
)
* Change red number to any day you want

2020-04-01

Soap Api: Services url is different between Partner WSDL and Enterprise WSDL

Partner WSDL: services/Soap/u/
Enterprise WSDL: services/Soap/c/

Example:
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <n1:login xmlns:n1="urn:partner.soap.sforce.com">
      <n1:username>your_username</n1:username>
      <n1:password>your_password</n1:password>
    </n1:login>
  </env:Body>
</env:Envelope>

https://login.salesforce.com/services/Soap/u/48.0
 

2020-03-02

ERROR: VS Code deploy source to salesfores return 1

The situation
  • First time install VSC and salesforce extension pack
  • Success in web login auth to sfdc
  • Success in download source from sfdc
  • Failed in deploy

Fix by:
Upgrade the version of salesforce extension pack.
Terminal command: sfdx update

2020-01-15

getElement is not working in Lightning tag

SFDC defines it as Invalid DOM Access.
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/security_dom.htm
<lightning:card aura:id="map" title="Current Boat Location">
    <div style="{!'width: ' + v.width + '; height: ' + v.height}">
        Please make a selection
    </div>
</lightning:card>

Because c:domLocker, below will be error.
var mapElement = component.find("map").getElement();

Change Dom as:
<lightning:card title="Current Boat Location">
    <div aura:id="map" style="{!'width: ' + v.width + '; height: ' + v.height}">
        Please make a selection
    </div>
</lightning:card>