Expected non-empty Guid

本文介绍了在使用DynamicCRM创建数据时遇到的'Expected non-empty Guid'错误,原因在于lookup字段赋值时实体实例未获取到guid。给出了正确的赋值方式:通过LogicalName和id,避免了空值问题。

背景:dynamic crm在用组织服务创建数据的时候报错:Expected non-empty Guid

原因:因为给lookup字段赋值的时候,数据的guid为空。

例子:

            Entity approvalInfo = new Entity("new_approvalcenter");
            approvalInfo["subject"] = "测试银行";
            approvalInfo["regardingobjectid"] = new EntityReference("eden_bank", new     Guid("20f63327-0fbb-ec11-a12c-005056ac8d5a"));
            var id = svc.Create(approvalInfo);

            Entity updateTodo = new Entity("new_approvalcenter");
            updateTodo.Id = new Guid("4703c443-18cc-ec11-a130-005056ac8d5a");
            updateTodo["statecode"] = new OptionSetValue(1);
            updateTodo["statuscode"] = new OptionSetValue(2);
            updateTodo["regardingobjectid"] = approvalInfo.ToEntityReference();
            svc.Update(updateTodo);

上诉例子,approvalInfo是新创建的,然后直接拿来用,因为创建的时候并不会把guid赋值给当前实例,如果赋值的话就报错:Expected non-empty Guid

解决方案:赋值方式改为如下

updateTodo["regardingobjectid"] = new EntityReference(approvalInfo.LogicalName, id);

# CurlTools.psm1 - 企业级安全下载模块 $projectRoot = $PSScriptRoot # 从清单文件获取路径配置 $manifest = Test-ModuleManifest -Path (Join-Path $projectRoot "CurlTools.psd1") -ErrorAction Stop $pathConfig = $manifest.PrivateData.PSData.ProjectPaths # 初始化目录路径 $configDir = Join-Path $projectRoot $pathConfig.Config $logDir = Join-Path $projectRoot $pathConfig.Logs $tempDir = Join-Path $projectRoot $pathConfig.Temp $curlBinDir = Join-Path $projectRoot $pathConfig.CurlBin # 目录初始化 foreach ($dir in ($configDir, $logDir, $tempDir, $curlBinDir)) { if (-not (Test-Path $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null Write-Verbose "Created directory: $dir" } } function Write-Log { param( [string]$Message, [ValidateSet('Info','Warning','Error','Audit')] [string]$Level = 'Info' ) $logPath = "$logDir\curl_tools_$(Get-Date -Format 'yyyyMMdd').log" $timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss' $logEntry = "[$timestamp] [$Level] $Message" Add-Content -Path $logPath -Value $logEntry # 审计级日志额外记录到单独文件 if ($Level -eq 'Audit') { $auditPath = "$logDir\audit_$(Get-Date -Format 'yyyyMMdd').log" Add-Content -Path $auditPath -Value $logEntry } } function Get-CurlPath { <# .SYNOPSIS 获取curl.exe的完整路径 #> try { $curlPath = Join-Path $curlBinDir "curl.exe" if (-not (Test-Path $curlPath)) { throw "curl.exe not found at: $curlPath" } return $curlPath } catch { Write-Log -Message "Get-CurlPath error: $_" -Level 'Error' throw $_ } } function Get-CurlVersion { <# .SYNOPSIS 获取curl版本信息(内部函数) #> try { $curlPath = Get-CurlPath $versionInfo = & $curlPath --version return $versionInfo[0] } catch { Write-Log -Message "Get-CurlVersion error: $_" -Level 'Error' throw $_ } } function Invoke-SecureDownload { <# .SYNOPSIS 执行安全文件下载 #> [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$Url, [Parameter(Mandatory=$true)] [string]$OutputPath, [ValidateSet('SHA256','MD5','SHA1')] [string]$HashAlgorithm, [string]$ExpectedHash, [int]$MaxRetry = 3, [int]$MaxDownloadSpeed = 0 ) $activity = "Secure Download" Write-Progress -Activity $activity -Status "Initializing..." $retryCount = 0 $tempFile = $null do { try { # 审计日志 Write-Log -Message "DOWNLOAD INITIATED: $Url => $OutputPath" -Level 'Audit' Write-Log -Message "Starting download (attempt $($retryCount+1)/$MaxRetry): $Url" -Level 'Info' # 域名验证 $domain = ([uri]$Url).Host $configPath = Join-Path $configDir "config.json" if (-not (Test-Path $configPath)) { throw "Config file not found: $configPath" } $config = Get-Content $configPath | ConvertFrom-Json -ErrorAction Stop $isAllowed = $false foreach ($allowed in $config.AllowedDomains) { if ($domain -eq $allowed -or $domain.EndsWith(".$allowed")) { $isAllowed = $true break } } if (-not $isAllowed) { $msg = "Domain blocked by policy: $domain" Write-Log -Message $msg -Level 'Warning' throw $msg } # 输出目录准备 $outputDir = Split-Path $OutputPath -Parent if (-not (Test-Path $outputDir)) { New-Item -Path $outputDir -ItemType Directory -Force | Out-Null Write-Log -Message "Created directory: $outputDir" } # 下载执行 $curlPath = Get-CurlPath $tempFile = Join-Path $tempDir "$([System.IO.Path]::GetRandomFileName()).tmp" $arguments = @( "-L", "--progress-bar", "--fail", "-o", "`"$tempFile`"", "`"$Url`"" ) # 添加限速参数 if ($MaxDownloadSpeed -gt 0) { $arguments += "--limit-rate" $arguments += "$($MaxDownloadSpeed)k" } Write-Progress -Activity $activity -Status "Downloading..." $process = Start-Process -FilePath $curlPath -ArgumentList $arguments ` -NoNewWindow -PassThru -Wait if ($process.ExitCode -ne 0) { throw "Download failed with exit code $($process.ExitCode)" } # 文件验证 if (-not (Test-Path $tempFile)) { throw "Downloaded file not found" } if ((Get-Item $tempFile).Length -eq 0) { throw "Downloaded file is empty" } # 哈希验证 if ($PSBoundParameters.ContainsKey('HashAlgorithm')) { Write-Progress -Activity $activity -Status "Verifying hash..." $actualHash = (Get-FileHash -Path $tempFile -Algorithm $HashAlgorithm).Hash Write-Log -Message "File hash ($HashAlgorithm): $actualHash" -Level 'Info' if ($PSBoundParameters.ContainsKey('ExpectedHash') -and $actualHash -ne $ExpectedHash) { $msg = "Hash verification failed! Expected: $ExpectedHash, Actual: $actualHash" Write-Log -Message $msg -Level 'Error' throw $msg } } # 最终文件移动 Move-Item -Path $tempFile -Destination $OutputPath -Force Write-Log -Message "Download completed: $OutputPath" -Level 'Info' # 在模块清单文件 (CurlTools.psd1) 中添加 FunctionsToExport = @('Get-CurlPath', 'Get-CurlVersion', 'Invoke-SecureDownload') # 在模块主体中实现 Get-CurlVersion function Get-CurlVersion { $curlPath = Get-CurlPath try { $result = & $curlPath --version 2>&1 return ($result | Select-Object -First 1).Trim() } catch { Write-Error "Failed to get curl version: $_" return $null } } # 添加配置注入点 $script:ModuleConfig = @{ AllowedDomains = @("example.com", "github.com") MaxDownloadSpeed = 1024 } function Set-ModuleConfig { param( [hashtable]$Config ) $script:ModuleConfig = $Config } Write-Log -Message "DOWNLOAD COMPLETED: $Url => $OutputPath" -Level 'Audit' return $true } catch { $retryCount++ Write-Log -Message "Download attempt $retryCount failed: $_" -Level 'Warning' if ($retryCount -ge $MaxRetry) { Write-Log -Message "Max retries reached for: $Url" -Level 'Error' throw "Download failed after $MaxRetry attempts: $_" } # 等待指数退避时间 $delay = [Math]::Pow(2, $retryCount) Write-Progress -Activity $activity -Status "Retrying in $delay seconds..." Start-Sleep -Seconds $delay } finally { if ($null -ne $tempFile -and (Test-Path $tempFile)) { Remove-Item $tempFile -Force -ErrorAction SilentlyContinue } } } while ($retryCount -lt $MaxRetry) } # 导出公共函数 Export-ModuleMember -Function 'Get-CurlPath', 'Invoke-SecureDownload'
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值