Salesforce 数据迁移

关于整个Org 迁移,都是在有限的时间里 摸索着前行:

1.整理好哪些元数据要迁移,PackMagix,整理好list ,与客户确认,确认好后准备迁移;

2.目前市面上有很多的迁移工具,选择一款适应你的,如果项目比较拮据,可以选择VS Code.

3.可以从目标环境中 使用 Generator xml,下载 元数据,然后部署到目标环境;

4.部署代码

5.导入数据:追加Migration_GUID__c ,保存源数据ID,同样在目标系统中,将Auto-number 的 Name 设置为text型,导完数据后在改为Auto。

使用dataloaser 迁移,需要新旧Id 替换, 注意乱码,科学计数法;

6.乱码问题,使用dataloader 导出数据,notepad++ 打开,设置编码格式UTF-8 BOM 形式保存,再用csv打开,copy到excel中,进行编辑保存;或者从excel中,设置文本格式,从csv 读入数据;

7.文件迁移:使用谷歌插件Tabs to save 下载附件,或用 Pythodn 脚本下载

Excel显示数字时,如果数字大于12位,它会自动转化为科学计数法;如果数字大于15位,它不仅用于科学计数法表示,还会只保留高15位,其他位都变0。

import csv
import requests
import shutil

# Salesforce实例URL和Access Token
instance_url = ''
access_token = '00D90000000w9e0!AQcAQNVcdSLh0AQcn.ORnHmAjOZSkrYE8rraspklugUgqS5jngdgc4.OQwkWDmDMbio8Zn4PgcOAuiG9ld1DQ_CFDZjVH6_l'

# CSV文件路径和名称,确保路径正确和文件存在
csv_file = r'\test_contentVersion.csv'

# 读取CSV文件并下载ContentVersion附件
def download_contentversion_attachments(csv_file, instance_url, access_token):
    session = requests.Session()
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/octet-stream'
    }

    with open(csv_file, newline='', encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            contentversion_id = row['Id']  # 假设CSV中有Id列
            attachment_name = row['Title']  # 假设CSV中有FileName列
            #download_url = f"https://{instance_url}/services/data/v60.0/sobjects/ContentVersion/{contentversion_id}/VersionData"
            download_url = "https://%s%s%s/VersionData" % (instance_url, '/services/data/v60.0/sobjects/ContentVersion/', contentversion_id)
            # 下载附件
            response = session.get(download_url, headers=headers, stream=True, allow_redirects=True)
            
            if response.status_code == 200:
                content_type = response.headers['Content-Type']
                # 检查下载的内容是否是HTML
                if content_type.startswith('text/html'):
                    print(f'{attachment_name} 下载的内容是HTML,可能是错误页面')
                    print(response.text)  # 打印HTML内容以供检查
                else:
                    try:
                        # 保存附件到本地文件,注意文件名编码问题
                        with open(attachment_name, 'wb') as file:
                            shutil.copyfileobj(response.raw, file)
                        print(f'{attachment_name} 下载成功. 内容类型: {content_type}')
                    except Exception as e:
                        print(f'{attachment_name} 不是有效的文件: {e}')
            else:
                print(f'下载 {attachment_name} 失败. 状态码: {response.status_code}')
                print(f'响应内容: {response.text}')

# 执行下载
if __name__ == '__main__':
    download_contentversion_attachments(csv_file, instance_url, access_token)

 

posted @ 2024-09-09 10:05  BandariFang  阅读(5)  评论(0编辑  收藏  举报