0

I'm using the following code to upload an attachment to an Account record, currently it's working but I have the Account Id hard coded while I test.

Now I'm trying to figure out how to query Accounts and grab the Id where Email__c is equal to the $userEmail that I'm passing through.

function sfWrap($userEmail){

    define("USERNAME", "[email protected]");
    define("PASSWORD", "MyPasswordandSecurityToken");
    define('CLIENT_ID', 'MYCLIENTID');
    define('CLIENT_SECRET', 'MYCLIENTSECRET');


    $loginurl = "https://test.salesforce.com/services/oauth2/token";

    $params = "grant_type=password"
    . "&client_id=" . CLIENT_ID
    . "&client_secret=" . CLIENT_SECRET
    . "&username=" . USERNAME
    . "&password=" . PASSWORD;

    $curl = curl_init($loginurl);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

    $json_response = curl_exec($curl);

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    if ( $status != 200 ) {
        die("Error: call to URL failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
    }

    curl_close($curl);

    $sfData = json_decode($json_response);

    $access_token = $sfData->access_token;
    $instance_url = $sfData->instance_url;


    $url = $instance_url . "/services/data/v20.0/sobjects/Attachment/";         
    $accId= '0015B000002MzlbQAC';

    $fp = fopen('MyFile.pdf', 'r');
    $content = fread($fp, filesize('MyFile.pdf'));
    $fileData = base64_encode($content);
    $data = array("ParentId" => $accId, "Name" => 'MyFile.pdf', "body" => "$fileData", "IsPrivate" => "false");
    $data_string = json_encode($data);
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER,
    array("Authorization: OAuth $access_token","Content-type: application/json"));
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

    $json_response = curl_exec($curl);

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    if ( $status != 201 ) {
        die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
    }

    // echo "HTTP status $status uploading file failed<br/><br/>";
    curl_close($curl);

    $response = json_decode($json_response, true);

    var_dump($response);
}

So you'll see in the script I've hard coded my Account Id: $accId= '0015B000002MzlbQAC'; but instead, I need to query the Account Id based on the email I pass to the function.

1 Answer 1

0

Got this working with the following:

$queryUrl = $instance_url . "/services/data/v20.0/query/?q=SELECT+id+from+Account+where+Email__c='$userEmail'";
        $curl = curl_init($queryUrl);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_HTTPHEADER,
        array("Authorization: Bearer $access_token","Content-type: application/json"));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, false);
        $response = curl_exec($curl);
        curl_close($curl);

        $accData = json_decode($response);

        foreach($accData->records as $record) {
            $accId = $record->Id;
        }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .